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_VMS)
22 # define OPENSSL_THREADS_POSIX
23 # elif defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_WINDOWS) && \
25 # if _WIN32_WINNT >= 0x0600
26 # define OPENSSL_THREADS_WINNT
28 # define OPENSSL_THREADS_NONE
31 # define OPENSSL_THREADS_NONE
34 # include <openssl/crypto.h>
36 typedef void CRYPTO_MUTEX
;
37 typedef void CRYPTO_CONDVAR
;
39 CRYPTO_MUTEX
*ossl_crypto_mutex_new(void);
40 void ossl_crypto_mutex_lock(CRYPTO_MUTEX
*mutex
);
41 int ossl_crypto_mutex_try_lock(CRYPTO_MUTEX
*mutex
);
42 void ossl_crypto_mutex_unlock(CRYPTO_MUTEX
*mutex
);
43 void ossl_crypto_mutex_free(CRYPTO_MUTEX
**mutex
);
45 CRYPTO_CONDVAR
*ossl_crypto_condvar_new(void);
46 void ossl_crypto_condvar_wait(CRYPTO_CONDVAR
*cv
, CRYPTO_MUTEX
*mutex
);
47 void ossl_crypto_condvar_broadcast(CRYPTO_CONDVAR
*cv
);
48 void ossl_crypto_condvar_free(CRYPTO_CONDVAR
**cv
);
50 typedef uint32_t CRYPTO_THREAD_RETVAL
;
51 typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE
)(void *);
52 typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE_CB
)(void *,
56 # define CRYPTO_THREAD_NO_STATE 0UL
57 # define CRYPTO_THREAD_FINISHED (1UL << 0)
58 # define CRYPTO_THREAD_JOIN_AWAIT (1UL << 1)
59 # define CRYPTO_THREAD_JOINED (1UL << 2)
61 # define CRYPTO_THREAD_GET_STATE(THREAD, FLAG) ((THREAD)->state & (FLAG))
62 # define CRYPTO_THREAD_GET_ERROR(THREAD, FLAG) (((THREAD)->state >> 16) & (FLAG))
64 typedef struct crypto_thread_st
{
67 CRYPTO_THREAD_ROUTINE routine
;
68 CRYPTO_THREAD_RETVAL retval
;
71 CRYPTO_MUTEX
*statelock
;
72 CRYPTO_CONDVAR
*condvar
;
73 unsigned long thread_id
;
78 # if defined(OPENSSL_THREADS)
80 # define CRYPTO_THREAD_UNSET_STATE(THREAD, FLAG) \
82 (THREAD)->state &= ~(FLAG); \
85 # define CRYPTO_THREAD_SET_STATE(THREAD, FLAG) \
87 (THREAD)->state |= (FLAG); \
90 # define CRYPTO_THREAD_SET_ERROR(THREAD, FLAG) \
92 (THREAD)->state |= ((FLAG) << 16); \
95 # define CRYPTO_THREAD_UNSET_ERROR(THREAD, FLAG) \
97 (THREAD)->state &= ~((FLAG) << 16); \
102 # define CRYPTO_THREAD_UNSET_STATE(THREAD, FLAG)
103 # define CRYPTO_THREAD_SET_STATE(THREAD, FLAG)
104 # define CRYPTO_THREAD_SET_ERROR(THREAD, FLAG)
105 # define CRYPTO_THREAD_UNSET_ERROR(THREAD, FLAG)
107 # endif /* defined(OPENSSL_THREADS) */
109 CRYPTO_THREAD
* ossl_crypto_thread_native_start(CRYPTO_THREAD_ROUTINE routine
,
110 void *data
, int joinable
);
111 int ossl_crypto_thread_native_spawn(CRYPTO_THREAD
*thread
);
112 int ossl_crypto_thread_native_join(CRYPTO_THREAD
*thread
,
113 CRYPTO_THREAD_RETVAL
*retval
);
114 int ossl_crypto_thread_native_perform_join(CRYPTO_THREAD
*thread
,
115 CRYPTO_THREAD_RETVAL
*retval
);
116 int ossl_crypto_thread_native_exit(void);
117 int ossl_crypto_thread_native_is_self(CRYPTO_THREAD
*thread
);
118 int ossl_crypto_thread_native_clean(CRYPTO_THREAD
*thread
);
120 #endif /* OSSL_INTERNAL_THREAD_ARCH_H */