2 * Copyright 2016-2018 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"
13 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
15 CRYPTO_RWLOCK
*CRYPTO_THREAD_lock_new(void)
19 if ((lock
= OPENSSL_zalloc(sizeof(unsigned int))) == NULL
) {
20 /* Don't set error, to avoid recursion blowup. */
24 *(unsigned int *)lock
= 1;
29 int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK
*lock
)
31 if (!ossl_assert(*(unsigned int *)lock
== 1))
36 int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK
*lock
)
38 if (!ossl_assert(*(unsigned int *)lock
== 1))
43 int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK
*lock
)
45 if (!ossl_assert(*(unsigned int *)lock
== 1))
50 void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK
*lock
) {
54 *(unsigned int *)lock
= 0;
60 int CRYPTO_THREAD_run_once(CRYPTO_ONCE
*once
, void (*init
)(void))
71 #define OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX 256
73 static void *thread_local_storage
[OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
];
75 int CRYPTO_THREAD_init_local(CRYPTO_THREAD_LOCAL
*key
, void (*cleanup
)(void *))
77 static unsigned int thread_local_key
= 0;
79 if (thread_local_key
>= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
)
82 *key
= thread_local_key
++;
84 thread_local_storage
[*key
] = NULL
;
89 void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL
*key
)
91 if (*key
>= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
)
94 return thread_local_storage
[*key
];
97 int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL
*key
, void *val
)
99 if (*key
>= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
)
102 thread_local_storage
[*key
] = val
;
107 int CRYPTO_THREAD_cleanup_local(CRYPTO_THREAD_LOCAL
*key
)
109 *key
= OPENSSL_CRYPTO_THREAD_LOCAL_KEY_MAX
+ 1;
113 CRYPTO_THREAD_ID
CRYPTO_THREAD_get_current_id(void)
118 int CRYPTO_THREAD_compare_id(CRYPTO_THREAD_ID a
, CRYPTO_THREAD_ID b
)
123 int CRYPTO_atomic_add(int *val
, int amount
, int *ret
, CRYPTO_RWLOCK
*lock
)
131 int openssl_init_fork_handlers(void)