2 * Copyright 2016-2020 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 #include <openssl/crypto.h>
11 #include "internal/cryptlib.h"
17 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG) && !defined(OPENSSL_SYS_WINDOWS)
19 # if defined(OPENSSL_SYS_UNIX)
20 # include <sys/types.h>
24 # ifdef PTHREAD_RWLOCK_INITIALIZER
28 CRYPTO_RWLOCK
*CRYPTO_THREAD_lock_new(void)
33 if ((lock
= OPENSSL_zalloc(sizeof(pthread_rwlock_t
))) == NULL
) {
34 /* Don't set error, to avoid recursion blowup. */
38 if (pthread_rwlock_init(lock
, NULL
) != 0) {
43 pthread_mutexattr_t attr
;
46 if ((lock
= OPENSSL_zalloc(sizeof(pthread_mutex_t
))) == NULL
) {
47 /* Don't set error, to avoid recursion blowup. */
51 pthread_mutexattr_init(&attr
);
52 #if defined(__TANDEM) && defined(_SPT_MODEL_)
53 pthread_mutexattr_setkind_np(&attr
,MUTEX_RECURSIVE_NP
);
55 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
58 if (pthread_mutex_init(lock
, &attr
) != 0) {
59 pthread_mutexattr_destroy(&attr
);
64 pthread_mutexattr_destroy(&attr
);
70 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK
*lock
)
73 if (pthread_rwlock_rdlock(lock
) != 0)
76 if (pthread_mutex_lock(lock
) != 0)
83 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK
*lock
)
86 if (pthread_rwlock_wrlock(lock
) != 0)
89 if (pthread_mutex_lock(lock
) != 0)
96 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK
*lock
)
99 if (pthread_rwlock_unlock(lock
) != 0)
102 if (pthread_mutex_unlock(lock
) != 0)
109 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK
*lock
)
115 pthread_rwlock_destroy(lock
);
117 pthread_mutex_destroy(lock
);
124 int CRYPTO_THREAD_run_once(CRYPTO_ONCE
*once
, void (*init
)(void))
126 if (pthread_once(once
, init
) != 0)
132 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL
*key
, void (*cleanup
)(void *))
134 if (pthread_key_create(key
, cleanup
) != 0)
140 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL
*key
)
142 return pthread_getspecific(*key
);
145 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL
*key
, void *val
)
147 if (pthread_setspecific(*key
, val
) != 0)
153 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL
*key
)
155 if (pthread_key_delete(*key
) != 0)
161 CRYPTO_THREAD_ID
CRYPTO_THREAD_get_current_id(void)
163 return pthread_self();
166 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a
, CRYPTO_THREAD_ID b
)
168 return pthread_equal(a
, b
);
171 int CRYPTO_atomic_add(int *val
, int amount
, int *ret
, CRYPTO_RWLOCK
*lock
)
173 # if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
174 if (__atomic_is_lock_free(sizeof(*val
), val
)) {
175 *ret
= __atomic_add_fetch(val
, amount
, __ATOMIC_ACQ_REL
);
178 # elif defined(__sun) && (defined(__SunOS_5_10) || defined(__SunOS_5_11))
179 /* This will work for all future Solaris versions. */
181 *ret
= atomic_add_int_nv((volatile unsigned int *)val
, amount
);
185 if (!CRYPTO_THREAD_write_lock(lock
))
191 if (!CRYPTO_THREAD_unlock(lock
))
198 # ifdef OPENSSL_SYS_UNIX
199 static pthread_once_t fork_once_control
= PTHREAD_ONCE_INIT
;
201 static void fork_once_func(void)
203 pthread_atfork(OPENSSL_fork_prepare
,
204 OPENSSL_fork_parent
, OPENSSL_fork_child
);
208 int openssl_init_fork_handlers(void)
210 # ifdef OPENSSL_SYS_UNIX
211 if (pthread_once(&fork_once_control
, fork_once_func
) == 0)
216 # endif /* FIPS_MODULE */
218 int openssl_get_fork_id(void)