blob: 08bdd5a750b09225f05f39a0244ad0d4b9ab02f6 [file] [log] [blame]
Adam Langleyd7c5dfb2015-03-16 12:48:56 -07001/* Copyright (c) 2015, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include "internal.h"
16
David Benjamin84759bd2016-09-20 17:35:09 -040017#if defined(OPENSSL_PTHREADS)
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070018
David Benjaminb7d63202022-07-26 13:25:02 -070019#include <assert.h>
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070020#include <pthread.h>
Adam Langleydf1f5e72015-04-13 11:04:08 -070021#include <stdlib.h>
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070022#include <string.h>
23
24#include <openssl/mem.h>
25
26
David Benjaminb7d63202022-07-26 13:25:02 -070027static_assert(sizeof(CRYPTO_MUTEX) >= sizeof(pthread_rwlock_t),
28 "CRYPTO_MUTEX is too small");
29static_assert(alignof(CRYPTO_MUTEX) >= alignof(pthread_rwlock_t),
30 "CRYPTO_MUTEX has insufficient alignment");
Adam Langleydf1f5e72015-04-13 11:04:08 -070031
32void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
33 if (pthread_rwlock_init((pthread_rwlock_t *) lock, NULL) != 0) {
34 abort();
35 }
36}
37
38void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
39 if (pthread_rwlock_rdlock((pthread_rwlock_t *) lock) != 0) {
40 abort();
41 }
42}
43
44void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
45 if (pthread_rwlock_wrlock((pthread_rwlock_t *) lock) != 0) {
46 abort();
47 }
48}
49
David Benjamin29270de2016-05-24 15:28:36 +000050void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) {
51 if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
52 abort();
53 }
54}
55
56void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) {
Adam Langleydf1f5e72015-04-13 11:04:08 -070057 if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
58 abort();
59 }
60}
61
62void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
63 pthread_rwlock_destroy((pthread_rwlock_t *) lock);
64}
65
66void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
67 if (pthread_rwlock_rdlock(&lock->lock) != 0) {
68 abort();
69 }
70}
71
72void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
73 if (pthread_rwlock_wrlock(&lock->lock) != 0) {
74 abort();
75 }
76}
77
David Benjamin29270de2016-05-24 15:28:36 +000078void CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX *lock) {
79 if (pthread_rwlock_unlock(&lock->lock) != 0) {
80 abort();
81 }
82}
83
84void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) {
Adam Langleydf1f5e72015-04-13 11:04:08 -070085 if (pthread_rwlock_unlock(&lock->lock) != 0) {
86 abort();
87 }
88}
89
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070090void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
Piotr Sikorac324f172015-11-16 23:56:55 -080091 if (pthread_once(once, init) != 0) {
Piotr Sikorac324f172015-11-16 23:56:55 -080092 abort();
93 }
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070094}
95
96static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER;
97static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
98
Fred Gylys-Colwell02d696f2018-02-07 17:11:18 -080099// thread_local_destructor is called when a thread exits. It releases thread
100// local data for that thread only.
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700101static void thread_local_destructor(void *arg) {
102 if (arg == NULL) {
103 return;
104 }
105
106 thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
107 if (pthread_mutex_lock(&g_destructors_lock) != 0) {
108 return;
109 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500110 OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors));
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700111 pthread_mutex_unlock(&g_destructors_lock);
112
113 unsigned i;
114 void **pointers = arg;
115 for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
116 if (destructors[i] != NULL) {
117 destructors[i](pointers[i]);
118 }
119 }
120
121 OPENSSL_free(pointers);
122}
123
124static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT;
125static pthread_key_t g_thread_local_key;
Fred Gylys-Colwell02d696f2018-02-07 17:11:18 -0800126static int g_thread_local_key_created = 0;
127
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700128static void thread_local_init(void) {
Fred Gylys-Colwell02d696f2018-02-07 17:11:18 -0800129 g_thread_local_key_created =
130 pthread_key_create(&g_thread_local_key, thread_local_destructor) == 0;
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700131}
132
133void *CRYPTO_get_thread_local(thread_local_data_t index) {
134 CRYPTO_once(&g_thread_local_init_once, thread_local_init);
Fred Gylys-Colwell02d696f2018-02-07 17:11:18 -0800135 if (!g_thread_local_key_created) {
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700136 return NULL;
137 }
138
139 void **pointers = pthread_getspecific(g_thread_local_key);
140 if (pointers == NULL) {
141 return NULL;
142 }
143 return pointers[index];
144}
145
146int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
147 thread_local_destructor_t destructor) {
148 CRYPTO_once(&g_thread_local_init_once, thread_local_init);
Fred Gylys-Colwell02d696f2018-02-07 17:11:18 -0800149 if (!g_thread_local_key_created) {
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700150 destructor(value);
151 return 0;
152 }
153
154 void **pointers = pthread_getspecific(g_thread_local_key);
155 if (pointers == NULL) {
156 pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
157 if (pointers == NULL) {
158 destructor(value);
159 return 0;
160 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500161 OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700162 if (pthread_setspecific(g_thread_local_key, pointers) != 0) {
163 OPENSSL_free(pointers);
164 destructor(value);
165 return 0;
166 }
167 }
168
169 if (pthread_mutex_lock(&g_destructors_lock) != 0) {
170 destructor(value);
171 return 0;
172 }
173 g_destructors[index] = destructor;
174 pthread_mutex_unlock(&g_destructors_lock);
175
176 pointers[index] = value;
177 return 1;
178}
179
David Benjamin808f8322017-08-18 14:06:02 -0400180#endif // OPENSSL_PTHREADS