2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 /* We need to use the OPENSSL_fork_*() deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include <openssl/crypto.h>
14 #include "internal/cryptlib.h"
20 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
22 # if defined(OPENSSL_SYS_UNIX)
23 # include <sys/types.h>
27 # ifdef PTHREAD_RWLOCK_INITIALIZER
31 CRYPTO_RWLOCK
*CRYPTO_THREAD_lock_new(void)
36 if ((lock
= OPENSSL_zalloc(sizeof(pthread_rwlock_t
))) == NULL
) {
37 /* Don't set error, to avoid recursion blowup. */
41 if (pthread_rwlock_init(lock
, NULL
) != 0) {
46 pthread_mutexattr_t attr
;
49 if ((lock
= OPENSSL_zalloc(sizeof(pthread_mutex_t
))) == NULL
) {
50 /* Don't set error, to avoid recursion blowup. */
55 * We don't use recursive mutexes, but try to catch errors if we do.
57 pthread_mutexattr_init(&attr
);
58 # if defined(NDEBUG) && defined(PTHREAD_MUTEX_ERRORCHECK)
59 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_ERRORCHECK
);
61 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_NORMAL
);
64 if (pthread_mutex_init(lock
, &attr
) != 0) {
65 pthread_mutexattr_destroy(&attr
);
70 pthread_mutexattr_destroy(&attr
);
76 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK
*lock
)
79 if (pthread_rwlock_rdlock(lock
) != 0)
82 if (pthread_mutex_lock(lock
) != 0) {
83 assert(errno
!= EDEADLK
&& errno
!= EBUSY
);
91 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK
*lock
)
94 if (pthread_rwlock_wrlock(lock
) != 0)
97 if (pthread_mutex_lock(lock
) != 0) {
98 assert(errno
!= EDEADLK
&& errno
!= EBUSY
);
106 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK
*lock
)
109 if (pthread_rwlock_unlock(lock
) != 0)
112 if (pthread_mutex_unlock(lock
) != 0) {
113 assert(errno
!= EPERM
);
121 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK
*lock
)
127 pthread_rwlock_destroy(lock
);
129 pthread_mutex_destroy(lock
);
136 int CRYPTO_THREAD_run_once(CRYPTO_ONCE
*once
, void (*init
)(void))
138 if (pthread_once(once
, init
) != 0)
144 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL
*key
, void (*cleanup
)(void *))
146 if (pthread_key_create(key
, cleanup
) != 0)
152 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL
*key
)
154 return pthread_getspecific(*key
);
157 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL
*key
, void *val
)
159 if (pthread_setspecific(*key
, val
) != 0)
165 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL
*key
)
167 if (pthread_key_delete(*key
) != 0)
173 CRYPTO_THREAD_ID
CRYPTO_THREAD_get_current_id(void)
175 return pthread_self();
178 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a
, CRYPTO_THREAD_ID b
)
180 return pthread_equal(a
, b
);
183 int CRYPTO_atomic_add(int *val
, int amount
, int *ret
, CRYPTO_RWLOCK
*lock
)
185 # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
186 if (__atomic_is_lock_free(sizeof(*val
), val
)) {
187 *ret
= __atomic_add_fetch(val
, amount
, __ATOMIC_ACQ_REL
);
190 # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
191 /* This will work for all future Solaris versions. */
193 *ret
= atomic_add_int_nv((volatile unsigned int *)val
, amount
);
197 if (lock
== NULL
|| !CRYPTO_THREAD_write_lock(lock
))
203 if (!CRYPTO_THREAD_unlock(lock
))
209 int CRYPTO_atomic_or(uint64_t *val
, uint64_t op
, uint64_t *ret
,
212 # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
213 if (__atomic_is_lock_free(sizeof(*val
), val
)) {
214 *ret
= __atomic_or_fetch(val
, op
, __ATOMIC_ACQ_REL
);
217 # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
218 /* This will work for all future Solaris versions. */
220 *ret
= atomic_or_64_nv(val
, op
);
224 if (lock
== NULL
|| !CRYPTO_THREAD_write_lock(lock
))
229 if (!CRYPTO_THREAD_unlock(lock
))
235 int CRYPTO_atomic_load(uint64_t *val
, uint64_t *ret
, CRYPTO_RWLOCK
*lock
)
237 # if defined(__GNUC__) && defined(__ATOMIC_ACQUIRE)
238 if (__atomic_is_lock_free(sizeof(*val
), val
)) {
239 __atomic_load(val
, ret
, __ATOMIC_ACQUIRE
);
242 # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
243 /* This will work for all future Solaris versions. */
245 *ret
= atomic_or_64_nv(val
, 0);
249 if (lock
== NULL
|| !CRYPTO_THREAD_read_lock(lock
))
252 if (!CRYPTO_THREAD_unlock(lock
))
258 # ifdef OPENSSL_SYS_UNIX
260 static pthread_once_t fork_once_control
= PTHREAD_ONCE_INIT
;
262 static void fork_once_func(void)
264 # ifndef OPENSSL_NO_DEPRECATED_3_0
265 pthread_atfork(OPENSSL_fork_prepare
,
266 OPENSSL_fork_parent
, OPENSSL_fork_child
);
271 int openssl_init_fork_handlers(void)
273 # ifdef OPENSSL_SYS_UNIX
274 if (pthread_once(&fork_once_control
, fork_once_func
) == 0)
279 # endif /* FIPS_MODULE */
281 int openssl_get_fork_id(void)