]>
Commit | Line | Data |
---|---|---|
b1322259 RS |
1 | /* |
2 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. | |
71a04cfc | 3 | * |
b1322259 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
71a04cfc AG |
8 | */ |
9 | ||
10 | #include <openssl/crypto.h> | |
5f8dd0f8 | 11 | #include "internal/cryptlib.h" |
71a04cfc AG |
12 | |
13 | #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS) | |
14 | ||
ec93a292 DK |
15 | # ifdef PTHREAD_RWLOCK_INITIALIZER |
16 | # define USE_RWLOCK | |
17 | # endif | |
2accf3f7 | 18 | |
71a04cfc AG |
19 | CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void) |
20 | { | |
ec93a292 | 21 | # ifdef USE_RWLOCK |
71a04cfc AG |
22 | CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t)); |
23 | if (lock == NULL) | |
24 | return NULL; | |
25 | ||
0b2fc928 F |
26 | if (pthread_rwlock_init(lock, NULL) != 0) { |
27 | OPENSSL_free(lock); | |
71a04cfc | 28 | return NULL; |
0b2fc928 | 29 | } |
ec93a292 DK |
30 | # else |
31 | pthread_mutexattr_t attr; | |
2accf3f7 DK |
32 | CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_mutex_t)); |
33 | if (lock == NULL) | |
34 | return NULL; | |
35 | ||
2accf3f7 DK |
36 | pthread_mutexattr_init(&attr); |
37 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | |
5d5eed44 | 38 | |
2accf3f7 DK |
39 | if (pthread_mutex_init(lock, &attr) != 0) { |
40 | pthread_mutexattr_destroy(&attr); | |
41 | OPENSSL_free(lock); | |
42 | return NULL; | |
43 | } | |
5d5eed44 | 44 | |
2accf3f7 | 45 | pthread_mutexattr_destroy(&attr); |
ec93a292 | 46 | # endif |
71a04cfc AG |
47 | |
48 | return lock; | |
49 | } | |
50 | ||
51 | int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock) | |
52 | { | |
ec93a292 | 53 | # ifdef USE_RWLOCK |
71a04cfc AG |
54 | if (pthread_rwlock_rdlock(lock) != 0) |
55 | return 0; | |
ec93a292 | 56 | # else |
2accf3f7 DK |
57 | if (pthread_mutex_lock(lock) != 0) |
58 | return 0; | |
ec93a292 | 59 | # endif |
71a04cfc AG |
60 | |
61 | return 1; | |
62 | } | |
63 | ||
64 | int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock) | |
65 | { | |
ec93a292 | 66 | # ifdef USE_RWLOCK |
71a04cfc AG |
67 | if (pthread_rwlock_wrlock(lock) != 0) |
68 | return 0; | |
ec93a292 | 69 | # else |
2accf3f7 DK |
70 | if (pthread_mutex_lock(lock) != 0) |
71 | return 0; | |
ec93a292 | 72 | # endif |
71a04cfc AG |
73 | |
74 | return 1; | |
75 | } | |
76 | ||
77 | int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock) | |
78 | { | |
ec93a292 | 79 | # ifdef USE_RWLOCK |
71a04cfc AG |
80 | if (pthread_rwlock_unlock(lock) != 0) |
81 | return 0; | |
ec93a292 | 82 | # else |
2accf3f7 DK |
83 | if (pthread_mutex_unlock(lock) != 0) |
84 | return 0; | |
ec93a292 | 85 | # endif |
71a04cfc AG |
86 | |
87 | return 1; | |
88 | } | |
89 | ||
90 | void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) | |
91 | { | |
92 | if (lock == NULL) | |
93 | return; | |
94 | ||
ec93a292 | 95 | # ifdef USE_RWLOCK |
71a04cfc | 96 | pthread_rwlock_destroy(lock); |
ec93a292 | 97 | # else |
2accf3f7 | 98 | pthread_mutex_destroy(lock); |
ec93a292 | 99 | # endif |
71a04cfc AG |
100 | OPENSSL_free(lock); |
101 | ||
102 | return; | |
103 | } | |
104 | ||
105 | int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void)) | |
106 | { | |
107 | if (pthread_once(once, init) != 0) | |
108 | return 0; | |
109 | ||
110 | return 1; | |
111 | } | |
112 | ||
113 | int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) | |
114 | { | |
115 | if (pthread_key_create(key, cleanup) != 0) | |
116 | return 0; | |
117 | ||
118 | return 1; | |
119 | } | |
120 | ||
121 | void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key) | |
122 | { | |
123 | return pthread_getspecific(*key); | |
124 | } | |
125 | ||
126 | int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val) | |
127 | { | |
128 | if (pthread_setspecific(*key, val) != 0) | |
129 | return 0; | |
130 | ||
131 | return 1; | |
132 | } | |
133 | ||
134 | int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key) | |
135 | { | |
136 | if (pthread_key_delete(*key) != 0) | |
137 | return 0; | |
138 | ||
139 | return 1; | |
140 | } | |
141 | ||
142 | CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void) | |
143 | { | |
144 | return pthread_self(); | |
145 | } | |
146 | ||
147 | int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b) | |
148 | { | |
149 | return pthread_equal(a, b); | |
150 | } | |
151 | ||
152 | int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock) | |
153 | { | |
11fc6c76 | 154 | # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) |
1beca676 RL |
155 | if (__atomic_is_lock_free(sizeof(*val), val)) { |
156 | *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL); | |
157 | return 1; | |
158 | } | |
159 | # endif | |
71a04cfc AG |
160 | if (!CRYPTO_THREAD_write_lock(lock)) |
161 | return 0; | |
162 | ||
163 | *val += amount; | |
164 | *ret = *val; | |
165 | ||
166 | if (!CRYPTO_THREAD_unlock(lock)) | |
167 | return 0; | |
71a04cfc AG |
168 | |
169 | return 1; | |
170 | } | |
171 | ||
94683b7a P |
172 | int CRYPTO_atomic_read(int *val, int *ret, CRYPTO_RWLOCK *lock) |
173 | { | |
174 | # if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE) | |
175 | if (__atomic_is_lock_free(sizeof(*val), val)) { | |
176 | __atomic_load(val, ret, __ATOMIC_ACQUIRE); | |
177 | return 1; | |
178 | } | |
179 | # endif | |
f49452c2 | 180 | if (!CRYPTO_THREAD_read_lock(lock)) |
94683b7a P |
181 | return 0; |
182 | ||
183 | *ret = *val; | |
184 | ||
185 | if (!CRYPTO_THREAD_unlock(lock)) | |
186 | return 0; | |
187 | ||
188 | return 1; | |
189 | } | |
190 | ||
30ff41be P |
191 | int CRYPTO_atomic_write(int *val, int n, CRYPTO_RWLOCK *lock) |
192 | { | |
193 | # if defined(__GNUC__) && defined(__ATOMIC_RELEASE) | |
194 | if (__atomic_is_lock_free(sizeof(*val), val)) { | |
195 | __atomic_store(val, &n, __ATOMIC_RELEASE); | |
196 | return 1; | |
197 | } | |
198 | # endif | |
199 | if (!CRYPTO_THREAD_write_lock(lock)) | |
200 | return 0; | |
201 | ||
202 | *val = n; | |
203 | ||
204 | if (!CRYPTO_THREAD_unlock(lock)) | |
205 | return 0; | |
206 | ||
207 | return 1; | |
208 | } | |
209 | ||
b842fcbb RS |
210 | # ifdef OPENSSL_SYS_UNIX |
211 | static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT; | |
212 | ||
213 | static void fork_once_func(void) | |
214 | { | |
215 | pthread_atfork(OPENSSL_fork_prepare, | |
216 | OPENSSL_fork_parent, OPENSSL_fork_child); | |
217 | } | |
218 | # endif | |
219 | ||
2915fe19 RS |
220 | int openssl_init_fork_handlers(void) |
221 | { | |
222 | # ifdef OPENSSL_SYS_UNIX | |
b842fcbb | 223 | if (pthread_once(&fork_once_control, fork_once_func) == 0) |
2915fe19 RS |
224 | return 1; |
225 | # endif | |
226 | return 0; | |
227 | } | |
71a04cfc | 228 | #endif |