critsec.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "precomp_wasabi_bfc.h"
  2. #include "critsec.h"
  3. // uncomment this if needed
  4. //#define CS_DEBUG
  5. CriticalSection::CriticalSection() {
  6. #ifdef WIN32
  7. InitializeCriticalSection(&cs);
  8. #elif defined(__APPLE__)
  9. MPCreateCriticalRegion(&cr);
  10. #elif defined(LINUX)
  11. pthread_mutex_t recursive = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  12. cs.mutex = recursive;
  13. #endif
  14. #ifdef ASSERTS_ENABLED
  15. #ifdef CS_DEBUG
  16. within = 0;
  17. #endif
  18. #endif
  19. }
  20. CriticalSection::~CriticalSection() {
  21. #ifdef CS_DEBUG
  22. #ifdef ASSERTS_ENABLED
  23. ASSERT(!within);
  24. #endif
  25. #endif
  26. #ifdef WIN32
  27. DeleteCriticalSection(&cs);
  28. #elif defined(__APPLE__)
  29. MPDeleteCriticalRegion(cr);
  30. #elif defined(LINUX)
  31. pthread_mutex_destroy(&cs.mutex);
  32. #endif
  33. }
  34. void CriticalSection::enter() {
  35. #ifdef WIN32
  36. EnterCriticalSection(&cs);
  37. #elif defined(__APPLE__)
  38. MPEnterCriticalRegion(cr, kDurationForever);
  39. #elif defined(LINUX)
  40. pthread_mutex_lock(&cs.mutex);
  41. #endif
  42. #ifdef CS_DEBUG
  43. #ifdef ASSERTS_ENABLED
  44. ASSERT(!within);
  45. within = 1;
  46. #endif
  47. #endif
  48. }
  49. void CriticalSection::leave() {
  50. #if defined(CS_DEBUG) && defined(ASSERTS_ENABLED)
  51. ASSERT(within);
  52. within = 0;
  53. #endif
  54. #ifdef WIN32
  55. LeaveCriticalSection(&cs);
  56. #elif defined(__APPLE__)
  57. MPExitCriticalRegion(cr);
  58. #elif defined(LINUX)
  59. pthread_mutex_unlock(&cs.mutex);
  60. #endif
  61. }
  62. void CriticalSection::inout() {
  63. enter();
  64. leave();
  65. }