From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001
From: chai <chaifix@163.com>
Date: Wed, 14 Aug 2019 22:50:43 +0800
Subject: +Unity Runtime code

---
 Runtime/Threads/Winapi/PlatformSemaphore.h | 59 ++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 Runtime/Threads/Winapi/PlatformSemaphore.h

(limited to 'Runtime/Threads/Winapi/PlatformSemaphore.h')

diff --git a/Runtime/Threads/Winapi/PlatformSemaphore.h b/Runtime/Threads/Winapi/PlatformSemaphore.h
new file mode 100644
index 0000000..0f00f20
--- /dev/null
+++ b/Runtime/Threads/Winapi/PlatformSemaphore.h
@@ -0,0 +1,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
-- 
cgit v1.1-26-g67d0