]> git.ipfire.org Git - thirdparty/openssl.git/blame - include/internal/thread_arch.h
Copyright year updates
[thirdparty/openssl.git] / include / internal / thread_arch.h
CommitLineData
4574a7fd 1/*
b6461792 2 * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
4574a7fd
ČK
3 *
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
8 */
9
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>
2b2b2678 14# include "internal/time.h"
4574a7fd
ČK
15
16# if defined(_WIN32)
17# include <windows.h>
18# endif
19
20# if defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_UNIX)
21# define OPENSSL_THREADS_POSIX
ac21c178
RL
22# elif defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_VMS)
23# define OPENSSL_THREADS_POSIX
4574a7fd
ČK
24# elif defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_WINDOWS) && \
25 defined(_WIN32_WINNT)
26# if _WIN32_WINNT >= 0x0600
27# define OPENSSL_THREADS_WINNT
1dd04a0f
HL
28# elif _WIN32_WINNT >= 0x0501
29# define OPENSSL_THREADS_WINNT
30# define OPENSSL_THREADS_WINNT_LEGACY
4574a7fd
ČK
31# else
32# define OPENSSL_THREADS_NONE
33# endif
34# else
35# define OPENSSL_THREADS_NONE
36# endif
37
38# include <openssl/crypto.h>
39
62cb7c81
HL
40typedef struct crypto_mutex_st CRYPTO_MUTEX;
41typedef struct crypto_condvar_st CRYPTO_CONDVAR;
4574a7fd
ČK
42
43CRYPTO_MUTEX *ossl_crypto_mutex_new(void);
44void ossl_crypto_mutex_lock(CRYPTO_MUTEX *mutex);
45int ossl_crypto_mutex_try_lock(CRYPTO_MUTEX *mutex);
46void ossl_crypto_mutex_unlock(CRYPTO_MUTEX *mutex);
47void ossl_crypto_mutex_free(CRYPTO_MUTEX **mutex);
48
49CRYPTO_CONDVAR *ossl_crypto_condvar_new(void);
50void ossl_crypto_condvar_wait(CRYPTO_CONDVAR *cv, CRYPTO_MUTEX *mutex);
2b2b2678
HL
51void ossl_crypto_condvar_wait_timeout(CRYPTO_CONDVAR *cv, CRYPTO_MUTEX *mutex,
52 OSSL_TIME deadline);
4574a7fd 53void ossl_crypto_condvar_broadcast(CRYPTO_CONDVAR *cv);
1dd04a0f 54void ossl_crypto_condvar_signal(CRYPTO_CONDVAR *cv);
4574a7fd
ČK
55void ossl_crypto_condvar_free(CRYPTO_CONDVAR **cv);
56
57typedef uint32_t CRYPTO_THREAD_RETVAL;
58typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE)(void *);
59typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE_CB)(void *,
60 void (**)(void *),
61 void **);
62
63# define CRYPTO_THREAD_NO_STATE 0UL
4e43bc06
ČK
64# define CRYPTO_THREAD_FINISHED (1UL << 0)
65# define CRYPTO_THREAD_JOIN_AWAIT (1UL << 1)
4574a7fd 66# define CRYPTO_THREAD_JOINED (1UL << 2)
4574a7fd
ČK
67
68# define CRYPTO_THREAD_GET_STATE(THREAD, FLAG) ((THREAD)->state & (FLAG))
69# define CRYPTO_THREAD_GET_ERROR(THREAD, FLAG) (((THREAD)->state >> 16) & (FLAG))
70
71typedef struct crypto_thread_st {
72 uint32_t state;
73 void *data;
74 CRYPTO_THREAD_ROUTINE routine;
75 CRYPTO_THREAD_RETVAL retval;
76 void *handle;
77 CRYPTO_MUTEX *lock;
78 CRYPTO_MUTEX *statelock;
79 CRYPTO_CONDVAR *condvar;
80 unsigned long thread_id;
81 int joinable;
82 OSSL_LIB_CTX *ctx;
83} CRYPTO_THREAD;
84
85# if defined(OPENSSL_THREADS)
86
87# define CRYPTO_THREAD_UNSET_STATE(THREAD, FLAG) \
88 do { \
89 (THREAD)->state &= ~(FLAG); \
90 } while ((void)0, 0)
91
92# define CRYPTO_THREAD_SET_STATE(THREAD, FLAG) \
93 do { \
94 (THREAD)->state |= (FLAG); \
95 } while ((void)0, 0)
96
97# define CRYPTO_THREAD_SET_ERROR(THREAD, FLAG) \
98 do { \
99 (THREAD)->state |= ((FLAG) << 16); \
100 } while ((void)0, 0)
101
102# define CRYPTO_THREAD_UNSET_ERROR(THREAD, FLAG) \
103 do { \
104 (THREAD)->state &= ~((FLAG) << 16); \
105 } while ((void)0, 0)
106
107# else
108
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)
113
114# endif /* defined(OPENSSL_THREADS) */
115
116CRYPTO_THREAD * ossl_crypto_thread_native_start(CRYPTO_THREAD_ROUTINE routine,
117 void *data, int joinable);
118int ossl_crypto_thread_native_spawn(CRYPTO_THREAD *thread);
119int ossl_crypto_thread_native_join(CRYPTO_THREAD *thread,
120 CRYPTO_THREAD_RETVAL *retval);
4e43bc06
ČK
121int ossl_crypto_thread_native_perform_join(CRYPTO_THREAD *thread,
122 CRYPTO_THREAD_RETVAL *retval);
4574a7fd
ČK
123int ossl_crypto_thread_native_exit(void);
124int ossl_crypto_thread_native_is_self(CRYPTO_THREAD *thread);
125int ossl_crypto_thread_native_clean(CRYPTO_THREAD *thread);
126
4574a7fd 127#endif /* OSSL_INTERNAL_THREAD_ARCH_H */