vhacdMutex.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*!
  2. **
  3. ** Copyright (c) 2009 by John W. Ratcliff mailto:jratcliffscarab@gmail.com
  4. **
  5. ** Portions of this source has been released with the PhysXViewer application, as well as
  6. ** Rocket, CreateDynamics, ODF, and as a number of sample code snippets.
  7. **
  8. ** If you find this code useful or you are feeling particularily generous I would
  9. ** ask that you please go to http://www.amillionpixels.us and make a donation
  10. ** to Troy DeMolay.
  11. **
  12. ** DeMolay is a youth group for young men between the ages of 12 and 21.
  13. ** It teaches strong moral principles, as well as leadership skills and
  14. ** public speaking. The donations page uses the 'pay for pixels' paradigm
  15. ** where, in this case, a pixel is only a single penny. Donations can be
  16. ** made for as small as $4 or as high as a $100 block. Each person who donates
  17. ** will get a link to their own site as well as acknowledgement on the
  18. ** donations blog located here http://www.amillionpixels.blogspot.com/
  19. **
  20. ** If you wish to contact me you can use the following methods:
  21. **
  22. ** Skype ID: jratcliff63367
  23. ** Yahoo: jratcliff63367
  24. ** AOL: jratcliff1961
  25. ** email: jratcliffscarab@gmail.com
  26. **
  27. **
  28. ** The MIT license:
  29. **
  30. ** Permission is hereby granted, free of charge, to any person obtaining a copy
  31. ** of this software and associated documentation files (the "Software"), to deal
  32. ** in the Software without restriction, including without limitation the rights
  33. ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  34. ** copies of the Software, and to permit persons to whom the Software is furnished
  35. ** to do so, subject to the following conditions:
  36. **
  37. ** The above copyright notice and this permission notice shall be included in all
  38. ** copies or substantial portions of the Software.
  39. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  40. ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  41. ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  42. ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  43. ** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  44. ** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  45. */
  46. #pragma once
  47. #ifndef VHACD_MUTEX_H
  48. #define VHACD_MUTEX_H
  49. #if defined(WIN32)
  50. #ifndef _WIN32_WINNT
  51. #define _WIN32_WINNT 0x400
  52. #endif
  53. #include <windows.h>
  54. #pragma comment(lib, "winmm.lib")
  55. #endif
  56. #if defined(__linux__)
  57. //#include <sys/time.h>
  58. #include <errno.h>
  59. #include <time.h>
  60. #include <unistd.h>
  61. #define __stdcall
  62. #endif
  63. #if defined(__APPLE__) || defined(__linux__)
  64. #include <pthread.h>
  65. #endif
  66. #if defined(__APPLE__) || !defined(__GLIBC__)
  67. #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
  68. #endif
  69. #define VHACD_DEBUG
  70. //#define VHACD_NDEBUG
  71. #ifdef VHACD_NDEBUG
  72. #define VHACD_VERIFY(x) (x)
  73. #else
  74. #define VHACD_VERIFY(x) assert((x))
  75. #endif
  76. namespace VHACD {
  77. class Mutex {
  78. public:
  79. Mutex(void)
  80. {
  81. #if defined(WIN32) || defined(_XBOX)
  82. InitializeCriticalSection(&m_mutex);
  83. #elif defined(__APPLE__) || defined(__linux__)
  84. pthread_mutexattr_t mutexAttr; // Mutex Attribute
  85. VHACD_VERIFY(pthread_mutexattr_init(&mutexAttr) == 0);
  86. VHACD_VERIFY(pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP) == 0);
  87. VHACD_VERIFY(pthread_mutex_init(&m_mutex, &mutexAttr) == 0);
  88. VHACD_VERIFY(pthread_mutexattr_destroy(&mutexAttr) == 0);
  89. #endif
  90. }
  91. ~Mutex(void)
  92. {
  93. #if defined(WIN32) || defined(_XBOX)
  94. DeleteCriticalSection(&m_mutex);
  95. #elif defined(__APPLE__) || defined(__linux__)
  96. VHACD_VERIFY(pthread_mutex_destroy(&m_mutex) == 0);
  97. #endif
  98. }
  99. void Lock(void)
  100. {
  101. #if defined(WIN32) || defined(_XBOX)
  102. EnterCriticalSection(&m_mutex);
  103. #elif defined(__APPLE__) || defined(__linux__)
  104. VHACD_VERIFY(pthread_mutex_lock(&m_mutex) == 0);
  105. #endif
  106. }
  107. bool TryLock(void)
  108. {
  109. #if defined(WIN32) || defined(_XBOX)
  110. bool bRet = false;
  111. //assert(("TryEnterCriticalSection seems to not work on XP???", 0));
  112. bRet = TryEnterCriticalSection(&m_mutex) ? true : false;
  113. return bRet;
  114. #elif defined(__APPLE__) || defined(__linux__)
  115. int32_t result = pthread_mutex_trylock(&m_mutex);
  116. return (result == 0);
  117. #endif
  118. }
  119. void Unlock(void)
  120. {
  121. #if defined(WIN32) || defined(_XBOX)
  122. LeaveCriticalSection(&m_mutex);
  123. #elif defined(__APPLE__) || defined(__linux__)
  124. VHACD_VERIFY(pthread_mutex_unlock(&m_mutex) == 0);
  125. #endif
  126. }
  127. private:
  128. #if defined(WIN32) || defined(_XBOX)
  129. CRITICAL_SECTION m_mutex;
  130. #elif defined(__APPLE__) || defined(__linux__)
  131. pthread_mutex_t m_mutex;
  132. #endif
  133. };
  134. }
  135. #endif // VHACD_MUTEX_H