aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/soundlib/AudioCriticalSection.cpp
blob: d7514e2504a8750231223aef60377c519eaf69d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * AudioCriticalSection.cpp
 * -----------
 * Purpose: Implementation of OpenMPT's critical section for access to CSoundFile.
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */

#include "stdafx.h"

#include "AudioCriticalSection.h"

#if defined(MODPLUG_TRACKER)
#include "../misc/mptMutex.h"
#endif

OPENMPT_NAMESPACE_BEGIN

#if defined(MODPLUG_TRACKER)

#if MPT_COMPILER_MSVC
_Acquires_lock_(m_refGlobalMutex.mutex)
#endif // MPT_COMPILER_MSVC
CriticalSection::CriticalSection()
	: m_refGlobalMutex(Tracker::GetGlobalMutexRef())
	, inSection(false)
{
	Enter();
}

CriticalSection::CriticalSection(CriticalSection &&other) noexcept
	: m_refGlobalMutex(other.m_refGlobalMutex)
	, inSection(other.inSection)
{
	other.inSection = false;
}

CriticalSection::CriticalSection(InitialState state)
	: m_refGlobalMutex(Tracker::GetGlobalMutexRef())
	, inSection(false)
{
	if(state == InitialState::Locked)
	{
		Enter();
	}
}

#if MPT_COMPILER_MSVC
_Acquires_lock_(m_refGlobalMutex.mutex)
#endif // MPT_COMPILER_MSVC
void CriticalSection::Enter()
{
	if(!inSection)
	{
		inSection = true;
		m_refGlobalMutex.lock();
	}
}

#if MPT_COMPILER_MSVC
_Requires_lock_held_(m_refGlobalMutex.mutex) _Releases_lock_(m_refGlobalMutex.mutex)
#endif // MPT_COMPILER_MSVC
void CriticalSection::Leave()
{
	if(inSection)
	{
		inSection = false;
		m_refGlobalMutex.unlock();
	}
}
CriticalSection::~CriticalSection()
{
	Leave();
}

#else

MPT_MSVC_WORKAROUND_LNK4221(AudioCriticalSection)

#endif

OPENMPT_NAMESPACE_END