2 * Copyright 2019-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 #ifndef OSSL_INTERNAL_THREAD_ARCH_H
11 # define OSSL_INTERNAL_THREAD_ARCH_H
12 # include <openssl/configuration.h>
13 # include <openssl/e_os2.h>
19 # if defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_UNIX)
20 # define OPENSSL_THREADS_POSIX
21 # elif defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_WINDOWS) && \
23 # if _WIN32_WINNT >= 0x0600
24 # define OPENSSL_THREADS_WINNT
26 # define OPENSSL_THREADS_NONE
29 # define OPENSSL_THREADS_NONE
32 # include <openssl/crypto.h>
34 typedef void CRYPTO_MUTEX;
35 typedef void CRYPTO_CONDVAR;
37 CRYPTO_MUTEX *ossl_crypto_mutex_new(void);
38 void ossl_crypto_mutex_lock(CRYPTO_MUTEX *mutex);
39 int ossl_crypto_mutex_try_lock(CRYPTO_MUTEX *mutex);
40 void ossl_crypto_mutex_unlock(CRYPTO_MUTEX *mutex);
41 void ossl_crypto_mutex_free(CRYPTO_MUTEX **mutex);
43 CRYPTO_CONDVAR *ossl_crypto_condvar_new(void);
44 void ossl_crypto_condvar_wait(CRYPTO_CONDVAR *cv, CRYPTO_MUTEX *mutex);
45 void ossl_crypto_condvar_broadcast(CRYPTO_CONDVAR *cv);
46 void ossl_crypto_condvar_free(CRYPTO_CONDVAR **cv);
48 typedef uint32_t CRYPTO_THREAD_RETVAL;
49 typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE)(void *);
50 typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE_CB)(void *,
54 # define CRYPTO_THREAD_NO_STATE 0UL
55 # define CRYPTO_THREAD_FINISHED (1UL << 1)
56 # define CRYPTO_THREAD_JOINED (1UL << 2)
57 # define CRYPTO_THREAD_TERMINATED (1UL << 3)
59 # define CRYPTO_THREAD_GET_STATE(THREAD, FLAG) ((THREAD)->state & (FLAG))
60 # define CRYPTO_THREAD_GET_ERROR(THREAD, FLAG) (((THREAD)->state >> 16) & (FLAG))
62 typedef struct crypto_thread_st {
65 CRYPTO_THREAD_ROUTINE routine;
66 CRYPTO_THREAD_RETVAL retval;
69 CRYPTO_MUTEX *statelock;
70 CRYPTO_CONDVAR *condvar;
71 unsigned long thread_id;
76 # if defined(OPENSSL_THREADS)
78 # define CRYPTO_THREAD_UNSET_STATE(THREAD, FLAG) \
80 (THREAD)->state &= ~(FLAG); \
83 # define CRYPTO_THREAD_SET_STATE(THREAD, FLAG) \
85 (THREAD)->state |= (FLAG); \
88 # define CRYPTO_THREAD_SET_ERROR(THREAD, FLAG) \
90 (THREAD)->state |= ((FLAG) << 16); \
93 # define CRYPTO_THREAD_UNSET_ERROR(THREAD, FLAG) \
95 (THREAD)->state &= ~((FLAG) << 16); \
100 # define CRYPTO_THREAD_UNSET_STATE(THREAD, FLAG)
101 # define CRYPTO_THREAD_SET_STATE(THREAD, FLAG)
102 # define CRYPTO_THREAD_SET_ERROR(THREAD, FLAG)
103 # define CRYPTO_THREAD_UNSET_ERROR(THREAD, FLAG)
105 # endif /* defined(OPENSSL_THREADS) */
107 CRYPTO_THREAD * ossl_crypto_thread_native_start(CRYPTO_THREAD_ROUTINE routine,
108 void *data, int joinable);
109 int ossl_crypto_thread_native_spawn(CRYPTO_THREAD *thread);
110 int ossl_crypto_thread_native_join(CRYPTO_THREAD *thread,
111 CRYPTO_THREAD_RETVAL *retval);
112 int ossl_crypto_thread_native_terminate(CRYPTO_THREAD *thread);
113 int ossl_crypto_thread_native_exit(void);
114 int ossl_crypto_thread_native_is_self(CRYPTO_THREAD *thread);
115 int ossl_crypto_thread_native_clean(CRYPTO_THREAD *thread);
117 void ossl_crypto_mem_barrier(void);
119 #endif /* OSSL_INTERNAL_THREAD_ARCH_H */