2 * Copyright 1995-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_ONCE_H
11 # define OSSL_INTERNAL_THREAD_ONCE_H
14 # include <openssl/crypto.h>
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.
22 # if !defined(FIPS_MODULE) || defined(ALLOW_RUN_ONCE_IN_FIPS)
24 * DEFINE_RUN_ONCE: Define an initialiser function that should be run exactly
25 * once. It takes no arguments and returns an int result (1 for success or
26 * 0 for failure). Typical usage might be:
28 * DEFINE_RUN_ONCE(myinitfunc)
30 * do_some_initialisation();
31 * if (init_is_successful())
37 # define DEFINE_RUN_ONCE(init) \
38 static int init(void); \
39 int init##_ossl_ret_ = 0; \
40 void init##_ossl_(void) \
42 init##_ossl_ret_ = init(); \
47 * DECLARE_RUN_ONCE: Declare an initialiser function that should be run exactly
48 * once that has been defined in another file via DEFINE_RUN_ONCE().
50 # define DECLARE_RUN_ONCE(init) \
51 extern int init##_ossl_ret_; \
52 void init##_ossl_(void);
55 * DEFINE_RUN_ONCE_STATIC: Define an initialiser function that should be run
56 * exactly once. This function will be declared as static within the file. It
57 * takes no arguments and returns an int result (1 for success or 0 for
58 * failure). Typical usage might be:
60 * DEFINE_RUN_ONCE_STATIC(myinitfunc)
62 * do_some_initialisation();
63 * if (init_is_successful())
69 # define DEFINE_RUN_ONCE_STATIC(init) \
70 static int init(void); \
71 static int init##_ossl_ret_ = 0; \
72 static void init##_ossl_(void) \
74 init##_ossl_ret_ = init(); \
79 * DEFINE_RUN_ONCE_STATIC_ALT: Define an alternative initialiser function. This
80 * function will be declared as static within the file. It takes no arguments
81 * and returns an int result (1 for success or 0 for failure). An alternative
82 * initialiser function is expected to be associated with a primary initialiser
83 * function defined via DEFINE_ONCE_STATIC where both functions use the same
84 * CRYPTO_ONCE object to synchronise. Where an alternative initialiser function
85 * is used only one of the primary or the alternative initialiser function will
86 * ever be called - and that function will be called exactly once. Definition
87 * of an alternative initialiser function MUST occur AFTER the definition of the
88 * primary initialiser function.
90 * Typical usage might be:
92 * DEFINE_RUN_ONCE_STATIC(myinitfunc)
94 * do_some_initialisation();
95 * if (init_is_successful())
101 * DEFINE_RUN_ONCE_STATIC_ALT(myaltinitfunc, myinitfunc)
103 * do_some_alternative_initialisation();
104 * if (init_is_successful())
110 # define DEFINE_RUN_ONCE_STATIC_ALT(initalt, init) \
111 static int initalt(void); \
112 static void initalt##_ossl_(void) \
114 init##_ossl_ret_ = initalt(); \
116 static int initalt(void)
119 * RUN_ONCE - use CRYPTO_THREAD_run_once, and check if the init succeeded
120 * @once: pointer to static object of type CRYPTO_ONCE
121 * @init: function name that was previously given to DEFINE_RUN_ONCE,
122 * DEFINE_RUN_ONCE_STATIC or DECLARE_RUN_ONCE. This function
123 * must return 1 for success or 0 for failure.
125 * The return value is 1 on success (*) or 0 in case of error.
127 * (*) by convention, since the init function must return 1 on success.
129 # define RUN_ONCE(once, init) \
130 (CRYPTO_THREAD_run_once(once, init##_ossl_) ? init##_ossl_ret_ : 0)
133 * RUN_ONCE_ALT - use CRYPTO_THREAD_run_once, to run an alternative initialiser
134 * function and check if that initialisation succeeded
135 * @once: pointer to static object of type CRYPTO_ONCE
136 * @initalt: alternative initialiser function name that was previously given to
137 * DEFINE_RUN_ONCE_STATIC_ALT. This function must return 1 for
138 * success or 0 for failure.
139 * @init: primary initialiser function name that was previously given to
140 * DEFINE_RUN_ONCE_STATIC. This function must return 1 for success or
143 * The return value is 1 on success (*) or 0 in case of error.
145 * (*) by convention, since the init function must return 1 on success.
147 # define RUN_ONCE_ALT(once, initalt, init) \
148 (CRYPTO_THREAD_run_once(once, initalt##_ossl_) ? init##_ossl_ret_ : 0)
150 # endif /* FIPS_MODULE */
151 #endif /* OSSL_INTERNAL_THREAD_ONCE_H */