blob: 78a225f8af40b5c80e361db45dbee26aae7d9564 [file] [log] [blame]
Asiri Rathnayake6d3ea682016-10-13 15:05:19 +00001//===------------------------ threading_support.h -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCXXABI_THREADING_SUPPORT_H
11#define _LIBCXXABI_THREADING_SUPPORT_H
12
13#include "__cxxabi_config.h"
14#include "config.h"
15
16#ifndef _LIBCXXABI_HAS_NO_THREADS
17
18#if defined(_LIBCXXABI_USE_THREAD_API_PTHREAD)
19#include <pthread.h>
20
21#define _LIBCXXABI_THREAD_ABI_VISIBILITY inline _LIBCXXABI_INLINE_VISIBILITY
22
23// Mutex
24typedef pthread_mutex_t __libcxxabi_mutex_t;
25#define _LIBCXXABI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
26
27_LIBCXXABI_THREAD_ABI_VISIBILITY
28int __libcxxabi_mutex_lock(__libcxxabi_mutex_t *mutex) {
29 return pthread_mutex_lock(mutex);
30}
31
32_LIBCXXABI_THREAD_ABI_VISIBILITY
33int __libcxxabi_mutex_unlock(__libcxxabi_mutex_t *mutex) {
34 return pthread_mutex_unlock(mutex);
35}
36
37// Condition variable
38typedef pthread_cond_t __libcxxabi_condvar_t;
39#define _LIBCXXABI_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
40
41_LIBCXXABI_THREAD_ABI_VISIBILITY
42int __libcxxabi_condvar_wait(__libcxxabi_condvar_t *cv,
43 __libcxxabi_mutex_t *mutex) {
44 return pthread_cond_wait(cv, mutex);
45}
46
47_LIBCXXABI_THREAD_ABI_VISIBILITY
48int __libcxxabi_condvar_broadcast(__libcxxabi_condvar_t *cv) {
49 return pthread_cond_broadcast(cv);
50}
51
52// Execute once
53typedef pthread_once_t __libcxxabi_exec_once_flag;
54#define _LIBCXXABI_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT
55
56_LIBCXXABI_THREAD_ABI_VISIBILITY
57int __libcxxabi_execute_once(__libcxxabi_exec_once_flag *flag,
58 void (*init_routine)(void)) {
59 return pthread_once(flag, init_routine);
60}
61
62// Thread id
63#if defined(__APPLE__) && !defined(__arm__)
64_LIBCXXABI_THREAD_ABI_VISIBILITY
65mach_port_t __libcxxabi_thread_get_port()
66{
67 return pthread_mach_thread_np(pthread_self());
68}
69#endif
70
71// Thread
72typedef pthread_t __libcxxabi_thread_t;
73
74_LIBCXXABI_THREAD_ABI_VISIBILITY
75int __libcxxabi_thread_create(__libcxxabi_thread_t* __t,
76 void* (*__func)(void*), void* __arg)
77{
78 return pthread_create(__t, 0, __func, __arg);
79}
80
81_LIBCXXABI_THREAD_ABI_VISIBILITY
82int __libcxxabi_thread_join(__libcxxabi_thread_t* __t)
83{
84 return pthread_join(*__t, 0);
85}
86
87// TLS
88typedef pthread_key_t __libcxxabi_tls_key;
89
90_LIBCXXABI_THREAD_ABI_VISIBILITY
91int __libcxxabi_tls_create(__libcxxabi_tls_key *key,
92 void (*destructor)(void *)) {
93 return pthread_key_create(key, destructor);
94}
95
96_LIBCXXABI_THREAD_ABI_VISIBILITY
97void *__libcxxabi_tls_get(__libcxxabi_tls_key key) {
98 return pthread_getspecific(key);
99}
100
101_LIBCXXABI_THREAD_ABI_VISIBILITY
102int __libcxxabi_tls_set(__libcxxabi_tls_key key, void *value) {
103 return pthread_setspecific(key, value);
104}
105#endif // _LIBCXXABI_USE_THREAD_API_PTHREAD
106#endif // !_LIBCXXABI_HAS_NO_THREADS
107#endif // _LIBCXXABI_THREADING_SUPPORT_H