2 * Copyright 2016-2017 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
14 #include <openssl/crypto.h>
15 #include "test_main.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 (!TEST_true(CRYPTO_THREAD_read_lock(lock))
89 || !TEST_true(CRYPTO_THREAD_unlock(lock)))
92 CRYPTO_THREAD_lock_free(lock);
97 static CRYPTO_ONCE once_run = CRYPTO_ONCE_STATIC_INIT;
98 static unsigned once_run_count = 0;
100 static void once_do_run(void)
105 static void once_run_thread_cb(void)
107 CRYPTO_THREAD_run_once(&once_run, once_do_run);
110 static int test_once(void)
114 if (!TEST_true(run_thread(&thread, once_run_thread_cb))
115 || !TEST_true(wait_for_thread(thread))
116 || !CRYPTO_THREAD_run_once(&once_run, once_do_run)
117 || !TEST_int_eq(once_run_count, 1))
122 static CRYPTO_THREAD_LOCAL thread_local_key;
123 static unsigned destructor_run_count = 0;
124 static int thread_local_thread_cb_ok = 0;
126 static void thread_local_destructor(void *arg)
138 static void thread_local_thread_cb(void)
142 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
143 if (!TEST_ptr_null(ptr)
144 || !TEST_true(CRYPTO_THREAD_set_local(&thread_local_key,
145 &destructor_run_count)))
148 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
149 if (!TEST_ptr_eq(ptr, &destructor_run_count))
152 thread_local_thread_cb_ok = 1;
155 static int test_thread_local(void)
160 if (!TEST_true(CRYPTO_THREAD_init_local(&thread_local_key,
161 thread_local_destructor)))
164 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
165 if (!TEST_ptr_null(ptr)
166 || !TEST_true(run_thread(&thread, thread_local_thread_cb))
167 || !TEST_true(wait_for_thread(thread))
168 || !TEST_int_eq(thread_local_thread_cb_ok, 1))
171 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
173 ptr = CRYPTO_THREAD_get_local(&thread_local_key);
174 if (!TEST_ptr_null(ptr))
177 # if !defined(OPENSSL_SYS_WINDOWS)
178 if (!TEST_int_eq(destructor_run_count, 1))
183 if (!TEST_true(CRYPTO_THREAD_cleanup_local(&thread_local_key)))
188 void register_tests(void)
192 ADD_TEST(test_thread_local);