blob: 82cbbfe5741978073313603a0695b0da2fd22e7c [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
Bob Beckfc524c12023-02-07 14:32:41 -070015// Ensure we can't call OPENSSL_malloc circularly.
16#define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070017#include "internal.h"
18
David Benjamin84759bd2016-09-20 17:35:09 -040019#if defined(OPENSSL_PTHREADS)
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070020
David Benjaminb7d63202022-07-26 13:25:02 -070021#include <assert.h>
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070022#include <pthread.h>
Adam Langleydf1f5e72015-04-13 11:04:08 -070023#include <stdlib.h>
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070024#include <string.h>
25
David Benjaminb7d63202022-07-26 13:25:02 -070026static_assert(sizeof(CRYPTO_MUTEX) >= sizeof(pthread_rwlock_t),
27 "CRYPTO_MUTEX is too small");
28static_assert(alignof(CRYPTO_MUTEX) >= alignof(pthread_rwlock_t),
29 "CRYPTO_MUTEX has insufficient alignment");
Adam Langleydf1f5e72015-04-13 11:04:08 -070030
31void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
32 if (pthread_rwlock_init((pthread_rwlock_t *) lock, NULL) != 0) {
33 abort();
34 }
35}
36
37void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
38 if (pthread_rwlock_rdlock((pthread_rwlock_t *) lock) != 0) {
39 abort();
40 }
41}
42
43void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
44 if (pthread_rwlock_wrlock((pthread_rwlock_t *) lock) != 0) {
45 abort();
46 }
47}
48
David Benjamin29270de2016-05-24 15:28:36 +000049void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) {
50 if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
51 abort();
52 }
53}
54
55void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) {
Adam Langleydf1f5e72015-04-13 11:04:08 -070056 if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
57 abort();
58 }
59}
60
61void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
62 pthread_rwlock_destroy((pthread_rwlock_t *) lock);
63}
64
65void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
66 if (pthread_rwlock_rdlock(&lock->lock) != 0) {
67 abort();
68 }
69}
70
71void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
72 if (pthread_rwlock_wrlock(&lock->lock) != 0) {
73 abort();
74 }
75}
76
David Benjamin29270de2016-05-24 15:28:36 +000077void CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX *lock) {
78 if (pthread_rwlock_unlock(&lock->lock) != 0) {
79 abort();
80 }
81}
82
83void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) {
Adam Langleydf1f5e72015-04-13 11:04:08 -070084 if (pthread_rwlock_unlock(&lock->lock) != 0) {
85 abort();
86 }
87}
88
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070089void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
Piotr Sikorac324f172015-11-16 23:56:55 -080090 if (pthread_once(once, init) != 0) {
Piotr Sikorac324f172015-11-16 23:56:55 -080091 abort();
92 }
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070093}
94
95static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER;
96static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
97
Fred Gylys-Colwell02d696f2018-02-07 17:11:18 -080098// thread_local_destructor is called when a thread exits. It releases thread
99// local data for that thread only.
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700100static void thread_local_destructor(void *arg) {
101 if (arg == NULL) {
102 return;
103 }
104
105 thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
106 if (pthread_mutex_lock(&g_destructors_lock) != 0) {
107 return;
108 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500109 OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors));
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700110 pthread_mutex_unlock(&g_destructors_lock);
111
112 unsigned i;
113 void **pointers = arg;
114 for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
115 if (destructors[i] != NULL) {
116 destructors[i](pointers[i]);
117 }
118 }
119
Bob Beckfc524c12023-02-07 14:32:41 -0700120 free(pointers);
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700121}
122
123static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT;
124static pthread_key_t g_thread_local_key;
Fred Gylys-Colwell02d696f2018-02-07 17:11:18 -0800125static int g_thread_local_key_created = 0;
126
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700127static void thread_local_init(void) {
Fred Gylys-Colwell02d696f2018-02-07 17:11:18 -0800128 g_thread_local_key_created =
129 pthread_key_create(&g_thread_local_key, thread_local_destructor) == 0;
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700130}
131
132void *CRYPTO_get_thread_local(thread_local_data_t index) {
133 CRYPTO_once(&g_thread_local_init_once, thread_local_init);
Fred Gylys-Colwell02d696f2018-02-07 17:11:18 -0800134 if (!g_thread_local_key_created) {
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700135 return NULL;
136 }
137
138 void **pointers = pthread_getspecific(g_thread_local_key);
139 if (pointers == NULL) {
140 return NULL;
141 }
142 return pointers[index];
143}
144
145int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
146 thread_local_destructor_t destructor) {
147 CRYPTO_once(&g_thread_local_init_once, thread_local_init);
Fred Gylys-Colwell02d696f2018-02-07 17:11:18 -0800148 if (!g_thread_local_key_created) {
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700149 destructor(value);
150 return 0;
151 }
152
153 void **pointers = pthread_getspecific(g_thread_local_key);
154 if (pointers == NULL) {
Bob Beckfc524c12023-02-07 14:32:41 -0700155 pointers = malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700156 if (pointers == NULL) {
157 destructor(value);
158 return 0;
159 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500160 OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700161 if (pthread_setspecific(g_thread_local_key, pointers) != 0) {
Bob Beckfc524c12023-02-07 14:32:41 -0700162 free(pointers);
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700163 destructor(value);
164 return 0;
165 }
166 }
167
168 if (pthread_mutex_lock(&g_destructors_lock) != 0) {
169 destructor(value);
170 return 0;
171 }
172 g_destructors[index] = destructor;
173 pthread_mutex_unlock(&g_destructors_lock);
174
175 pointers[index] = value;
176 return 1;
177}
178
David Benjamin808f8322017-08-18 14:06:02 -0400179#endif // OPENSSL_PTHREADS