summaryrefslogtreecommitdiff
path: root/Runtime/Threads/Posix/PlatformSemaphore.h
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-08-14 22:50:43 +0800
committerchai <chaifix@163.com>2019-08-14 22:50:43 +0800
commit15740faf9fe9fe4be08965098bbf2947e096aeeb (patch)
treea730ec236656cc8cab5b13f088adfaed6bb218fb /Runtime/Threads/Posix/PlatformSemaphore.h
+Unity Runtime codeHEADmaster
Diffstat (limited to 'Runtime/Threads/Posix/PlatformSemaphore.h')
-rw-r--r--Runtime/Threads/Posix/PlatformSemaphore.h69
1 files changed, 69 insertions, 0 deletions
diff --git a/Runtime/Threads/Posix/PlatformSemaphore.h b/Runtime/Threads/Posix/PlatformSemaphore.h
new file mode 100644
index 0000000..bc20a55
--- /dev/null
+++ b/Runtime/Threads/Posix/PlatformSemaphore.h
@@ -0,0 +1,69 @@
+#ifndef __PLATFORMSEMAPHORE_H
+#define __PLATFORMSEMAPHORE_H
+
+#if SUPPORT_THREADS
+
+#ifndef SEMAPHORE_API_PTHREAD
+#define SEMAPHORE_API_PTHREAD (UNITY_LINUX || UNITY_PEPPER || UNITY_ANDROID || UNITY_PS3 || UNITY_BB10 || UNITY_TIZEN)
+#endif
+
+#endif // SUPPORT_THREADS
+
+#if SEMAPHORE_API_PTHREAD
+
+#if UNITY_PEPPER
+# include <errno.h>
+# if defined(__native_client__)
+# include <semaphore.h>
+# else
+# include <sys/semaphore.h>
+# endif
+#else
+# include <semaphore.h>
+# include <errno.h>
+#endif
+
+#include "Runtime/Utilities/Word.h"
+#include "Runtime/Utilities/NonCopyable.h"
+
+class PlatformSemaphore : public NonCopyable
+{
+ friend class Semaphore;
+protected:
+ void Create();
+ void Destroy();
+
+ void WaitForSignal();
+ void Signal();
+
+private:
+ sem_t m_Semaphore;
+};
+
+#define REPORT_SEM_ERROR(action) ErrorStringMsg ("Failed to %s a semaphore (%s)\n", action, strerror (errno))
+
+ inline void PlatformSemaphore::Create() { if (sem_init(&m_Semaphore, 0, 0) == -1) REPORT_SEM_ERROR ("open"); }
+ inline void PlatformSemaphore::Destroy() { if (sem_destroy(&m_Semaphore) == -1) REPORT_SEM_ERROR ("destroy"); }
+#if !UNITY_BB10
+ inline void PlatformSemaphore::WaitForSignal() { if (sem_wait(&m_Semaphore) == -1) REPORT_SEM_ERROR ("wait on"); }
+#else
+ inline void PlatformSemaphore::WaitForSignal() {
+ int ret = 0;
+ while ((ret = sem_wait(&m_Semaphore)) == -1 && errno == EINTR)
+ {
+ continue;
+ }
+
+ if( ret == -1 )
+ REPORT_SEM_ERROR ("wait on");
+ }
+#endif
+ inline void PlatformSemaphore::Signal() {
+ if (sem_post(&m_Semaphore) == -1)
+ REPORT_SEM_ERROR ("post to");
+ }
+
+#undef REPORT_SEM_ERROR
+
+#endif // SEMAPHORE_API_PTHREAD
+#endif // __PLATFORMSEMAPHORE_H