include/mgba-util/platform/posix/threading.h (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef POSIX_THREADING_H
7#define POSIX_THREADING_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <pthread.h>
14#include <sys/time.h>
15#ifdef HAVE_PTHREAD_NP_H
16#include <pthread_np.h>
17#elif defined(__HAIKU__)
18#include <OS.h>
19#endif
20
21#define THREAD_ENTRY void*
22typedef THREAD_ENTRY (*ThreadEntry)(void*);
23
24typedef pthread_t Thread;
25typedef pthread_mutex_t Mutex;
26typedef pthread_cond_t Condition;
27typedef pthread_key_t ThreadLocal;
28
29static inline int MutexInit(Mutex* mutex) {
30 return pthread_mutex_init(mutex, 0);
31}
32
33static inline int MutexDeinit(Mutex* mutex) {
34 return pthread_mutex_destroy(mutex);
35}
36
37static inline int MutexLock(Mutex* mutex) {
38 return pthread_mutex_lock(mutex);
39}
40
41static inline int MutexTryLock(Mutex* mutex) {
42 return pthread_mutex_trylock(mutex);
43}
44
45static inline int MutexUnlock(Mutex* mutex) {
46 return pthread_mutex_unlock(mutex);
47}
48
49static inline int ConditionInit(Condition* cond) {
50 return pthread_cond_init(cond, 0);
51}
52
53static inline int ConditionDeinit(Condition* cond) {
54 return pthread_cond_destroy(cond);
55}
56
57static inline int ConditionWait(Condition* cond, Mutex* mutex) {
58 return pthread_cond_wait(cond, mutex);
59}
60
61static inline int ConditionWaitTimed(Condition* cond, Mutex* mutex, int32_t timeoutMs) {
62 struct timespec ts;
63 struct timeval tv;
64
65 gettimeofday(&tv, 0);
66 ts.tv_sec = tv.tv_sec;
67 ts.tv_nsec = (tv.tv_usec + timeoutMs * 1000L) * 1000L;
68 if (ts.tv_nsec >= 1000000000L) {
69 ts.tv_nsec -= 1000000000L;
70 ++ts.tv_sec;
71 }
72
73 return pthread_cond_timedwait(cond, mutex, &ts);
74}
75
76static inline int ConditionWake(Condition* cond) {
77 return pthread_cond_broadcast(cond);
78}
79
80static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) {
81 return pthread_create(thread, 0, entry, context);
82}
83
84static inline int ThreadJoin(Thread* thread) {
85 return pthread_join(*thread, 0);
86}
87
88static inline int ThreadSetName(const char* name) {
89#if defined(__APPLE__) && defined(HAVE_PTHREAD_SETNAME_NP)
90 return pthread_setname_np(name);
91#elif defined(HAVE_PTHREAD_SET_NAME_NP)
92 pthread_set_name_np(pthread_self(), name);
93 return 0;
94#elif defined(__HAIKU__)
95 rename_thread(find_thread(NULL), name);
96 return 0;
97#elif defined(HAVE_PTHREAD_SETNAME_NP)
98 return pthread_setname_np(pthread_self(), name);
99#else
100 UNUSED(name);
101 return 0;
102#endif
103}
104
105static inline void ThreadLocalInitKey(ThreadLocal* key) {
106 pthread_key_create(key, 0);
107}
108
109static inline void ThreadLocalSetKey(ThreadLocal key, void* value) {
110 pthread_setspecific(key, value);
111}
112
113static inline void* ThreadLocalGetValue(ThreadLocal key) {
114 return pthread_getspecific(key);
115}
116
117CXX_GUARD_END
118
119#endif