2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
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
16 #include <openssl/crypto.h>
18 #if !defined(OPENSSL_THREADS) || defined(CRYPTO_TDEBUG)
20 typedef unsigned int thread_t;
22 static int run_thread(thread_t *t, void (*f)(void))
28 static int wait_for_thread(thread_t thread)
33 #elif defined(OPENSSL_SYS_WINDOWS)
35 typedef HANDLE thread_t;
37 static DWORD WINAPI thread_run(LPVOID arg)
41 *(void **) (&f) = arg;
47 static int run_thread(thread_t *t, void (*f)(void))
49 *t = CreateThread(NULL, 0, thread_run, *(void **) &f, 0, NULL);
53 static int wait_for_thread(thread_t thread)
55 return WaitForSingleObject(thread, INFINITE) == 0;
60 typedef pthread_t thread_t;
62 static void *thread_run(void *arg)
66 *(void **) (&f) = arg;
72 static int run_thread(thread_t *t, void (*f)(void))
74 return pthread_create(t, NULL, thread_run, *(void **) &f) == 0;
77 static int wait_for_thread(thread_t thread)
79 return pthread_join(thread, NULL) == 0;
84 static int test_lock(void)
86 CRYPTO_RWLOCK *lock = CRYPTO_THREAD_lock_new();
88 if (!CRYPTO_THREAD_read_lock(lock)) {
89 fprintf(stderr, "CRYPTO_THREAD_read_lock() failed\n");
93 if (!CRYPTO_THREAD_unlock(lock)) {
94 fprintf(stderr, "CRYPTO_THREAD_unlock() failed\n");
98 CRYPTO_THREAD_lock_free(lock);
103 static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
104 static unsigned once_run_count = 0;
106 static void once_do_run(void)
111 static void once_run_thread_cb(void)
113 CRYPTO_THREAD_run_once(&once_run, once_do_run);
116 static int test_once(void)
119 if (!run_thread(&thread, once_run_thread_cb) ||
120 !wait_for_thread(thread))
122 fprintf(stderr, "run_thread() failed\n");
126 if (!CRYPTO_THREAD_run_once(&once_run, once_do_run)) {
127 fprintf(stderr, "CRYPTO_THREAD_run_once() failed\n");
131 if (once_run_count != 1) {
132 fprintf(stderr, "once run %u times\n", once_run_count);
139 static CRYPTO_THREAD_LOCAL thread_local_key;
140 static unsigned destructor_run_count = 0;
141 static int thread_local_thread_cb_ok = 0;
143 static void thread_local_destructor(void *arg)
155 static void thread_local_thread_cb(void)
159 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
161 fprintf(stderr, "ptr not NULL\n");
165 if (!CRYPTO_THREAD_set_local(&thread_local_key, &destructor_run_count)) {
166 fprintf(stderr, "CRYPTO_THREAD_set_local() failed\n");
170 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
171 if (ptr != &destructor_run_count) {
172 fprintf(stderr, "invalid ptr\n");
176 thread_local_thread_cb_ok = 1;
179 static int test_thread_local(void)
184 if (!CRYPTO_THREAD_init_local(&thread_local_key, thread_local_destructor)) {
185 fprintf(stderr, "CRYPTO_THREAD_init_local() failed\n");
189 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
191 fprintf(stderr, "ptr not NULL\n");
195 if (!run_thread(&thread, thread_local_thread_cb) ||
196 !wait_for_thread(thread))
198 fprintf(stderr, "run_thread() failed\n");
202 if (thread_local_thread_cb_ok != 1) {
203 fprintf(stderr, "thread-local thread callback failed\n");
207 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
209 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
211 fprintf(stderr, "ptr not NULL\n");
215 # if !defined(OPENSSL_SYS_WINDOWS)
216 if (destructor_run_count != 1) {
217 fprintf(stderr, "thread-local destructor run %u times\n",
218 destructor_run_count);
225 if (!CRYPTO_THREAD_cleanup_local(&thread_local_key)) {
226 fprintf(stderr, "CRYPTO_THREAD_cleanup_local() failed\n");
233 int main(int argc, char **argv)
241 if (!test_thread_local())