VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.1/include/internal/thread_once.h@ 94082

最後變更 在這個檔案從94082是 94082,由 vboxsync 提交於 3 年 前

libs/openssl-3.0.1: started applying and adjusting our OpenSSL changes to 3.0.1. bugref:10128

檔案大小: 7.4 KB
 
1/*
2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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_ONCE_H
11# define OSSL_INTERNAL_THREAD_ONCE_H
12# pragma once
13
14# include <openssl/crypto.h>
15
16/*
17 * Initialisation of global data should never happen via "RUN_ONCE" inside the
18 * FIPS module. Global data should instead always be associated with a specific
19 * OSSL_LIB_CTX object. In this way data will get cleaned up correctly when the
20 * module gets unloaded.
21 */
22# if !defined(FIPS_MODULE) || defined(ALLOW_RUN_ONCE_IN_FIPS)
23# ifndef VBOX
24/*
25 * DEFINE_RUN_ONCE: Define an initialiser function that should be run exactly
26 * once. It takes no arguments and returns an int result (1 for success or
27 * 0 for failure). Typical usage might be:
28 *
29 * DEFINE_RUN_ONCE(myinitfunc)
30 * {
31 * do_some_initialisation();
32 * if (init_is_successful())
33 * return 1;
34 *
35 * return 0;
36 * }
37 */
38# define DEFINE_RUN_ONCE(init) \
39 static int init(void); \
40 int init##_ossl_ret_ = 0; \
41 void init##_ossl_(void) \
42 { \
43 init##_ossl_ret_ = init(); \
44 } \
45 static int init(void)
46
47/*
48 * DECLARE_RUN_ONCE: Declare an initialiser function that should be run exactly
49 * once that has been defined in another file via DEFINE_RUN_ONCE().
50 */
51# define DECLARE_RUN_ONCE(init) \
52 extern int init##_ossl_ret_; \
53 void init##_ossl_(void);
54
55/*
56 * DEFINE_RUN_ONCE_STATIC: Define an initialiser function that should be run
57 * exactly once. This function will be declared as static within the file. It
58 * takes no arguments and returns an int result (1 for success or 0 for
59 * failure). Typical usage might be:
60 *
61 * DEFINE_RUN_ONCE_STATIC(myinitfunc)
62 * {
63 * do_some_initialisation();
64 * if (init_is_successful())
65 * return 1;
66 *
67 * return 0;
68 * }
69 */
70# define DEFINE_RUN_ONCE_STATIC(init) \
71 static int init(void); \
72 static int init##_ossl_ret_ = 0; \
73 static void init##_ossl_(void) \
74 { \
75 init##_ossl_ret_ = init(); \
76 } \
77 static int init(void)
78
79/*
80 * DEFINE_RUN_ONCE_STATIC_ALT: Define an alternative initialiser function. This
81 * function will be declared as static within the file. It takes no arguments
82 * and returns an int result (1 for success or 0 for failure). An alternative
83 * initialiser function is expected to be associated with a primary initialiser
84 * function defined via DEFINE_ONCE_STATIC where both functions use the same
85 * CRYPTO_ONCE object to synchronise. Where an alternative initialiser function
86 * is used only one of the primary or the alternative initialiser function will
87 * ever be called - and that function will be called exactly once. Definition
88 * of an alternative initialiser function MUST occur AFTER the definition of the
89 * primary initialiser function.
90 *
91 * Typical usage might be:
92 *
93 * DEFINE_RUN_ONCE_STATIC(myinitfunc)
94 * {
95 * do_some_initialisation();
96 * if (init_is_successful())
97 * return 1;
98 *
99 * return 0;
100 * }
101 *
102 * DEFINE_RUN_ONCE_STATIC_ALT(myaltinitfunc, myinitfunc)
103 * {
104 * do_some_alternative_initialisation();
105 * if (init_is_successful())
106 * return 1;
107 *
108 * return 0;
109 * }
110 */
111# define DEFINE_RUN_ONCE_STATIC_ALT(initalt, init) \
112 static int initalt(void); \
113 static void initalt##_ossl_(void) \
114 { \
115 init##_ossl_ret_ = initalt(); \
116 } \
117 static int initalt(void)
118
119#else /* VBOX */
120
121/*
122 * PFNRTONCE returns an IPRT status code but the OpenSSL once functions
123 * return 1 for success and 0 for failure. We need to translate between
124 * these errors back (here) and force (in RUN_ONCE()).
125 */
126# undef DEFINE_RUN_ONCE /* currently unused */
127# undef DECLARE_RUN_ONCE /* currently unused */
128# undef DEFINE_RUN_ONCE_STATIC_ALT /* currently unused */
129
130# define DEFINE_RUN_ONCE_STATIC(init) \
131 static int init(void); \
132 static int init##_ossl_ret_ = 0; \
133 static DECLCALLBACK(int) init##_ossl_(void *pvUser) \
134 { \
135 init##_ossl_ret_ = init(); \
136 return init##_ossl_ret_ ? VINF_SUCCESS : VERR_INTERNAL_ERROR; \
137 } \
138 static int init(void)
139
140# define DEFINE_RUN_ONCE_STATIC_ALT(initalt, init) \
141 static int initalt(void); \
142 static DECLCALLBACK(int) initalt##_ossl_(void *pvUser) \
143 { \
144 init##_ossl_ret_ = initalt(); \
145 return init##_ossl_ret_ ? VINF_SUCCESS : VERR_INTERNAL_ERROR; \
146 } \
147 static int initalt(void)
148
149#endif /* VBOX */
150
151/*
152 * RUN_ONCE - use CRYPTO_THREAD_run_once, and check if the init succeeded
153 * @once: pointer to static object of type CRYPTO_ONCE
154 * @init: function name that was previously given to DEFINE_RUN_ONCE,
155 * DEFINE_RUN_ONCE_STATIC or DECLARE_RUN_ONCE. This function
156 * must return 1 for success or 0 for failure.
157 *
158 * The return value is 1 on success (*) or 0 in case of error.
159 *
160 * (*) by convention, since the init function must return 1 on success.
161 */
162# ifndef VBOX
163# define RUN_ONCE(once, init) \
164 (CRYPTO_THREAD_run_once(once, init##_ossl_) ? init##_ossl_ret_ : 0)
165
166/*
167 * RUN_ONCE_ALT - use CRYPTO_THREAD_run_once, to run an alternative initialiser
168 * function and check if that initialisation succeeded
169 * @once: pointer to static object of type CRYPTO_ONCE
170 * @initalt: alternative initialiser function name that was previously given to
171 * DEFINE_RUN_ONCE_STATIC_ALT. This function must return 1 for
172 * success or 0 for failure.
173 * @init: primary initialiser function name that was previously given to
174 * DEFINE_RUN_ONCE_STATIC. This function must return 1 for success or
175 * 0 for failure.
176 *
177 * The return value is 1 on success (*) or 0 in case of error.
178 *
179 * (*) by convention, since the init function must return 1 on success.
180 */
181# define RUN_ONCE_ALT(once, initalt, init) \
182 (CRYPTO_THREAD_run_once(once, initalt##_ossl_) ? init##_ossl_ret_ : 0)
183# else
184# define RUN_ONCE(once, init) \
185 (RT_SUCCESS_NP(RTOnce(once, init##_ossl_, NULL)) ? init##_ossl_ret_ : 0)
186
187# define RUN_ONCE_ALT(once, initalt, init) \
188 (RT_SUCCESS_NP(RTOnce(once, initalt##_ossl_, NULL)) ? init##_ossl_ret_ : 0)
189# endif /* VBOX */
190
191# endif /* FIPS_MODULE */
192#endif /* OSSL_INTERNAL_THREAD_ONCE_H */
193
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette