Mutex.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // Copyright (c) 2008-2013 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 "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_ = 0;
  44. }
  45. void Mutex::Acquire()
  46. {
  47. EnterCriticalSection((CRITICAL_SECTION*)handle_);
  48. }
  49. void Mutex::Release()
  50. {
  51. LeaveCriticalSection((CRITICAL_SECTION*)handle_);
  52. }
  53. #else
  54. Mutex::Mutex() :
  55. handle_(new pthread_mutex_t)
  56. {
  57. pthread_mutex_t* mutex = (pthread_mutex_t*)handle_;
  58. pthread_mutexattr_t attr;
  59. pthread_mutexattr_init(&attr);
  60. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  61. pthread_mutex_init(mutex, &attr);
  62. }
  63. Mutex::~Mutex()
  64. {
  65. pthread_mutex_t* mutex = (pthread_mutex_t*)handle_;
  66. pthread_mutex_destroy(mutex);
  67. delete mutex;
  68. handle_ = 0;
  69. }
  70. void Mutex::Acquire()
  71. {
  72. pthread_mutex_lock((pthread_mutex_t*)handle_);
  73. }
  74. void Mutex::Release()
  75. {
  76. pthread_mutex_unlock((pthread_mutex_t*)handle_);
  77. }
  78. #endif
  79. MutexLock::MutexLock(Mutex& mutex) :
  80. mutex_(mutex)
  81. {
  82. mutex_.Acquire();
  83. }
  84. MutexLock::~MutexLock()
  85. {
  86. mutex_.Release();
  87. }
  88. }