eathread_mutex_unix.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_mutex.h>
  7. #if defined(EA_PLATFORM_UNIX) || EA_POSIX_THREADS_AVAILABLE
  8. #include <errno.h>
  9. #include <string.h>
  10. #ifdef EA_PLATFORM_WINDOWS
  11. EA_DISABLE_ALL_VC_WARNINGS()
  12. #include <Windows.h> // Presumably we are using pthreads-win32.
  13. EA_RESTORE_ALL_VC_WARNINGS()
  14. #ifdef CreateMutex
  15. #undef CreateMutex // Windows #defines CreateMutex to CreateMutexA or CreateMutexW.
  16. #endif
  17. #endif
  18. EAMutexData::EAMutexData()
  19. : mMutex(), mnLockCount(0)
  20. {
  21. #if EAT_ASSERT_ENABLED
  22. mThreadId = EA::Thread::kThreadIdInvalid;
  23. #endif
  24. ::memset(&mMutex, 0, sizeof(mMutex));
  25. }
  26. void EAMutexData::SimulateLock(bool bLock)
  27. {
  28. if(bLock)
  29. {
  30. ++mnLockCount;
  31. #if EAT_ASSERT_ENABLED
  32. mThreadId = EA::Thread::GetThreadId();
  33. #endif
  34. }
  35. else
  36. {
  37. --mnLockCount;
  38. #if EAT_ASSERT_ENABLED
  39. mThreadId = EA::Thread::kThreadIdInvalid;
  40. #endif
  41. }
  42. }
  43. EA::Thread::MutexParameters::MutexParameters(bool bIntraProcess, const char* /*pName*/)
  44. : mbIntraProcess(bIntraProcess)
  45. {
  46. // Empty
  47. }
  48. EA::Thread::Mutex::Mutex(const MutexParameters* pMutexParameters, bool bDefaultParameters)
  49. {
  50. if(!pMutexParameters && bDefaultParameters)
  51. {
  52. MutexParameters parameters;
  53. Init(&parameters);
  54. }
  55. else
  56. Init(pMutexParameters);
  57. }
  58. EA::Thread::Mutex::~Mutex()
  59. {
  60. EAT_ASSERT(mMutexData.mnLockCount == 0);
  61. pthread_mutex_destroy(&mMutexData.mMutex);
  62. }
  63. bool EA::Thread::Mutex::Init(const MutexParameters* pMutexParameters)
  64. {
  65. if(pMutexParameters)
  66. {
  67. mMutexData.mnLockCount = 0;
  68. pthread_mutexattr_t attr;
  69. pthread_mutexattr_init(&attr);
  70. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  71. #if defined(PTHREAD_PROCESS_PRIVATE) // Some pthread implementations don't recognize this.
  72. #if defined(PTHREAD_PROCESS_SHARED)
  73. if (pMutexParameters->mbIntraProcess)
  74. pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
  75. else
  76. pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
  77. #else
  78. pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
  79. #endif
  80. #endif
  81. const int result = pthread_mutex_init(&mMutexData.mMutex, &attr);
  82. pthread_mutexattr_destroy(&attr);
  83. EAT_ASSERT(result != -1);
  84. return (result != -1);
  85. }
  86. return false;
  87. }
  88. int EA::Thread::Mutex::Lock(const ThreadTime& timeoutAbsolute)
  89. {
  90. int result;
  91. EAT_ASSERT(mMutexData.mnLockCount < 100000);
  92. if(timeoutAbsolute == kTimeoutNone)
  93. {
  94. result = pthread_mutex_lock(&mMutexData.mMutex);
  95. if(result != 0)
  96. {
  97. EAT_ASSERT(false);
  98. return kResultError;
  99. }
  100. }
  101. else if(timeoutAbsolute == kTimeoutImmediate)
  102. {
  103. result = pthread_mutex_trylock(&mMutexData.mMutex);
  104. if(result != 0)
  105. {
  106. if(result == EBUSY)
  107. return kResultTimeout;
  108. EAT_ASSERT(false);
  109. return kResultError;
  110. }
  111. }
  112. else
  113. {
  114. #if (defined(EA_PLATFORM_LINUX) || defined(EA_PLATFORM_WINDOWS)) && !defined(EA_PLATFORM_CYGWIN) && !defined(EA_PLATFORM_ANDROID)
  115. const timespec* pTimeSpec = &timeoutAbsolute;
  116. result = pthread_mutex_timedlock(&mMutexData.mMutex, const_cast<timespec*>(pTimeSpec)); // Some pthread implementations use non-const timespec, so cast for them.
  117. if(result != 0)
  118. {
  119. if(result == ETIMEDOUT)
  120. return kResultTimeout;
  121. EAT_ASSERT(false);
  122. return kResultError;
  123. }
  124. #else // OSX, BSD
  125. // Some Posix systems don't have pthread_mutex_timedlock. In these
  126. // cases we fall back to a polling mechanism. However, polling really
  127. // isn't proper because the polling thread might be at a greater
  128. // priority level than the lock-owning thread and thus this code
  129. // might not work as well as desired.
  130. while(((result = pthread_mutex_trylock(&mMutexData.mMutex)) != 0) && (GetThreadTime() < timeoutAbsolute))
  131. ThreadSleep(1);
  132. if(result != 0)
  133. {
  134. if(result == EBUSY)
  135. return kResultTimeout;
  136. EAT_ASSERT(false);
  137. return kResultError;
  138. }
  139. #endif
  140. }
  141. EAT_ASSERT(mMutexData.mThreadId = EA::Thread::GetThreadId()); // Intentionally '=' here and not '=='.
  142. EAT_ASSERT(mMutexData.mnLockCount >= 0);
  143. return ++mMutexData.mnLockCount; // This is safe to do because we have the lock.
  144. }
  145. int EA::Thread::Mutex::Unlock()
  146. {
  147. EAT_ASSERT(mMutexData.mThreadId == EA::Thread::GetThreadId());
  148. EAT_ASSERT(mMutexData.mnLockCount > 0);
  149. const int nReturnValue(--mMutexData.mnLockCount); // This is safe to do because we have the lock.
  150. if(pthread_mutex_unlock(&mMutexData.mMutex) != 0)
  151. {
  152. EAT_ASSERT(false);
  153. return nReturnValue + 1;
  154. }
  155. return nReturnValue;
  156. }
  157. int EA::Thread::Mutex::GetLockCount() const
  158. {
  159. return mMutexData.mnLockCount;
  160. }
  161. bool EA::Thread::Mutex::HasLock() const
  162. {
  163. #if EAT_ASSERT_ENABLED
  164. return (mMutexData.mnLockCount > 0) && (mMutexData.mThreadId == GetThreadId());
  165. #else
  166. return (mMutexData.mnLockCount > 0); // This is the best we can do.
  167. #endif
  168. }
  169. #endif // EA_PLATFORM_XXX