Asiri Rathnayake | 6d3ea68 | 2016-10-13 15:05:19 +0000 | [diff] [blame] | 1 | //===------------------------ 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 |
| 24 | typedef pthread_mutex_t __libcxxabi_mutex_t; |
| 25 | #define _LIBCXXABI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
| 26 | |
| 27 | _LIBCXXABI_THREAD_ABI_VISIBILITY |
| 28 | int __libcxxabi_mutex_lock(__libcxxabi_mutex_t *mutex) { |
| 29 | return pthread_mutex_lock(mutex); |
| 30 | } |
| 31 | |
| 32 | _LIBCXXABI_THREAD_ABI_VISIBILITY |
| 33 | int __libcxxabi_mutex_unlock(__libcxxabi_mutex_t *mutex) { |
| 34 | return pthread_mutex_unlock(mutex); |
| 35 | } |
| 36 | |
| 37 | // Condition variable |
| 38 | typedef pthread_cond_t __libcxxabi_condvar_t; |
| 39 | #define _LIBCXXABI_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER |
| 40 | |
| 41 | _LIBCXXABI_THREAD_ABI_VISIBILITY |
| 42 | int __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 |
| 48 | int __libcxxabi_condvar_broadcast(__libcxxabi_condvar_t *cv) { |
| 49 | return pthread_cond_broadcast(cv); |
| 50 | } |
| 51 | |
| 52 | // Execute once |
| 53 | typedef pthread_once_t __libcxxabi_exec_once_flag; |
| 54 | #define _LIBCXXABI_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT |
| 55 | |
| 56 | _LIBCXXABI_THREAD_ABI_VISIBILITY |
| 57 | int __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 |
| 65 | mach_port_t __libcxxabi_thread_get_port() |
| 66 | { |
| 67 | return pthread_mach_thread_np(pthread_self()); |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | // Thread |
| 72 | typedef pthread_t __libcxxabi_thread_t; |
| 73 | |
| 74 | _LIBCXXABI_THREAD_ABI_VISIBILITY |
| 75 | int __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 |
| 82 | int __libcxxabi_thread_join(__libcxxabi_thread_t* __t) |
| 83 | { |
| 84 | return pthread_join(*__t, 0); |
| 85 | } |
| 86 | |
| 87 | // TLS |
| 88 | typedef pthread_key_t __libcxxabi_tls_key; |
| 89 | |
| 90 | _LIBCXXABI_THREAD_ABI_VISIBILITY |
| 91 | int __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 |
| 97 | void *__libcxxabi_tls_get(__libcxxabi_tls_key key) { |
| 98 | return pthread_getspecific(key); |
| 99 | } |
| 100 | |
| 101 | _LIBCXXABI_THREAD_ABI_VISIBILITY |
| 102 | int __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 |