1
0

AudioCriticalSection.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * AudioCriticalSection.cpp
  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. #include "stdafx.h"
  10. #include "AudioCriticalSection.h"
  11. #if defined(MODPLUG_TRACKER)
  12. #include "../misc/mptMutex.h"
  13. #endif
  14. OPENMPT_NAMESPACE_BEGIN
  15. #if defined(MODPLUG_TRACKER)
  16. #if MPT_COMPILER_MSVC
  17. _Acquires_lock_(m_refGlobalMutex.mutex)
  18. #endif // MPT_COMPILER_MSVC
  19. CriticalSection::CriticalSection()
  20. : m_refGlobalMutex(Tracker::GetGlobalMutexRef())
  21. , inSection(false)
  22. {
  23. Enter();
  24. }
  25. CriticalSection::CriticalSection(CriticalSection &&other) noexcept
  26. : m_refGlobalMutex(other.m_refGlobalMutex)
  27. , inSection(other.inSection)
  28. {
  29. other.inSection = false;
  30. }
  31. CriticalSection::CriticalSection(InitialState state)
  32. : m_refGlobalMutex(Tracker::GetGlobalMutexRef())
  33. , inSection(false)
  34. {
  35. if(state == InitialState::Locked)
  36. {
  37. Enter();
  38. }
  39. }
  40. #if MPT_COMPILER_MSVC
  41. _Acquires_lock_(m_refGlobalMutex.mutex)
  42. #endif // MPT_COMPILER_MSVC
  43. void CriticalSection::Enter()
  44. {
  45. if(!inSection)
  46. {
  47. inSection = true;
  48. m_refGlobalMutex.lock();
  49. }
  50. }
  51. #if MPT_COMPILER_MSVC
  52. _Requires_lock_held_(m_refGlobalMutex.mutex) _Releases_lock_(m_refGlobalMutex.mutex)
  53. #endif // MPT_COMPILER_MSVC
  54. void CriticalSection::Leave()
  55. {
  56. if(inSection)
  57. {
  58. inSection = false;
  59. m_refGlobalMutex.unlock();
  60. }
  61. }
  62. CriticalSection::~CriticalSection()
  63. {
  64. Leave();
  65. }
  66. #else
  67. MPT_MSVC_WORKAROUND_LNK4221(AudioCriticalSection)
  68. #endif
  69. OPENMPT_NAMESPACE_END