]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/threads_pthread.c
Moved OPENSSL_fork_prepare,_parent,_child from init.c to threads_pthread.c.
[thirdparty/openssl.git] / crypto / threads_pthread.c
CommitLineData
b1322259 1/*
454afd98 2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
71a04cfc 3 *
0e9725bc 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
9750b4d3
RB
10/* We need to use the OPENSSL_fork_*() deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
71a04cfc 13#include <openssl/crypto.h>
5f8dd0f8 14#include "internal/cryptlib.h"
71a04cfc 15
d6dda392
VK
16#if defined(__sun)
17# include <atomic.h>
18#endif
19
71a04cfc
AG
20#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
21
84952925
DMSP
22# if defined(OPENSSL_SYS_UNIX)
23# include <sys/types.h>
24# include <unistd.h>
25#endif
26
ec93a292
DK
27# ifdef PTHREAD_RWLOCK_INITIALIZER
28# define USE_RWLOCK
29# endif
2accf3f7 30
71a04cfc
AG
31CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)
32{
ec93a292 33# ifdef USE_RWLOCK
7de2b9c4
RS
34 CRYPTO_RWLOCK *lock;
35
36 if ((lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t))) == NULL) {
37 /* Don't set error, to avoid recursion blowup. */
71a04cfc 38 return NULL;
7de2b9c4 39 }
71a04cfc 40
0b2fc928
F
41 if (pthread_rwlock_init(lock, NULL) != 0) {
42 OPENSSL_free(lock);
71a04cfc 43 return NULL;
0b2fc928 44 }
ec93a292
DK
45# else
46 pthread_mutexattr_t attr;
7de2b9c4
RS
47 CRYPTO_RWLOCK *lock;
48
49 if ((lock = OPENSSL_zalloc(sizeof(pthread_mutex_t))) == NULL) {
50 /* Don't set error, to avoid recursion blowup. */
2accf3f7 51 return NULL;
7de2b9c4 52 }
2accf3f7 53
2accf3f7 54 pthread_mutexattr_init(&attr);
08073700
RB
55 #if defined(__TANDEM) && defined(_SPT_MODEL_)
56 pthread_mutexattr_setkind_np(&attr,MUTEX_RECURSIVE_NP);
57 #else
58 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
59 #endif
5d5eed44 60
2accf3f7
DK
61 if (pthread_mutex_init(lock, &attr) != 0) {
62 pthread_mutexattr_destroy(&attr);
63 OPENSSL_free(lock);
64 return NULL;
65 }
5d5eed44 66
2accf3f7 67 pthread_mutexattr_destroy(&attr);
ec93a292 68# endif
71a04cfc
AG
69
70 return lock;
71}
72
73int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock)
74{
ec93a292 75# ifdef USE_RWLOCK
71a04cfc
AG
76 if (pthread_rwlock_rdlock(lock) != 0)
77 return 0;
ec93a292 78# else
2accf3f7
DK
79 if (pthread_mutex_lock(lock) != 0)
80 return 0;
ec93a292 81# endif
71a04cfc
AG
82
83 return 1;
84}
85
86int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)
87{
ec93a292 88# ifdef USE_RWLOCK
71a04cfc
AG
89 if (pthread_rwlock_wrlock(lock) != 0)
90 return 0;
ec93a292 91# else
2accf3f7
DK
92 if (pthread_mutex_lock(lock) != 0)
93 return 0;
ec93a292 94# endif
71a04cfc
AG
95
96 return 1;
97}
98
99int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock)
100{
ec93a292 101# ifdef USE_RWLOCK
71a04cfc
AG
102 if (pthread_rwlock_unlock(lock) != 0)
103 return 0;
ec93a292 104# else
2accf3f7
DK
105 if (pthread_mutex_unlock(lock) != 0)
106 return 0;
ec93a292 107# endif
71a04cfc
AG
108
109 return 1;
110}
111
112void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock)
113{
114 if (lock == NULL)
115 return;
116
ec93a292 117# ifdef USE_RWLOCK
71a04cfc 118 pthread_rwlock_destroy(lock);
ec93a292 119# else
2accf3f7 120 pthread_mutex_destroy(lock);
ec93a292 121# endif
71a04cfc
AG
122 OPENSSL_free(lock);
123
124 return;
125}
126
127int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))
128{
129 if (pthread_once(once, init) != 0)
130 return 0;
131
132 return 1;
133}
134
135int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL *key, void (*cleanup)(void *))
136{
137 if (pthread_key_create(key, cleanup) != 0)
138 return 0;
139
140 return 1;
141}
142
143void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)
144{
145 return pthread_getspecific(*key);
146}
147
148int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)
149{
150 if (pthread_setspecific(*key, val) != 0)
151 return 0;
152
153 return 1;
154}
155
156int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL *key)
157{
158 if (pthread_key_delete(*key) != 0)
159 return 0;
160
161 return 1;
162}
163
164CRYPTO_THREAD_ID CRYPTO_THREAD_get_current_id(void)
165{
166 return pthread_self();
167}
168
169int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a, CRYPTO_THREAD_ID b)
170{
171 return pthread_equal(a, b);
172}
173
174int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
175{
11fc6c76 176# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
1beca676
RL
177 if (__atomic_is_lock_free(sizeof(*val), val)) {
178 *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
179 return 1;
180 }
d6dda392
VK
181# elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
182 /* This will work for all future Solaris versions. */
183 if (ret != NULL) {
184 *ret = atomic_add_int_nv((volatile unsigned int *)val, amount);
185 return 1;
186 }
1beca676 187# endif
71a04cfc
AG
188 if (!CRYPTO_THREAD_write_lock(lock))
189 return 0;
190
191 *val += amount;
192 *ret = *val;
193
194 if (!CRYPTO_THREAD_unlock(lock))
195 return 0;
71a04cfc
AG
196
197 return 1;
198}
199
f844f9eb 200# ifndef FIPS_MODULE
3593266d 201# ifdef OPENSSL_SYS_UNIX
9750b4d3
RB
202
203# ifndef OPENSSL_NO_DEPRECATED_3_0
204
205void OPENSSL_fork_prepare(void)
206{
207}
208
209void OPENSSL_fork_parent(void)
210{
211}
212
213void OPENSSL_fork_child(void)
214{
215}
216
217# endif
b842fcbb
RS
218static pthread_once_t fork_once_control = PTHREAD_ONCE_INIT;
219
220static void fork_once_func(void)
221{
9750b4d3 222# ifndef OPENSSL_NO_DEPRECATED_3_0
b842fcbb
RS
223 pthread_atfork(OPENSSL_fork_prepare,
224 OPENSSL_fork_parent, OPENSSL_fork_child);
9750b4d3 225# endif
b842fcbb 226}
3593266d 227# endif
b842fcbb 228
2915fe19
RS
229int openssl_init_fork_handlers(void)
230{
3593266d 231# ifdef OPENSSL_SYS_UNIX
b842fcbb 232 if (pthread_once(&fork_once_control, fork_once_func) == 0)
2915fe19 233 return 1;
3593266d 234# endif
2915fe19
RS
235 return 0;
236}
f844f9eb 237# endif /* FIPS_MODULE */
84952925
DMSP
238
239int openssl_get_fork_id(void)
240{
241 return getpid();
242}
71a04cfc 243#endif