AudioCriticalSection.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * AudioCriticalSection.h
  3. * ---------
  4. * Purpose: Implementation of OpenMPT's critical section for access to CSoundFile.
  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. #if defined(MODPLUG_TRACKER)
  12. #include "../misc/mptMutex.h"
  13. #endif
  14. OPENMPT_NAMESPACE_BEGIN
  15. #if defined(MODPLUG_TRACKER)
  16. namespace mpt {
  17. class recursive_mutex_with_lock_count;
  18. } // namespace mpt
  19. namespace Tracker { // implemented in mptrack/Mptrack.cpp
  20. mpt::recursive_mutex_with_lock_count & GetGlobalMutexRef();
  21. } // namespace Tracker
  22. // Critical section handling done in (safe) RAII style.
  23. // Create a CriticalSection object whenever you need exclusive access to CSoundFile.
  24. // One object = one lock / critical section.
  25. // The critical section is automatically left when the object is destroyed, but
  26. // Enter() and Leave() can also be called manually if needed.
  27. class CriticalSection
  28. {
  29. private:
  30. mpt::recursive_mutex_with_lock_count & m_refGlobalMutex;
  31. protected:
  32. bool inSection;
  33. public:
  34. enum class InitialState
  35. {
  36. Locked = 0,
  37. Unlocked = 1,
  38. };
  39. public:
  40. #if MPT_COMPILER_MSVC
  41. _Acquires_lock_(m_refGlobalMutex.mutex)
  42. #endif // MPT_COMPILER_MSVC
  43. CriticalSection();
  44. CriticalSection(CriticalSection &&other) noexcept;
  45. explicit CriticalSection(InitialState state);
  46. #if MPT_COMPILER_MSVC
  47. _Acquires_lock_(m_refGlobalMutex.mutex)
  48. #endif // MPT_COMPILER_MSVC
  49. void Enter();
  50. #if MPT_COMPILER_MSVC
  51. _Requires_lock_held_(m_refGlobalMutex.mutex) _Releases_lock_(m_refGlobalMutex.mutex)
  52. #endif // MPT_COMPILER_MSVC
  53. void Leave();
  54. ~CriticalSection();
  55. };
  56. #else // !MODPLUG_TRACKER
  57. class CriticalSection
  58. {
  59. public:
  60. enum class InitialState
  61. {
  62. Locked = 0,
  63. Unlocked = 1,
  64. };
  65. public:
  66. CriticalSection() {}
  67. CriticalSection(CriticalSection &&) noexcept {}
  68. explicit CriticalSection(InitialState) {}
  69. void Enter() {}
  70. void Leave() {}
  71. ~CriticalSection() {}
  72. };
  73. #endif // MODPLUG_TRACKER
  74. OPENMPT_NAMESPACE_END