17 #if defined(OPENSSL_PTHREADS)
28 "CRYPTO_MUTEX is too small");
29 #if defined(__GNUC__) || defined(__clang__)
31 "CRYPTO_MUTEX has insufficient alignment");
35 if (pthread_rwlock_init((pthread_rwlock_t *) lock, NULL) != 0) {
41 if (pthread_rwlock_rdlock((pthread_rwlock_t *) lock) != 0) {
47 if (pthread_rwlock_wrlock((pthread_rwlock_t *) lock) != 0) {
53 if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
59 if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
65 pthread_rwlock_destroy((pthread_rwlock_t *) lock);
69 if (pthread_rwlock_rdlock(&lock->lock) != 0) {
75 if (pthread_rwlock_wrlock(&lock->lock) != 0) {
81 if (pthread_rwlock_unlock(&lock->lock) != 0) {
87 if (pthread_rwlock_unlock(&lock->lock) != 0) {
98 static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER;
103 static void thread_local_destructor(
void *
arg) {
109 if (pthread_mutex_lock(&g_destructors_lock) != 0) {
113 pthread_mutex_unlock(&g_destructors_lock);
116 void **pointers =
arg;
118 if (destructors[
i] != NULL) {
119 destructors[
i](pointers[
i]);
126 static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT;
127 static pthread_key_t g_thread_local_key;
128 static int g_thread_local_key_created = 0;
130 static void thread_local_init(
void) {
131 g_thread_local_key_created =
132 pthread_key_create(&g_thread_local_key, thread_local_destructor) == 0;
136 CRYPTO_once(&g_thread_local_init_once, thread_local_init);
137 if (!g_thread_local_key_created) {
141 void **pointers = pthread_getspecific(g_thread_local_key);
142 if (pointers == NULL) {
145 return pointers[
index];
150 CRYPTO_once(&g_thread_local_init_once, thread_local_init);
151 if (!g_thread_local_key_created) {
156 void **pointers = pthread_getspecific(g_thread_local_key);
157 if (pointers == NULL) {
159 if (pointers == NULL) {
164 if (pthread_setspecific(g_thread_local_key, pointers) != 0) {
171 if (pthread_mutex_lock(&g_destructors_lock) != 0) {
175 g_destructors[
index] = destructor;
176 pthread_mutex_unlock(&g_destructors_lock);
182 #endif // OPENSSL_PTHREADS