osxMutex.mm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. ThreadIdent 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. bool ok;
  59. ok = pthread_mutex_destroy( &(mData->mMutex) );
  60. // Sanity!
  61. AssertFatal(ok == 0, "~Mutex() failed: pthread_mutex_destroy() failed.");
  62. // Delete the mutex data.
  63. SAFE_DELETE( mData );
  64. }
  65. //-----------------------------------------------------------------------------
  66. bool Mutex::lock( bool block )
  67. {
  68. // Is this a blocking lock?
  69. if( block )
  70. {
  71. // Yes, so block until mutex can be locked.
  72. int ok;
  73. ok = pthread_mutex_lock( &(mData->mMutex) );
  74. // Sanity!
  75. AssertFatal( ok != EINVAL, "Mutex::lockMutex() failed: invalid mutex.");
  76. AssertFatal( ok != EDEADLK, "Mutex::lockMutex() failed: system detected a deadlock!");
  77. AssertFatal( ok == 0, "Mutex::lockMutex() failed: pthread_mutex_lock() failed -- unknown reason.");
  78. }
  79. else
  80. {
  81. // No, so attempt to lock the thread without blocking.
  82. const int ok = pthread_mutex_trylock( &(mData->mMutex) );
  83. // returns EBUSY if mutex was locked by another thread,
  84. // returns EINVAL if mutex was not a valid mutex pointer,
  85. // returns 0 if lock succeeded.
  86. // Sanity!
  87. AssertFatal( ok != EINVAL, "Mutex::lockMutex(non blocking) failed: invalid mutex.");
  88. // Finish if we couldn't lock the mutex.
  89. if( ok != 0 )
  90. return false;
  91. AssertFatal( ok == 0, "Mutex::lockMutex(non blocking) failed: pthread_mutex_trylock() failed -- unknown reason.");
  92. }
  93. // Flag as locked by the current thread.
  94. mData->locked = true;
  95. mData->lockedByThread = ThreadManager::getCurrentThreadId();
  96. return true;
  97. }
  98. //-----------------------------------------------------------------------------
  99. void Mutex::unlock()
  100. {
  101. // Unlock the thread.
  102. bool ok;
  103. ok = pthread_mutex_unlock( &(mData->mMutex) );
  104. // Sanity!
  105. AssertFatal( ok == 0, "Mutex::unlockMutex() failed: pthread_mutex_unlock() failed.");
  106. // Flag as unlocked.
  107. mData->locked = false;
  108. mData->lockedByThread = 0;
  109. }