blob: 90b3d60515cd6f39038e67fa8fdac4b2caa962ef [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
19#include <pthread.h>
Adam Langleydf1f5e72015-04-13 11:04:08 -070020#include <stdlib.h>
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070021#include <string.h>
22
23#include <openssl/mem.h>
Adam Langleydf1f5e72015-04-13 11:04:08 -070024#include <openssl/type_check.h>
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070025
26
Adam Langleydf1f5e72015-04-13 11:04:08 -070027OPENSSL_COMPILE_ASSERT(sizeof(CRYPTO_MUTEX) >= sizeof(pthread_rwlock_t),
28 CRYPTO_MUTEX_too_small);
29
30void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) {
31 if (pthread_rwlock_init((pthread_rwlock_t *) lock, NULL) != 0) {
32 abort();
33 }
34}
35
36void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) {
37 if (pthread_rwlock_rdlock((pthread_rwlock_t *) lock) != 0) {
38 abort();
39 }
40}
41
42void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) {
43 if (pthread_rwlock_wrlock((pthread_rwlock_t *) lock) != 0) {
44 abort();
45 }
46}
47
David Benjamin29270de2016-05-24 15:28:36 +000048void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) {
49 if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
50 abort();
51 }
52}
53
54void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) {
Adam Langleydf1f5e72015-04-13 11:04:08 -070055 if (pthread_rwlock_unlock((pthread_rwlock_t *) lock) != 0) {
56 abort();
57 }
58}
59
60void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) {
61 pthread_rwlock_destroy((pthread_rwlock_t *) lock);
62}
63
64void CRYPTO_STATIC_MUTEX_lock_read(struct CRYPTO_STATIC_MUTEX *lock) {
65 if (pthread_rwlock_rdlock(&lock->lock) != 0) {
66 abort();
67 }
68}
69
70void CRYPTO_STATIC_MUTEX_lock_write(struct CRYPTO_STATIC_MUTEX *lock) {
71 if (pthread_rwlock_wrlock(&lock->lock) != 0) {
72 abort();
73 }
74}
75
David Benjamin29270de2016-05-24 15:28:36 +000076void CRYPTO_STATIC_MUTEX_unlock_read(struct CRYPTO_STATIC_MUTEX *lock) {
77 if (pthread_rwlock_unlock(&lock->lock) != 0) {
78 abort();
79 }
80}
81
82void CRYPTO_STATIC_MUTEX_unlock_write(struct CRYPTO_STATIC_MUTEX *lock) {
Adam Langleydf1f5e72015-04-13 11:04:08 -070083 if (pthread_rwlock_unlock(&lock->lock) != 0) {
84 abort();
85 }
86}
87
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070088void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) {
Piotr Sikorac324f172015-11-16 23:56:55 -080089 if (pthread_once(once, init) != 0) {
Piotr Sikorac324f172015-11-16 23:56:55 -080090 abort();
91 }
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070092}
93
94static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER;
95static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS];
96
97static void thread_local_destructor(void *arg) {
98 if (arg == NULL) {
99 return;
100 }
101
102 thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS];
103 if (pthread_mutex_lock(&g_destructors_lock) != 0) {
104 return;
105 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500106 OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors));
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700107 pthread_mutex_unlock(&g_destructors_lock);
108
109 unsigned i;
110 void **pointers = arg;
111 for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) {
112 if (destructors[i] != NULL) {
113 destructors[i](pointers[i]);
114 }
115 }
116
117 OPENSSL_free(pointers);
118}
119
120static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT;
121static pthread_key_t g_thread_local_key;
122static int g_thread_local_failed = 0;
123
124static void thread_local_init(void) {
125 g_thread_local_failed =
126 pthread_key_create(&g_thread_local_key, thread_local_destructor) != 0;
127}
128
129void *CRYPTO_get_thread_local(thread_local_data_t index) {
130 CRYPTO_once(&g_thread_local_init_once, thread_local_init);
131 if (g_thread_local_failed) {
132 return NULL;
133 }
134
135 void **pointers = pthread_getspecific(g_thread_local_key);
136 if (pointers == NULL) {
137 return NULL;
138 }
139 return pointers[index];
140}
141
142int CRYPTO_set_thread_local(thread_local_data_t index, void *value,
143 thread_local_destructor_t destructor) {
144 CRYPTO_once(&g_thread_local_init_once, thread_local_init);
145 if (g_thread_local_failed) {
146 destructor(value);
147 return 0;
148 }
149
150 void **pointers = pthread_getspecific(g_thread_local_key);
151 if (pointers == NULL) {
152 pointers = OPENSSL_malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
153 if (pointers == NULL) {
154 destructor(value);
155 return 0;
156 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500157 OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS);
Adam Langleyd7c5dfb2015-03-16 12:48:56 -0700158 if (pthread_setspecific(g_thread_local_key, pointers) != 0) {
159 OPENSSL_free(pointers);
160 destructor(value);
161 return 0;
162 }
163 }
164
165 if (pthread_mutex_lock(&g_destructors_lock) != 0) {
166 destructor(value);
167 return 0;
168 }
169 g_destructors[index] = destructor;
170 pthread_mutex_unlock(&g_destructors_lock);
171
172 pointers[index] = value;
173 return 1;
174}
175
David Benjamin808f8322017-08-18 14:06:02 -0400176#endif // OPENSSL_PTHREADS