osxMutex.mm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #import "memory/safeDelete.h"
  26. #import "platform/threads/mutex.h"
  27. #import "platform/threads/thread.h"
  28. #import "platform/platform.h"
  29. #import "platformOSX.h"
  30. //-----------------------------------------------------------------------------
  31. struct PlatformMutexData
  32. {
  33. pthread_mutex_t mMutex;
  34. bool locked;
  35. U32 lockedByThread;
  36. };
  37. //-----------------------------------------------------------------------------
  38. Mutex::Mutex()
  39. {
  40. bool ok;
  41. // Create the mutex data.
  42. mData = new PlatformMutexData;
  43. // Initialize the system mutex.
  44. pthread_mutexattr_t attr;
  45. ok = pthread_mutexattr_init(&attr);
  46. ok = pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
  47. ok = pthread_mutex_init(&(mData->mMutex),&attr);
  48. // Sanity!
  49. AssertFatal(ok == 0, "Mutex() failed: pthread_mutex_init() failed.");
  50. // Set the initial mutex state.
  51. mData->locked = false;
  52. mData->lockedByThread = 0;
  53. }
  54. //-----------------------------------------------------------------------------
  55. Mutex::~Mutex()
  56. {
  57. // Destroy the system mutex.
  58. const bool ok = pthread_mutex_destroy( &(mData->mMutex) );
  59. // Sanity!
  60. AssertFatal(ok == 0, "~Mutex() failed: pthread_mutex_destroy() failed.");
  61. // Delete the mutex data.
  62. SAFE_DELETE( mData );
  63. }
  64. //-----------------------------------------------------------------------------
  65. bool Mutex::lock( bool block )
  66. {
  67. // Is this a blocking lock?
  68. if( block )
  69. {
  70. // Yes, so block until mutex can be locked.
  71. const bool ok = pthread_mutex_lock( &(mData->mMutex) );
  72. // Sanity!
  73. AssertFatal( ok != EINVAL, "Mutex::lockMutex() failed: invalid mutex.");
  74. AssertFatal( ok != EDEADLK, "Mutex::lockMutex() failed: system detected a deadlock!");
  75. AssertFatal( ok == 0, "Mutex::lockMutex() failed: pthread_mutex_lock() failed -- unknown reason.");
  76. }
  77. else
  78. {
  79. // No, so attempt to lock the thread without blocking.
  80. const bool ok = pthread_mutex_trylock( &(mData->mMutex) );
  81. // returns EBUSY if mutex was locked by another thread,
  82. // returns EINVAL if mutex was not a valid mutex pointer,
  83. // returns 0 if lock succeeded.
  84. // Sanity!
  85. AssertFatal( ok != EINVAL, "Mutex::lockMutex(non blocking) failed: invalid mutex.");
  86. // Finish if we couldn't lock the mutex.
  87. if( ok != 0 )
  88. return false;
  89. AssertFatal( ok == 0, "Mutex::lockMutex(non blocking) failed: pthread_mutex_trylock() failed -- unknown reason.");
  90. }
  91. // Flag as locked by the current thread.
  92. mData->locked = true;
  93. mData->lockedByThread = ThreadManager::getCurrentThreadId();
  94. return true;
  95. }
  96. //-----------------------------------------------------------------------------
  97. void Mutex::unlock()
  98. {
  99. // Unlock the thread.
  100. const bool ok = pthread_mutex_unlock( &(mData->mMutex) );
  101. // Sanity!
  102. AssertFatal( ok == 0, "Mutex::unlockMutex() failed: pthread_mutex_unlock() failed.");
  103. // Flag as unlocked.
  104. mData->locked = false;
  105. mData->lockedByThread = 0;
  106. }