mptMutex.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * mptMutex.h
  3. * ----------
  4. * Purpose: Partially implement c++ mutexes as far as openmpt needs them. Can eventually go away when we only support c++11 compilers some time.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include "mpt/mutex/mutex.hpp"
  12. OPENMPT_NAMESPACE_BEGIN
  13. namespace mpt {
  14. class recursive_mutex_with_lock_count {
  15. private:
  16. mpt::recursive_mutex mutex;
  17. #if MPT_COMPILER_MSVC
  18. _Guarded_by_(mutex)
  19. #endif // MPT_COMPILER_MSVC
  20. long lockCount;
  21. public:
  22. recursive_mutex_with_lock_count()
  23. : lockCount(0)
  24. {
  25. return;
  26. }
  27. ~recursive_mutex_with_lock_count()
  28. {
  29. return;
  30. }
  31. #if MPT_COMPILER_MSVC
  32. _Acquires_lock_(mutex)
  33. #endif // MPT_COMPILER_MSVC
  34. void lock()
  35. {
  36. mutex.lock();
  37. lockCount++;
  38. }
  39. #if MPT_COMPILER_MSVC
  40. _Requires_lock_held_(mutex) _Releases_lock_(mutex)
  41. #endif // MPT_COMPILER_MSVC
  42. void unlock()
  43. {
  44. lockCount--;
  45. mutex.unlock();
  46. }
  47. public:
  48. bool IsLockedByCurrentThread() // DEBUGGING only
  49. {
  50. bool islocked = false;
  51. if(mutex.try_lock())
  52. {
  53. islocked = (lockCount > 0);
  54. mutex.unlock();
  55. }
  56. return islocked;
  57. }
  58. };
  59. } // namespace mpt
  60. OPENMPT_NAMESPACE_END