]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
1cb7eff4 | 2 | * Copyright 2016-2019 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 | ||
1b0fe00e DMSP |
15 | # if defined(OPENSSL_SYS_UNIX) |
16 | # include <sys/types.h> | |
17 | # include <unistd.h> | |
18 | #endif | |
19 | ||
ec93a292 DK |
20 | # ifdef PTHREAD_RWLOCK_INITIALIZER |
21 | # define USE_RWLOCK | |
22 | # endif | |
2accf3f7 | 23 | |
71a04cfc AG |
24 | CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void) |
25 | { | |
ec93a292 | 26 | # ifdef USE_RWLOCK |
7de2b9c4 RS |
27 | CRYPTO_RWLOCK *lock; |
28 | ||
29 | if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) { | |
30 | /* Don't set error, to avoid recursion blowup. */ | |
71a04cfc | 31 | return NULL; |
7de2b9c4 | 32 | } |
71a04cfc | 33 | |
0b2fc928 F |
34 | if (pthread_rwlock_init(lock, NULL) != 0) { |
35 | OPENSSL_free(lock); | |
71a04cfc | 36 | return NULL; |
0b2fc928 | 37 | } |
ec93a292 DK |
38 | # else |
39 | pthread_mutexattr_t attr; | |
7de2b9c4 RS |
40 | CRYPTO_RWLOCK *lock; |
41 | ||
42 | if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) { | |
43 | /* Don't set error, to avoid recursion blowup. */ | |
2accf3f7 | 44 | return NULL; |
7de2b9c4 | 45 | } |
2accf3f7 | 46 | |
2accf3f7 DK |
47 | pthread_mutexattr_init(&attr); |
48 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | |
5d5eed44 | 49 | |
2accf3f7 DK |
50 | if (pthread_mutex_init(lock, &attr) != 0) { |
51 | pthread_mutexattr_destroy(&attr); | |
52 | OPENSSL_free(lock); | |
53 | return NULL; | |
54 | } | |
5d5eed44 | 55 | |
2accf3f7 | 56 | pthread_mutexattr_destroy(&attr); |
ec93a292 | 57 | # endif |
71a04cfc AG |
58 | |
59 | return lock; | |
60 | } | |
61 | ||
62 | int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock) | |
63 | { | |
ec93a292 | 64 | # ifdef USE_RWLOCK |
71a04cfc AG |
65 | if (pthread_rwlock_rdlock(lock) != 0) |
66 | return 0; | |
ec93a292 | 67 | # else |
2accf3f7 DK |
68 | if (pthread_mutex_lock(lock) != 0) |
69 | return 0; | |
ec93a292 | 70 | # endif |
71a04cfc AG |
71 | |
72 | return 1; | |
73 | } | |
74 | ||
75 | int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock) | |
76 | { | |
ec93a292 | 77 | # ifdef USE_RWLOCK |
71a04cfc AG |
78 | if (pthread_rwlock_wrlock(lock) != 0) |
79 | return 0; | |
ec93a292 | 80 | # else |
2accf3f7 DK |
81 | if (pthread_mutex_lock(lock) != 0) |
82 | return 0; | |
ec93a292 | 83 | # endif |
71a04cfc AG |
84 | |
85 | return 1; | |
86 | } | |
87 | ||
88 | int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock) | |
89 | { | |
ec93a292 | 90 | # ifdef USE_RWLOCK |
71a04cfc AG |
91 | if (pthread_rwlock_unlock(lock) != 0) |
92 | return 0; | |
ec93a292 | 93 | # else |
2accf3f7 DK |
94 | if (pthread_mutex_unlock(lock) != 0) |
95 | return 0; | |
ec93a292 | 96 | # endif |
71a04cfc AG |
97 | |
98 | return 1; | |
99 | } | |
100 | ||
101 | void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock) | |
102 | { | |
103 | if (lock == NULL) | |
104 | return; | |
105 | ||
ec93a292 | 106 | # ifdef USE_RWLOCK |
71a04cfc | 107 | pthread_rwlock_destroy(lock); |
ec93a292 | 108 | # else |
2accf3f7 | 109 | pthread_mutex_destroy(lock); |
ec93a292 | 110 | # endif |
71a04cfc AG |
111 | OPENSSL_free(lock); |
112 | ||
113 | return; | |
114 | } | |
115 | ||
116 | int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void)) | |
117 | { | |
118 | if (pthread_once(once, init) != 0) | |
119 | return 0; | |
120 | ||
121 | return 1; | |
122 | } | |
123 | ||
124 | int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *)) | |
125 | { | |
126 | if (pthread_key_create(key, cleanup) != 0) | |
127 | return 0; | |
128 | ||
129 | return 1; | |
130 | } | |
131 | ||
132 | void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key) | |
133 | { | |
134 | return pthread_getspecific(*key); | |
135 | } | |
136 | ||
137 | int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val) | |
138 | { | |
139 | if (pthread_setspecific(*key, val) != 0) | |
140 | return 0; | |
141 | ||
142 | return 1; | |
143 | } | |
144 | ||
145 | int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key) | |
146 | { | |
147 | if (pthread_key_delete(*key) != 0) | |
148 | return 0; | |
149 | ||
150 | return 1; | |
151 | } | |
152 | ||
153 | CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void) | |
154 | { | |
155 | return pthread_self(); | |
156 | } | |
157 | ||
158 | int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b) | |
159 | { | |
160 | return pthread_equal(a, b); | |
161 | } | |
162 | ||
163 | int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock) | |
164 | { | |
11fc6c76 | 165 | # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL) |
1beca676 RL |
166 | if (__atomic_is_lock_free(sizeof(*val), val)) { |
167 | *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL); | |
168 | return 1; | |
169 | } | |
170 | # endif | |
71a04cfc AG |
171 | if (!CRYPTO_THREAD_write_lock(lock)) |
172 | return 0; | |
173 | ||
174 | *val += amount; | |
175 | *ret = *val; | |
176 | ||
177 | if (!CRYPTO_THREAD_unlock(lock)) | |
178 | return 0; | |
71a04cfc AG |
179 | |
180 | return 1; | |
181 | } | |
182 | ||
b842fcbb RS |
183 | # ifdef OPENSSL_SYS_UNIX |
184 | static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT; | |
185 | ||
186 | static void fork_once_func(void) | |
187 | { | |
188 | pthread_atfork(OPENSSL_fork_prepare, | |
189 | OPENSSL_fork_parent, OPENSSL_fork_child); | |
190 | } | |
191 | # endif | |
192 | ||
2915fe19 RS |
193 | int openssl_init_fork_handlers(void) |
194 | { | |
195 | # ifdef OPENSSL_SYS_UNIX | |
b842fcbb | 196 | if (pthread_once(&fork_once_control, fork_once_func) == 0) |
2915fe19 RS |
197 | return 1; |
198 | # endif | |
199 | return 0; | |
200 | } | |
1b0fe00e DMSP |
201 | |
202 | int openssl_get_fork_id(void) | |
203 | { | |
204 | return getpid(); | |
205 | } | |
71a04cfc | 206 | #endif |