eathread_condition_unix.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include <EABase/eabase.h>
  5. #include <eathread/internal/config.h>
  6. #include <eathread/eathread_condition.h>
  7. #if defined(EA_PLATFORM_UNIX) || EA_POSIX_THREADS_AVAILABLE
  8. #include <pthread.h>
  9. #include <time.h>
  10. #include <errno.h>
  11. #include <string.h>
  12. #ifdef EA_PLATFORM_WINDOWS
  13. EA_DISABLE_ALL_VC_WARNINGS()
  14. #include <Windows.h> // Presumably we are using pthreads-win32.
  15. EA_RESTORE_ALL_VC_WARNINGS()
  16. #endif
  17. EAConditionData::EAConditionData()
  18. {
  19. memset(&mCV, 0, sizeof(mCV));
  20. }
  21. EA::Thread::ConditionParameters::ConditionParameters(bool bIntraProcess, const char* /*pName*/)
  22. : mbIntraProcess(bIntraProcess)
  23. {
  24. // Empty
  25. }
  26. EA::Thread::Condition::Condition(const ConditionParameters* pConditionParameters, bool bDefaultParameters)
  27. {
  28. if(!pConditionParameters && bDefaultParameters)
  29. {
  30. ConditionParameters parameters;
  31. Init(&parameters);
  32. }
  33. else
  34. Init(pConditionParameters);
  35. }
  36. EA::Thread::Condition::~Condition()
  37. {
  38. pthread_cond_destroy(&mConditionData.mCV);
  39. }
  40. bool EA::Thread::Condition::Init(const ConditionParameters* pConditionParameters)
  41. {
  42. if(pConditionParameters)
  43. {
  44. #if defined(EA_PLATFORM_ANDROID) // Some platforms don't provide pthread_condattr_init, and pthread_condattr_t is just an int.
  45. pthread_condattr_t cattr;
  46. memset(&cattr, 0, sizeof(cattr));
  47. const int result = pthread_cond_init(&mConditionData.mCV, &cattr);
  48. return (result == 0);
  49. #else
  50. pthread_condattr_t cattr;
  51. pthread_condattr_init(&cattr);
  52. #if defined(PTHREAD_PROCESS_PRIVATE) // Some pthread implementations don't recognize this. PTHREAD_PROCESS_SHARED bugged on iphone
  53. #if defined(EA_PLATFORM_IPHONE) || defined(EA_PLATFORM_OSX)
  54. EAT_ASSERT( pConditionParameters->mbIntraProcess == true ); // shared conditions bugged on apple hardware
  55. #else
  56. if(pConditionParameters->mbIntraProcess)
  57. pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_PRIVATE);
  58. else
  59. pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
  60. #endif
  61. #endif
  62. const int result = pthread_cond_init(&mConditionData.mCV, &cattr);
  63. pthread_condattr_destroy(&cattr);
  64. return (result == 0);
  65. #endif
  66. }
  67. return false;
  68. }
  69. EA::Thread::Condition::Result EA::Thread::Condition::Wait(Mutex* pMutex, const ThreadTime& timeoutAbsolute)
  70. {
  71. int result;
  72. pthread_mutex_t* pMutex_t;
  73. EAMutexData* pMutexData;
  74. EAT_ASSERT(pMutex);
  75. // We have a small problem here in that if we are using the pMutex argument,
  76. // the pthread_cond_wait call will unlock the mutex via the internal mutex data and
  77. // not without calling the Mutex::Lock function. The result is that the Mutex doesn't
  78. // have its lock count value reduced by one and so other threads will see the lock
  79. // count as being 1 when in fact it should be zero. So we account for that here
  80. // by manually maintaining the lock count, which we can do because we have the lock.
  81. EAT_ASSERT(pMutex->GetLockCount() == 1);
  82. pMutexData = (EAMutexData*)pMutex->GetPlatformData();
  83. pMutexData->SimulateLock(false);
  84. pMutex_t = &pMutexData->mMutex;
  85. if(timeoutAbsolute == kTimeoutNone)
  86. result = pthread_cond_wait(&mConditionData.mCV, pMutex_t);
  87. else
  88. result = pthread_cond_timedwait(&mConditionData.mCV, pMutex_t, &timeoutAbsolute);
  89. pMutexData->SimulateLock(true);
  90. EAT_ASSERT(!pMutex || (pMutex->GetLockCount() == 1));
  91. if(result != 0)
  92. {
  93. if(result == ETIMEDOUT)
  94. return kResultTimeout;
  95. EAT_ASSERT(false);
  96. return kResultError;
  97. }
  98. return kResultOK;
  99. }
  100. bool EA::Thread::Condition::Signal(bool bBroadcast)
  101. {
  102. if(bBroadcast)
  103. return (pthread_cond_broadcast(&mConditionData.mCV) == 0);
  104. return (pthread_cond_signal(&mConditionData.mCV) == 0);
  105. }
  106. #endif // EA_PLATFORM_XXX