macCarbMutex.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include <pthread.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include "platform/platform.h"
  26. #include "platform/threads/mutex.h"
  27. #include "platform/threads/thread.h"
  28. // TODO: examine & dump errno if pthread_* funcs fail. ( only in debug build )
  29. class PlatformMutexData
  30. {
  31. public:
  32. pthread_mutex_t mMutex;
  33. bool locked;
  34. U32 lockedByThread;
  35. };
  36. Mutex::Mutex(void)
  37. {
  38. int ok;
  39. mData = new PlatformMutexData;
  40. pthread_mutexattr_t attr;
  41. ok = pthread_mutexattr_init(&attr);
  42. ok = pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
  43. ok = pthread_mutex_init(&(mData->mMutex),&attr);
  44. AssertFatal(ok == 0, "Mutex() failed: pthread_mutex_init() failed.");
  45. mData->locked = false;
  46. mData->lockedByThread = 0;
  47. }
  48. Mutex::~Mutex()
  49. {
  50. int ok;
  51. ok = pthread_mutex_destroy( &(mData->mMutex) );
  52. AssertFatal(ok == 0, "~Mutex() failed: pthread_mutex_destroy() failed.");
  53. delete mData;
  54. }
  55. bool Mutex::lock( bool block)
  56. {
  57. int ok;
  58. if(block)
  59. {
  60. ok = pthread_mutex_lock( &(mData->mMutex) );
  61. AssertFatal( ok != EINVAL, "Mutex::lockMutex() failed: invalid mutex.");
  62. AssertFatal( ok != EDEADLK, "Mutex::lockMutex() failed: system detected a deadlock!");
  63. AssertFatal( ok == 0, "Mutex::lockMutex() failed: pthread_mutex_lock() failed -- unknown reason.");
  64. }
  65. else {
  66. ok = pthread_mutex_trylock( &(mData->mMutex) );
  67. // returns EBUSY if mutex was locked by another thread,
  68. // returns EINVAL if mutex was not a valid mutex pointer,
  69. // returns 0 if lock succeeded.
  70. AssertFatal( ok != EINVAL, "Mutex::lockMutex(non blocking) failed: invalid mutex.");
  71. if( ok != 0 )
  72. return false;
  73. AssertFatal( ok == 0, "Mutex::lockMutex(non blocking) failed: pthread_mutex_trylock() failed -- unknown reason.");
  74. }
  75. mData->locked = true;
  76. mData->lockedByThread = ThreadManager::getCurrentThreadId();
  77. return true;
  78. }
  79. void Mutex::unlock()
  80. {
  81. int ok;
  82. ok = pthread_mutex_unlock( &(mData->mMutex) );
  83. AssertFatal( ok == 0, "Mutex::unlockMutex() failed: pthread_mutex_unlock() failed.");
  84. mData->locked = false;
  85. mData->lockedByThread = 0;
  86. }