Mutex.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  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. #ifdef WIN32
  32. Mutex::Mutex() :
  33. handle_(new CRITICAL_SECTION)
  34. {
  35. InitializeCriticalSection((CRITICAL_SECTION*)handle_);
  36. }
  37. Mutex::~Mutex()
  38. {
  39. CRITICAL_SECTION* cs = (CRITICAL_SECTION*)handle_;
  40. DeleteCriticalSection(cs);
  41. delete cs;
  42. handle_ = 0;
  43. }
  44. void Mutex::Acquire()
  45. {
  46. EnterCriticalSection((CRITICAL_SECTION*)handle_);
  47. }
  48. void Mutex::Release()
  49. {
  50. LeaveCriticalSection((CRITICAL_SECTION*)handle_);
  51. }
  52. #else
  53. Mutex::Mutex() :
  54. handle_(new pthread_mutex_t)
  55. {
  56. pthread_mutex_t* mutex = (pthread_mutex_t*)handle_;
  57. pthread_mutexattr_t attr;
  58. pthread_mutexattr_init(&attr);
  59. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  60. pthread_mutex_init(mutex, &attr);
  61. }
  62. Mutex::~Mutex()
  63. {
  64. pthread_mutex_t* mutex = (pthread_mutex_t*)handle_;
  65. pthread_mutex_destroy(mutex);
  66. delete mutex;
  67. handle_ = 0;
  68. }
  69. void Mutex::Acquire()
  70. {
  71. pthread_mutex_lock((pthread_mutex_t*)handle_);
  72. }
  73. void Mutex::Release()
  74. {
  75. pthread_mutex_unlock((pthread_mutex_t*)handle_);
  76. }
  77. #endif
  78. MutexLock::MutexLock(Mutex& mutex) :
  79. mutex_(mutex)
  80. {
  81. mutex_.Acquire();
  82. }
  83. MutexLock::~MutexLock()
  84. {
  85. mutex_.Release();
  86. }