/src/boringssl/crypto/thread_pthread.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* 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 | | // Ensure we can't call OPENSSL_malloc circularly. |
16 | | #define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC |
17 | | #include "internal.h" |
18 | | |
19 | | #if defined(OPENSSL_PTHREADS) |
20 | | |
21 | | #include <assert.h> |
22 | | #include <pthread.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | |
26 | 202 | void CRYPTO_MUTEX_init(CRYPTO_MUTEX *lock) { |
27 | 202 | if (pthread_rwlock_init(lock, NULL) != 0) { |
28 | 0 | abort(); |
29 | 0 | } |
30 | 202 | } |
31 | | |
32 | 59 | void CRYPTO_MUTEX_lock_read(CRYPTO_MUTEX *lock) { |
33 | 59 | if (pthread_rwlock_rdlock(lock) != 0) { |
34 | 0 | abort(); |
35 | 0 | } |
36 | 59 | } |
37 | | |
38 | 59 | void CRYPTO_MUTEX_lock_write(CRYPTO_MUTEX *lock) { |
39 | 59 | if (pthread_rwlock_wrlock(lock) != 0) { |
40 | 0 | abort(); |
41 | 0 | } |
42 | 59 | } |
43 | | |
44 | 59 | void CRYPTO_MUTEX_unlock_read(CRYPTO_MUTEX *lock) { |
45 | 59 | if (pthread_rwlock_unlock(lock) != 0) { |
46 | 0 | abort(); |
47 | 0 | } |
48 | 59 | } |
49 | | |
50 | 59 | void CRYPTO_MUTEX_unlock_write(CRYPTO_MUTEX *lock) { |
51 | 59 | if (pthread_rwlock_unlock(lock) != 0) { |
52 | 0 | abort(); |
53 | 0 | } |
54 | 59 | } |
55 | | |
56 | 202 | void CRYPTO_MUTEX_cleanup(CRYPTO_MUTEX *lock) { |
57 | 202 | pthread_rwlock_destroy(lock); |
58 | 202 | } |
59 | | |
60 | 69.1k | void CRYPTO_once(CRYPTO_once_t *once, void (*init)(void)) { |
61 | 69.1k | if (pthread_once(once, init) != 0) { |
62 | 0 | abort(); |
63 | 0 | } |
64 | 69.1k | } |
65 | | |
66 | | static pthread_mutex_t g_destructors_lock = PTHREAD_MUTEX_INITIALIZER; |
67 | | static thread_local_destructor_t g_destructors[NUM_OPENSSL_THREAD_LOCALS]; |
68 | | |
69 | | // thread_local_destructor is called when a thread exits. It releases thread |
70 | | // local data for that thread only. |
71 | 0 | static void thread_local_destructor(void *arg) { |
72 | 0 | if (arg == NULL) { |
73 | 0 | return; |
74 | 0 | } |
75 | | |
76 | 0 | thread_local_destructor_t destructors[NUM_OPENSSL_THREAD_LOCALS]; |
77 | 0 | if (pthread_mutex_lock(&g_destructors_lock) != 0) { |
78 | 0 | return; |
79 | 0 | } |
80 | 0 | OPENSSL_memcpy(destructors, g_destructors, sizeof(destructors)); |
81 | 0 | pthread_mutex_unlock(&g_destructors_lock); |
82 | |
|
83 | 0 | unsigned i; |
84 | 0 | void **pointers = arg; |
85 | 0 | for (i = 0; i < NUM_OPENSSL_THREAD_LOCALS; i++) { |
86 | 0 | if (destructors[i] != NULL) { |
87 | 0 | destructors[i](pointers[i]); |
88 | 0 | } |
89 | 0 | } |
90 | |
|
91 | 0 | free(pointers); |
92 | 0 | } |
93 | | |
94 | | static pthread_once_t g_thread_local_init_once = PTHREAD_ONCE_INIT; |
95 | | static pthread_key_t g_thread_local_key; |
96 | | static int g_thread_local_key_created = 0; |
97 | | |
98 | 2 | static void thread_local_init(void) { |
99 | 2 | g_thread_local_key_created = |
100 | 2 | pthread_key_create(&g_thread_local_key, thread_local_destructor) == 0; |
101 | 2 | } |
102 | | |
103 | 9.49k | void *CRYPTO_get_thread_local(thread_local_data_t index) { |
104 | 9.49k | CRYPTO_once(&g_thread_local_init_once, thread_local_init); |
105 | 9.49k | if (!g_thread_local_key_created) { |
106 | 0 | return NULL; |
107 | 0 | } |
108 | | |
109 | 9.49k | void **pointers = pthread_getspecific(g_thread_local_key); |
110 | 9.49k | if (pointers == NULL) { |
111 | 2 | return NULL; |
112 | 2 | } |
113 | 9.49k | return pointers[index]; |
114 | 9.49k | } |
115 | | |
116 | | int CRYPTO_set_thread_local(thread_local_data_t index, void *value, |
117 | 4 | thread_local_destructor_t destructor) { |
118 | 4 | CRYPTO_once(&g_thread_local_init_once, thread_local_init); |
119 | 4 | if (!g_thread_local_key_created) { |
120 | 0 | destructor(value); |
121 | 0 | return 0; |
122 | 0 | } |
123 | | |
124 | 4 | void **pointers = pthread_getspecific(g_thread_local_key); |
125 | 4 | if (pointers == NULL) { |
126 | 2 | pointers = malloc(sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS); |
127 | 2 | if (pointers == NULL) { |
128 | 0 | destructor(value); |
129 | 0 | return 0; |
130 | 0 | } |
131 | 2 | OPENSSL_memset(pointers, 0, sizeof(void *) * NUM_OPENSSL_THREAD_LOCALS); |
132 | 2 | if (pthread_setspecific(g_thread_local_key, pointers) != 0) { |
133 | 0 | free(pointers); |
134 | 0 | destructor(value); |
135 | 0 | return 0; |
136 | 0 | } |
137 | 2 | } |
138 | | |
139 | 4 | if (pthread_mutex_lock(&g_destructors_lock) != 0) { |
140 | 0 | destructor(value); |
141 | 0 | return 0; |
142 | 0 | } |
143 | 4 | g_destructors[index] = destructor; |
144 | 4 | pthread_mutex_unlock(&g_destructors_lock); |
145 | | |
146 | 4 | pointers[index] = value; |
147 | 4 | return 1; |
148 | 4 | } |
149 | | |
150 | | #endif // OPENSSL_PTHREADS |