summaryrefslogtreecommitdiff
path: root/Runtime/Threads/Semaphore.h
blob: 151f64d305b1617e0ea88b783fd08510cc20b453 (plain)
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
#ifndef __SEMAPHORE_H
#define __SEMAPHORE_H

#if SUPPORT_THREADS

#if UNITY_WIN || UNITY_XENON
#	include "Winapi/PlatformSemaphore.h"
#elif UNITY_LINUX || UNITY_PEPPER || UNITY_ANDROID || UNITY_PS3 || UNITY_BB10 || UNITY_TIZEN
#	include "Posix/PlatformSemaphore.h"
#else
#	include "PlatformSemaphore.h"
#endif

#include "Runtime/Utilities/NonCopyable.h"

class Semaphore : public NonCopyable
{
public:
	Semaphore() { m_Semaphore.Create(); }
	~Semaphore() { m_Semaphore.Destroy(); }
	void Reset() { m_Semaphore.Destroy(); m_Semaphore.Create(); }
	void WaitForSignal() { m_Semaphore.WaitForSignal(); }
	void Signal() { m_Semaphore.Signal(); }

private:
	PlatformSemaphore m_Semaphore;
};

class SuspendableSemaphore : public NonCopyable
{
public:
	explicit SuspendableSemaphore(bool suspended = true) : m_Suspended(suspended), m_SuspendedIndefinitely(false) { }
	bool IsSuspended() const { return m_Suspended; }
	void Reset() { m_Semaphore.Reset(); }
	void WaitForSignal() { if (!m_Suspended) m_Semaphore.WaitForSignal(); };
	void Signal() { if (!m_Suspended) m_Semaphore.Signal(); }
	void Resume(bool reset = true);
	void Suspend(bool indefinitely = false);

private:
	volatile bool m_SuspendedIndefinitely;
	volatile bool m_Suspended;
	Semaphore m_Semaphore;
};

inline void SuspendableSemaphore::Resume(bool reset)
{
	if (reset)
		Reset();

	if (!m_SuspendedIndefinitely)
		m_Suspended = false;
}

inline void SuspendableSemaphore::Suspend(bool indefinitely)
{
	m_Suspended = true;
	if (indefinitely)
		m_SuspendedIndefinitely = indefinitely;

	m_Semaphore.Signal(); // release any waiting thread
}


#endif // SUPPORT_THREADS

#endif // __SEMAPHORE_H