2 * Copyright 2019-2023 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>
14 # include "internal/time.h"
20 # if defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_UNIX)
21 # define OPENSSL_THREADS_POSIX
22 # elif defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_VMS)
23 # define OPENSSL_THREADS_POSIX
24 # elif defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_WINDOWS) && \
26 # if _WIN32_WINNT >= 0x0600
27 # define OPENSSL_THREADS_WINNT
28 # elif _WIN32_WINNT >= 0x0501
29 # define OPENSSL_THREADS_WINNT
30 # define OPENSSL_THREADS_WINNT_LEGACY
32 # define OPENSSL_THREADS_NONE
35 # define OPENSSL_THREADS_NONE
38 # include <openssl/crypto.h>
40 typedef void CRYPTO_MUTEX;
41 typedef void CRYPTO_CONDVAR;
43 CRYPTO_MUTEX *ossl_crypto_mutex_new(void);
44 void ossl_crypto_mutex_lock(CRYPTO_MUTEX *mutex);
45 int ossl_crypto_mutex_try_lock(CRYPTO_MUTEX *mutex);
46 void ossl_crypto_mutex_unlock(CRYPTO_MUTEX *mutex);
47 void ossl_crypto_mutex_free(CRYPTO_MUTEX **mutex);
49 CRYPTO_CONDVAR *ossl_crypto_condvar_new(void);
50 void ossl_crypto_condvar_wait(CRYPTO_CONDVAR *cv, CRYPTO_MUTEX *mutex);
51 void ossl_crypto_condvar_wait_timeout(CRYPTO_CONDVAR *cv, CRYPTO_MUTEX *mutex,
53 void ossl_crypto_condvar_broadcast(CRYPTO_CONDVAR *cv);
54 void ossl_crypto_condvar_signal(CRYPTO_CONDVAR *cv);
55 void ossl_crypto_condvar_free(CRYPTO_CONDVAR **cv);
57 typedef uint32_t CRYPTO_THREAD_RETVAL;
58 typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE)(void *);
59 typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE_CB)(void *,
63 # define CRYPTO_THREAD_NO_STATE 0UL
64 # define CRYPTO_THREAD_FINISHED (1UL << 0)
65 # define CRYPTO_THREAD_JOIN_AWAIT (1UL << 1)
66 # define CRYPTO_THREAD_JOINED (1UL << 2)
68 # define CRYPTO_THREAD_GET_STATE(THREAD, FLAG) ((THREAD)->state & (FLAG))
69 # define CRYPTO_THREAD_GET_ERROR(THREAD, FLAG) (((THREAD)->state >> 16) & (FLAG))
71 typedef struct crypto_thread_st {
74 CRYPTO_THREAD_ROUTINE routine;
75 CRYPTO_THREAD_RETVAL retval;
78 CRYPTO_MUTEX *statelock;
79 CRYPTO_CONDVAR *condvar;
80 unsigned long thread_id;
85 # if defined(OPENSSL_THREADS)
87 # define CRYPTO_THREAD_UNSET_STATE(THREAD, FLAG) \
89 (THREAD)->state &= ~(FLAG); \
92 # define CRYPTO_THREAD_SET_STATE(THREAD, FLAG) \
94 (THREAD)->state |= (FLAG); \
97 # define CRYPTO_THREAD_SET_ERROR(THREAD, FLAG) \
99 (THREAD)->state |= ((FLAG) << 16); \
102 # define CRYPTO_THREAD_UNSET_ERROR(THREAD, FLAG) \
104 (THREAD)->state &= ~((FLAG) << 16); \
109 # define CRYPTO_THREAD_UNSET_STATE(THREAD, FLAG)
110 # define CRYPTO_THREAD_SET_STATE(THREAD, FLAG)
111 # define CRYPTO_THREAD_SET_ERROR(THREAD, FLAG)
112 # define CRYPTO_THREAD_UNSET_ERROR(THREAD, FLAG)
114 # endif /* defined(OPENSSL_THREADS) */
116 CRYPTO_THREAD * ossl_crypto_thread_native_start(CRYPTO_THREAD_ROUTINE routine,
117 void *data, int joinable);
118 int ossl_crypto_thread_native_spawn(CRYPTO_THREAD *thread);
119 int ossl_crypto_thread_native_join(CRYPTO_THREAD *thread,
120 CRYPTO_THREAD_RETVAL *retval);
121 int ossl_crypto_thread_native_perform_join(CRYPTO_THREAD *thread,
122 CRYPTO_THREAD_RETVAL *retval);
123 int ossl_crypto_thread_native_exit(void);
124 int ossl_crypto_thread_native_is_self(CRYPTO_THREAD *thread);
125 int ossl_crypto_thread_native_clean(CRYPTO_THREAD *thread);
127 #endif /* OSSL_INTERNAL_THREAD_ARCH_H */