summaryrefslogtreecommitdiff
path: root/Runtime/Threads/Winapi/PlatformSemaphore.h
blob: 0f00f206c23773c7e83f232e87241cf9d4e3d618 (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
#ifndef __PLATFORMSEMAPHORE_H
#define __PLATFORMSEMAPHORE_H

#if SUPPORT_THREADS

#include "Runtime/Utilities/NonCopyable.h"

class PlatformSemaphore : public NonCopyable
{
	friend class Semaphore;
protected:
	void Create();
	void Destroy();
	
	void WaitForSignal();
	void Signal();
	
private:
	HANDLE	m_Semaphore;
};

	inline void PlatformSemaphore::Create()
	{
#if UNITY_WINRT
		m_Semaphore = CreateSemaphoreExW(NULL, 0, 256, NULL, 0, (STANDARD_RIGHTS_REQUIRED | SEMAPHORE_MODIFY_STATE | SYNCHRONIZE));
#else
		m_Semaphore = CreateSemaphoreA( NULL, 0, 256, NULL );
#endif
	}
	inline void PlatformSemaphore::Destroy(){ if( m_Semaphore ) CloseHandle(m_Semaphore); }
	inline void PlatformSemaphore::WaitForSignal()
	{
#if UNITY_WINRT	// ?!-
		WaitForSingleObjectEx(m_Semaphore, INFINITE, FALSE);
#else
		while (1)
		{
			DWORD result = WaitForSingleObjectEx( m_Semaphore, INFINITE, TRUE );
			switch (result)
			{
			case WAIT_OBJECT_0:
				// We got the signal
				return;
			case WAIT_IO_COMPLETION:
				// Allow thread to run IO completion task
				Sleep(1);
				break;
			default:
				Assert(false);
				break;
			}
		}
#endif
	}
	inline void PlatformSemaphore::Signal() { ReleaseSemaphore( m_Semaphore, 1, NULL ); }

#endif // SUPPORT_THREADS

#endif // __PLATFORMSEMAPHORE_H