[PATCH 4/6] Support for threaded compession
Eugene Velesevich
evel at ispras.ru
Fri May 31 13:50:54 PDT 2013
This patch adds interfaces for platform-specific thread functions
---
common/os_thread.hpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 92 insertions(+)
diff --git a/common/os_thread.hpp b/common/os_thread.hpp
index f3ae210..c35febe 100644
--- a/common/os_thread.hpp
+++ b/common/os_thread.hpp
@@ -37,6 +37,8 @@
#include <windows.h>
#else
#include <pthread.h>
+#include <signal.h>
+#include <semaphore.h>
#endif
@@ -422,6 +424,96 @@ namespace os {
#endif
};
+
+#if defined _WIN32
+
+typedef HANDLE Thread;
+typedef HANDLE Mutex;
+
+#define THREAD_ROUTINE WINAPI
+
+static inline Thread ThreadCreate(void *(WINAPI * routine)(void *),
+ void *param) {
+ DWORD id;
+ return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) routine,
+ param, 0, &id);
+}
+
+static inline int ThreadWait(Thread thread) {
+ if (WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0) {
+ return 0;
+ }
+ return -1;
+}
+
+static inline int ThreadDestroy(Thread thread) {
+ if (CloseHandle(thread)) {
+ return 0;
+ }
+ return -1;
+}
+
+#define MutexInit(mutex) \
+ (void)(mutex = CreateSemaphore(NULL, 1, 1, NULL))
+
+#define MutexDestroy(mutex) \
+ CloseHandle(mutex);
+
+#define MutexLock(mutex) \
+ (void) WaitForSingleObject(mutex, INFINITE)
+
+#define MutexUnlock(mutex) \
+ (void) ReleaseSemaphore(mutex, 1, NULL);
+
+#else /* !_WIN32 */
+
+typedef pthread_t Thread;
+typedef sem_t Mutex;
+
+#define THREAD_ROUTINE /* */
+
+static inline Thread ThreadCreate(void *(* routine)( void *),
+ void *param) {
+ Thread thread;
+ sigset_t saved_set, new_set;
+ int ret;
+
+ /*
+ * Block signals for new thread when spawning threads.
+ */
+
+ sigfillset(&new_set);
+ pthread_sigmask(SIG_SETMASK, &new_set, &saved_set);
+ ret = pthread_create( &thread, NULL, routine, param );
+ pthread_sigmask(SIG_SETMASK, &saved_set, NULL);
+ if (ret) {
+ return 0;
+ }
+ return thread;
+}
+
+static inline int ThreadWait(Thread thread) {
+ return pthread_join(thread, NULL);
+}
+
+static inline int ThreadDestroy(Thread thread) {
+ return pthread_detach(thread);
+}
+
+#define MutexInit(mutex) \
+ (void) sem_init(&(mutex), 0, 1)
+
+#define MutexDestroy(mutex) \
+ (void) sem_destroy(&(mutex))
+
+#define MutexLock(mutex) \
+ (void) sem_wait(&(mutex))
+
+#define MutexUnlock(mutex) \
+ (void) sem_post(&(mutex))
+
+#endif
+
} /* namespace os */
#endif /* _OS_THREAD_HPP_ */
--
1.7.9.5
More information about the apitrace
mailing list