mutex.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "platform/types.h"
  23. #include "platform/platformAssert.h"
  24. #ifndef _PLATFORM_THREADS_MUTEX_H_
  25. #define _PLATFORM_THREADS_MUTEX_H_
  26. // Forward ref used by platform code
  27. struct PlatformMutexData;
  28. class Mutex
  29. {
  30. protected:
  31. PlatformMutexData *mData;
  32. public:
  33. Mutex();
  34. virtual ~Mutex();
  35. virtual bool lock(bool block = true);
  36. virtual void unlock();
  37. // Old API so that we don't have to change a load of code
  38. static void *createMutex()
  39. {
  40. Mutex *mutex = new Mutex;
  41. return (void *)mutex;
  42. }
  43. static void destroyMutex(void *mutex)
  44. {
  45. Mutex *realMutex = reinterpret_cast<Mutex *>(mutex);
  46. delete realMutex;
  47. }
  48. static bool lockMutex(void *mutex, bool block = true)
  49. {
  50. Mutex *realMutex = reinterpret_cast<Mutex *>(mutex);
  51. return realMutex->lock(block);
  52. }
  53. static void unlockMutex(void *mutex)
  54. {
  55. Mutex *realMutex = reinterpret_cast<Mutex *>(mutex);
  56. realMutex->unlock();
  57. }
  58. };
  59. /// Helper for simplifying mutex locking code.
  60. ///
  61. /// This class will automatically unlock a mutex that you've
  62. /// locked through it, saving you from managing a lot of complex
  63. /// exit cases. For instance:
  64. ///
  65. /// @code
  66. /// MutexHandle handle;
  67. /// handle.lock(myMutex);
  68. ///
  69. /// if(error1)
  70. /// return; // Auto-unlocked by handle if we leave here - normally would
  71. /// // leave the mutex locked, causing much pain later.
  72. ///
  73. /// handle.unlock();
  74. /// @endcode
  75. class MutexHandle
  76. {
  77. private:
  78. void *mMutexPtr;
  79. public:
  80. MutexHandle()
  81. : mMutexPtr(NULL)
  82. {
  83. }
  84. ~MutexHandle()
  85. {
  86. if(mMutexPtr)
  87. unlock();
  88. }
  89. bool lock(void *mutex, bool blocking=false)
  90. {
  91. AssertFatal(!mMutexPtr, "MutexHandle::lock - shouldn't be locking things twice!");
  92. bool ret = Mutex::lockMutex(mutex, blocking);
  93. if(ret)
  94. {
  95. // We succeeded, do book-keeping.
  96. mMutexPtr = mutex;
  97. }
  98. return ret;
  99. }
  100. void unlock()
  101. {
  102. if(mMutexPtr)
  103. {
  104. Mutex::unlockMutex(mMutexPtr);
  105. mMutexPtr = NULL;
  106. }
  107. }
  108. };
  109. #endif // _PLATFORM_THREADS_MUTEX_H_