osxSemaphore.mm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #import <pthread.h>
  23. #import <stdlib.h>
  24. #import <errno.h>
  25. #include "platform/platform.h"
  26. #include "platform/threads/semaphore.h"
  27. //-----------------------------------------------------------------------------
  28. struct PlatformSemaphore
  29. {
  30. pthread_mutex_t mDarkroom;
  31. pthread_cond_t mCond;
  32. S32 count;
  33. };
  34. //-----------------------------------------------------------------------------
  35. Semaphore::Semaphore(S32 initialCount)
  36. {
  37. bool ok;
  38. // Create the semaphore data.
  39. mData = new PlatformSemaphore;
  40. // Initialize the semaphore.
  41. ok = pthread_mutex_init(&mData->mDarkroom,NULL);
  42. // Sanity!
  43. AssertFatal(ok == 0,"Create semaphore failed at creating mutex mDarkroom.");
  44. // Initialize the condition variables.
  45. ok = pthread_cond_init(&mData->mCond,NULL);
  46. // Sanity!
  47. AssertFatal(ok == 0,"Create semaphore failed at creating condition mCond.");
  48. // Set the initial semaphore count.
  49. mData->count = initialCount;
  50. }
  51. //-----------------------------------------------------------------------------
  52. Semaphore::~Semaphore()
  53. {
  54. // Destroy the system semaphore.
  55. pthread_mutex_destroy(&mData->mDarkroom);
  56. pthread_cond_destroy(&mData->mCond);
  57. // Destroy the semaphore data.
  58. delete mData;
  59. }
  60. //-----------------------------------------------------------------------------
  61. bool Semaphore::acquire( bool block, S32 timeoutMS )
  62. {
  63. // Sanity!
  64. AssertFatal(mData, "Semaphore::acquireSemaphore: invalid semaphore");
  65. // Create the system mutex.
  66. bool ok;
  67. ok = pthread_mutex_lock(&mData->mDarkroom);
  68. // Sanity!
  69. AssertFatal(ok == 0,"Mutex Lock failed on mDarkroom in acquireSemaphore().");
  70. // Should we unlock the mutex?
  71. if( mData->count <= 0 && !block )
  72. {
  73. // Yes, so unlock it.
  74. ok = pthread_mutex_unlock(&mData->mDarkroom);
  75. // Sanity!
  76. AssertFatal(ok == 0,"Mutex Unlock failed on mDarkroom when not blocking in acquireSemaphore().");
  77. return false;
  78. }
  79. while( mData->count <= 0 )
  80. {
  81. ok = pthread_cond_wait(&mData->mCond, &mData->mDarkroom); // releases mDarkroom while blocked.
  82. AssertFatal(ok == 0,"Waiting on mCond failed in acquireSemaphore().");
  83. }
  84. // Reduce the semaphore count.
  85. mData->count--;
  86. // Unlock the mutex.
  87. ok = pthread_mutex_unlock(&mData->mDarkroom);
  88. // Sanity!
  89. AssertFatal(ok == 0,"Mutex Unlock failed on mDarkroom when leaving acquireSemaphore().");
  90. return true;
  91. }
  92. //-----------------------------------------------------------------------------
  93. void Semaphore::release()
  94. {
  95. // Sanity!
  96. AssertFatal(mData, "Semaphore::releaseSemaphore: invalid semaphore");
  97. // Lock the mutex.
  98. bool ok;
  99. ok = pthread_mutex_lock(&mData->mDarkroom);
  100. // Sanity!
  101. AssertFatal(ok == 0,"Mutex Lock failed on mDarkroom in releaseSemaphore().");
  102. // Increase the semaphore count.
  103. mData->count++;
  104. if(mData->count > 0)
  105. {
  106. ok = pthread_cond_signal(&mData->mCond);
  107. // Sanity!
  108. AssertFatal(ok == 0,"Signaling mCond failed in releaseSemaphore().");
  109. }
  110. // Unlock the mutex.
  111. ok = pthread_mutex_unlock(&mData->mDarkroom);
  112. // Sanity!
  113. AssertFatal(ok == 0,"Mutex Unlock failed on mDarkroom when leaving releaseSemaphore().");
  114. }