AndroidSemaphore.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // platformSemaphore.h does not ask for any inter process communication,
  23. // and the posix semaphores require a file to be created on disk.
  24. // which could create annoyances if the appication crashes...
  25. // so we'll just roll our own semaphore here.
  26. // note: this is not a bulletproof solution to the starving problem...
  27. // see: The Little Book of Semapores, by Allen B. Downey, at http://greenteapress.com/semaphores/
  28. #include <pthread.h>
  29. #include "platform/platform.h"
  30. #include "platform/threads/semaphore.h"
  31. struct PlatformSemaphore
  32. {
  33. pthread_mutex_t mDarkroom;
  34. // pthread_mutex_t mFoyer; // second lock, to help control starving.
  35. pthread_cond_t mCond;
  36. S32 count;
  37. };
  38. Semaphore::Semaphore(S32 initialCount)
  39. {
  40. bool ok;
  41. PlatformSemaphore* semaphore = new PlatformSemaphore();
  42. ok = pthread_mutex_init(&semaphore->mDarkroom,NULL);
  43. AssertFatal(ok == 0,"Create semaphore failed at creating mutex mDarkroom.");
  44. // ok = pthread_mutex_init(&semaphore->mFoyer,NULL);
  45. // AssertFatal(ok != 0,"Create semaphore failed at creating mutex mFoyer.");
  46. ok = pthread_cond_init(&semaphore->mCond,NULL);
  47. AssertFatal(ok == 0,"Create semaphore failed at creating condition mCond.");
  48. semaphore->count = initialCount;
  49. mData = semaphore;
  50. }
  51. Semaphore::~Semaphore()
  52. {
  53. pthread_mutex_destroy(&mData->mDarkroom);
  54. // pthread_mutex_destroy(&mData->mFoyer);
  55. pthread_cond_destroy(&mData->mCond);
  56. delete mData;
  57. }
  58. bool Semaphore::acquire( bool block, S32 timeoutMS )
  59. {
  60. bool ok;
  61. AssertFatal(mData, "Semaphore::acquireSemaphore: invalid semaphore");
  62. ok = pthread_mutex_lock(&mData->mDarkroom);
  63. AssertFatal(ok == 0,"Mutex Lock failed on mDarkroom in acquireSemaphore().");
  64. if(mData->count <= 0 && !block) {
  65. ok = pthread_mutex_unlock(&mData->mDarkroom);
  66. AssertFatal(ok == 0,"Mutex Unlock failed on mDarkroom when not blocking in acquireSemaphore().");
  67. return false;
  68. }
  69. while( mData->count <= 0 )
  70. {
  71. ok = pthread_cond_wait(&mData->mCond, &mData->mDarkroom); // releases mDarkroom while blocked.
  72. AssertFatal(ok == 0,"Waiting on mCond failed in acquireSemaphore().");
  73. }
  74. mData->count--;
  75. ok = pthread_mutex_unlock(&mData->mDarkroom);
  76. AssertFatal(ok == 0,"Mutex Unlock failed on mDarkroom when leaving acquireSemaphore().");
  77. return true;
  78. }
  79. void Semaphore::release()
  80. {
  81. bool ok;
  82. AssertFatal(mData, "Semaphore::releaseSemaphore: invalid semaphore");
  83. ok = pthread_mutex_lock(&mData->mDarkroom);
  84. AssertFatal(ok == 0,"Mutex Lock failed on mDarkroom in releaseSemaphore().");
  85. mData->count++;
  86. if(mData->count > 0) {
  87. ok = pthread_cond_signal(&mData->mCond);
  88. AssertFatal(ok == 0,"Signaling mCond failed in releaseSemaphore().");
  89. }
  90. ok = pthread_mutex_unlock(&mData->mDarkroom);
  91. AssertFatal(ok == 0,"Mutex Unlock failed on mDarkroom when leaving releaseSemaphore().");
  92. }