summaryrefslogtreecommitdiffstats
path: root/libcxxabi/src/threading_support.h
blob: 78a225f8af40b5c80e361db45dbee26aae7d9564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//===------------------------ threading_support.h -------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCXXABI_THREADING_SUPPORT_H
#define _LIBCXXABI_THREADING_SUPPORT_H

#include "__cxxabi_config.h"
#include "config.h"

#ifndef _LIBCXXABI_HAS_NO_THREADS

#if defined(_LIBCXXABI_USE_THREAD_API_PTHREAD)
#include <pthread.h>

#define _LIBCXXABI_THREAD_ABI_VISIBILITY inline _LIBCXXABI_INLINE_VISIBILITY

// Mutex
typedef pthread_mutex_t __libcxxabi_mutex_t;
#define _LIBCXXABI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER

_LIBCXXABI_THREAD_ABI_VISIBILITY
int __libcxxabi_mutex_lock(__libcxxabi_mutex_t *mutex) {
  return pthread_mutex_lock(mutex);
}

_LIBCXXABI_THREAD_ABI_VISIBILITY
int __libcxxabi_mutex_unlock(__libcxxabi_mutex_t *mutex) {
  return pthread_mutex_unlock(mutex);
}

// Condition variable
typedef pthread_cond_t __libcxxabi_condvar_t;
#define _LIBCXXABI_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER

_LIBCXXABI_THREAD_ABI_VISIBILITY
int __libcxxabi_condvar_wait(__libcxxabi_condvar_t *cv,
                             __libcxxabi_mutex_t *mutex) {
  return pthread_cond_wait(cv, mutex);
}

_LIBCXXABI_THREAD_ABI_VISIBILITY
int __libcxxabi_condvar_broadcast(__libcxxabi_condvar_t *cv) {
  return pthread_cond_broadcast(cv);
}

// Execute once
typedef pthread_once_t __libcxxabi_exec_once_flag;
#define _LIBCXXABI_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT

_LIBCXXABI_THREAD_ABI_VISIBILITY
int __libcxxabi_execute_once(__libcxxabi_exec_once_flag *flag,
                             void (*init_routine)(void)) {
  return pthread_once(flag, init_routine);
}

// Thread id
#if defined(__APPLE__) && !defined(__arm__)
_LIBCXXABI_THREAD_ABI_VISIBILITY
mach_port_t __libcxxabi_thread_get_port()
{
    return pthread_mach_thread_np(pthread_self());
}
#endif

// Thread
typedef pthread_t __libcxxabi_thread_t;

_LIBCXXABI_THREAD_ABI_VISIBILITY
int __libcxxabi_thread_create(__libcxxabi_thread_t* __t,
                           void* (*__func)(void*), void* __arg)
{
    return pthread_create(__t, 0, __func, __arg);
}

_LIBCXXABI_THREAD_ABI_VISIBILITY
int __libcxxabi_thread_join(__libcxxabi_thread_t* __t)
{
    return pthread_join(*__t, 0);
}

// TLS
typedef pthread_key_t __libcxxabi_tls_key;

_LIBCXXABI_THREAD_ABI_VISIBILITY
int __libcxxabi_tls_create(__libcxxabi_tls_key *key,
                           void (*destructor)(void *)) {
  return pthread_key_create(key, destructor);
}

_LIBCXXABI_THREAD_ABI_VISIBILITY
void *__libcxxabi_tls_get(__libcxxabi_tls_key key) {
  return pthread_getspecific(key);
}

_LIBCXXABI_THREAD_ABI_VISIBILITY
int __libcxxabi_tls_set(__libcxxabi_tls_key key, void *value) {
  return pthread_setspecific(key, value);
}
#endif // _LIBCXXABI_USE_THREAD_API_PTHREAD
#endif // !_LIBCXXABI_HAS_NO_THREADS
#endif // _LIBCXXABI_THREADING_SUPPORT_H
OpenPOWER on IntegriCloud