Mutex.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Mutex.h"
  24. #ifdef _WIN32
  25. #include <windows.h>
  26. #else
  27. #include <pthread.h>
  28. #endif
  29. #include "../DebugNew.h"
  30. namespace Urho3D
  31. {
  32. #ifdef _WIN32
  33. Mutex::Mutex() :
  34. handle_(new CRITICAL_SECTION)
  35. {
  36. InitializeCriticalSection((CRITICAL_SECTION*)handle_);
  37. }
  38. Mutex::~Mutex()
  39. {
  40. CRITICAL_SECTION* cs = (CRITICAL_SECTION*)handle_;
  41. DeleteCriticalSection(cs);
  42. delete cs;
  43. handle_ = nullptr;
  44. }
  45. void Mutex::Acquire()
  46. {
  47. EnterCriticalSection((CRITICAL_SECTION*)handle_);
  48. }
  49. bool Mutex::TryAcquire()
  50. {
  51. return TryEnterCriticalSection((CRITICAL_SECTION*)handle_) != FALSE;
  52. }
  53. void Mutex::Release()
  54. {
  55. LeaveCriticalSection((CRITICAL_SECTION*)handle_);
  56. }
  57. #else
  58. Mutex::Mutex() :
  59. handle_(new pthread_mutex_t)
  60. {
  61. auto* mutex = (pthread_mutex_t*)handle_;
  62. pthread_mutexattr_t attr;
  63. pthread_mutexattr_init(&attr);
  64. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  65. pthread_mutex_init(mutex, &attr);
  66. }
  67. Mutex::~Mutex()
  68. {
  69. auto* mutex = (pthread_mutex_t*)handle_;
  70. pthread_mutex_destroy(mutex);
  71. delete mutex;
  72. handle_ = 0;
  73. }
  74. void Mutex::Acquire()
  75. {
  76. pthread_mutex_lock((pthread_mutex_t*)handle_);
  77. }
  78. bool Mutex::TryAcquire()
  79. {
  80. return pthread_mutex_trylock((pthread_mutex_t*)handle_) == 0;
  81. }
  82. void Mutex::Release()
  83. {
  84. pthread_mutex_unlock((pthread_mutex_t*)handle_);
  85. }
  86. #endif
  87. MutexLock::MutexLock(Mutex& mutex) :
  88. mutex_(mutex)
  89. {
  90. mutex_.Acquire();
  91. }
  92. MutexLock::~MutexLock()
  93. {
  94. mutex_.Release();
  95. }
  96. }