eathread_semaphore_apple.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include <EABase/eabase.h>
  5. #include <eathread/eathread_semaphore.h>
  6. #if defined(EA_PLATFORM_APPLE)
  7. #include <mach/task.h>
  8. #include <mach/mach_init.h>
  9. #include <mach/kern_return.h>
  10. #include <semaphore.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include <stdio.h>
  15. #include <limits.h>
  16. EASemaphoreData::EASemaphoreData()
  17. : mSemaphore(), mnCount(0), mnMaxCount(INT_MAX)
  18. {
  19. }
  20. EA::Thread::SemaphoreParameters::SemaphoreParameters(int initialCount, bool bIntraProcess, const char* /*pName*/)
  21. : mInitialCount(initialCount), mMaxCount(INT_MAX), mbIntraProcess(bIntraProcess)
  22. {
  23. }
  24. EA::Thread::Semaphore::Semaphore(const SemaphoreParameters* pSemaphoreParameters, bool bDefaultParameters)
  25. {
  26. if(!pSemaphoreParameters && bDefaultParameters)
  27. {
  28. SemaphoreParameters parameters;
  29. Init(&parameters);
  30. }
  31. else
  32. Init(pSemaphoreParameters);
  33. }
  34. EA::Thread::Semaphore::Semaphore(int initialCount)
  35. {
  36. SemaphoreParameters parameters(initialCount);
  37. Init(&parameters);
  38. }
  39. EA::Thread::Semaphore::~Semaphore()
  40. {
  41. const kern_return_t result = semaphore_destroy(mach_task_self(), mSemaphoreData.mSemaphore); (void)result;
  42. EAT_ASSERT(KERN_SUCCESS == result);
  43. }
  44. bool EA::Thread::Semaphore::Init(const SemaphoreParameters* pSemaphoreParameters)
  45. {
  46. if(pSemaphoreParameters)
  47. {
  48. mSemaphoreData.mnCount = pSemaphoreParameters->mInitialCount;
  49. mSemaphoreData.mnMaxCount = pSemaphoreParameters->mMaxCount;
  50. if(mSemaphoreData.mnCount < 0)
  51. mSemaphoreData.mnCount = 0;
  52. // Todo, Jaap Suter, December 2009, do we care about actually supporting this?
  53. mSemaphoreData.mbIntraProcess = pSemaphoreParameters->mbIntraProcess;
  54. const kern_return_t result = semaphore_create(mach_task_self(), &mSemaphoreData.mSemaphore, SYNC_POLICY_FIFO, static_cast<int>(mSemaphoreData.mnCount)); (void)result;
  55. EAT_ASSERT(KERN_SUCCESS == result);
  56. return true;
  57. }
  58. return false;
  59. }
  60. int EA::Thread::Semaphore::Wait(const ThreadTime& timeoutAbsolute)
  61. {
  62. kern_return_t result = KERN_SUCCESS;
  63. if(timeoutAbsolute == kTimeoutNone)
  64. {
  65. result = semaphore_wait(mSemaphoreData.mSemaphore);
  66. if(result != KERN_SUCCESS)
  67. {
  68. EAT_ASSERT(false); // This is an error condition.
  69. return kResultError;
  70. }
  71. }
  72. else
  73. {
  74. for (;;)
  75. {
  76. ThreadTime timeoutRelative = kTimeoutImmediate;
  77. if (timeoutAbsolute != kTimeoutImmediate)
  78. {
  79. ThreadTime timeCurrent = GetThreadTime();
  80. timeoutRelative = (timeoutAbsolute > timeCurrent) ? (timeoutAbsolute - timeCurrent) : kTimeoutImmediate;
  81. }
  82. mach_timespec_t machTimeoutRelative = { (unsigned int)timeoutRelative.tv_sec, (clock_res_t)timeoutRelative.tv_nsec };
  83. result = semaphore_timedwait(mSemaphoreData.mSemaphore, machTimeoutRelative);
  84. if (result == KERN_SUCCESS)
  85. break;
  86. if (result == KERN_OPERATION_TIMED_OUT)
  87. return kResultTimeout;
  88. // printf("semaphore_timedwait other error: %d\n", result);
  89. }
  90. }
  91. EAT_ASSERT(mSemaphoreData.mnCount > 0);
  92. return (int)mSemaphoreData.mnCount.Decrement(); // AtomicInt32 operation. Note that the value of the semaphore count could change from the returned value by the time the caller reads it. This is fine but the user should understand this.
  93. }
  94. int EA::Thread::Semaphore::Post(int count)
  95. {
  96. // Some systems have a sem_post_multiple which we could take advantage
  97. // of here to atomically post multiple times.
  98. EAT_ASSERT(mSemaphoreData.mnCount >= 0);
  99. int currentCount = mSemaphoreData.mnCount;
  100. // If count would cause an overflow exit early
  101. if ((mSemaphoreData.mnMaxCount - count) < currentCount)
  102. return kResultError;
  103. currentCount += count;
  104. while(count-- > 0)
  105. {
  106. ++mSemaphoreData.mnCount; // AtomicInt32 operation.
  107. if(semaphore_signal(mSemaphoreData.mSemaphore) != KERN_SUCCESS)
  108. {
  109. --mSemaphoreData.mnCount; // AtomicInt32 operation.
  110. EAT_ASSERT(false);
  111. return kResultError;
  112. }
  113. }
  114. // If all count posts occurred...
  115. return currentCount; // It's possible that another thread may have modified this value since we changed it, but that's not important.
  116. }
  117. int EA::Thread::Semaphore::GetCount() const
  118. {
  119. return (int)mSemaphoreData.mnCount;
  120. }
  121. #endif // #if defined(EA_PLATFORM_APPLE)