Mutex.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Mutex.h"
  25. #ifdef WIN32
  26. #include <windows.h>
  27. #else
  28. #include <pthread.h>
  29. #endif
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. #ifdef WIN32
  34. Mutex::Mutex() :
  35. handle_(new CRITICAL_SECTION)
  36. {
  37. InitializeCriticalSection((CRITICAL_SECTION*)handle_);
  38. }
  39. Mutex::~Mutex()
  40. {
  41. CRITICAL_SECTION* cs = (CRITICAL_SECTION*)handle_;
  42. DeleteCriticalSection(cs);
  43. delete cs;
  44. handle_ = 0;
  45. }
  46. void Mutex::Acquire()
  47. {
  48. EnterCriticalSection((CRITICAL_SECTION*)handle_);
  49. }
  50. void Mutex::Release()
  51. {
  52. LeaveCriticalSection((CRITICAL_SECTION*)handle_);
  53. }
  54. #else
  55. Mutex::Mutex() :
  56. handle_(new pthread_mutex_t)
  57. {
  58. pthread_mutex_t* mutex = (pthread_mutex_t*)handle_;
  59. pthread_mutexattr_t attr;
  60. pthread_mutexattr_init(&attr);
  61. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  62. pthread_mutex_init(mutex, &attr);
  63. }
  64. Mutex::~Mutex()
  65. {
  66. pthread_mutex_t* mutex = (pthread_mutex_t*)handle_;
  67. pthread_mutex_destroy(mutex);
  68. delete mutex;
  69. handle_ = 0;
  70. }
  71. void Mutex::Acquire()
  72. {
  73. pthread_mutex_lock((pthread_mutex_t*)handle_);
  74. }
  75. void Mutex::Release()
  76. {
  77. pthread_mutex_unlock((pthread_mutex_t*)handle_);
  78. }
  79. #endif
  80. MutexLock::MutexLock(Mutex& mutex) :
  81. mutex_(mutex)
  82. {
  83. mutex_.Acquire();
  84. }
  85. MutexLock::~MutexLock()
  86. {
  87. mutex_.Release();
  88. }
  89. }