xref: /freebsd/crypto/openssl/include/internal/thread_once.h (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3e71b7053SJung-uk Kim  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
8e71b7053SJung-uk Kim  */
9e71b7053SJung-uk Kim 
10*b077aed3SPierre Pronchery #ifndef OSSL_INTERNAL_THREAD_ONCE_H
11*b077aed3SPierre Pronchery # define OSSL_INTERNAL_THREAD_ONCE_H
12*b077aed3SPierre Pronchery # pragma once
13*b077aed3SPierre Pronchery 
14e71b7053SJung-uk Kim # include <openssl/crypto.h>
15e71b7053SJung-uk Kim 
166935a639SJung-uk Kim /*
17*b077aed3SPierre Pronchery  * Initialisation of global data should never happen via "RUN_ONCE" inside the
18*b077aed3SPierre Pronchery  * FIPS module. Global data should instead always be associated with a specific
19*b077aed3SPierre Pronchery  * OSSL_LIB_CTX object. In this way data will get cleaned up correctly when the
20*b077aed3SPierre Pronchery  * module gets unloaded.
21*b077aed3SPierre Pronchery  */
22*b077aed3SPierre Pronchery # if !defined(FIPS_MODULE) || defined(ALLOW_RUN_ONCE_IN_FIPS)
23*b077aed3SPierre Pronchery /*
246935a639SJung-uk Kim  * DEFINE_RUN_ONCE: Define an initialiser function that should be run exactly
25*b077aed3SPierre Pronchery  * once. It takes no arguments and returns an int result (1 for success or
266935a639SJung-uk Kim  * 0 for failure). Typical usage might be:
276935a639SJung-uk Kim  *
286935a639SJung-uk Kim  * DEFINE_RUN_ONCE(myinitfunc)
296935a639SJung-uk Kim  * {
306935a639SJung-uk Kim  *     do_some_initialisation();
316935a639SJung-uk Kim  *     if (init_is_successful())
326935a639SJung-uk Kim  *         return 1;
336935a639SJung-uk Kim  *
346935a639SJung-uk Kim  *     return 0;
356935a639SJung-uk Kim  * }
366935a639SJung-uk Kim  */
37e71b7053SJung-uk Kim #  define DEFINE_RUN_ONCE(init)                   \
38e71b7053SJung-uk Kim     static int init(void);                     \
39e71b7053SJung-uk Kim     int init##_ossl_ret_ = 0;                   \
40e71b7053SJung-uk Kim     void init##_ossl_(void)                     \
41e71b7053SJung-uk Kim     {                                           \
42e71b7053SJung-uk Kim         init##_ossl_ret_ = init();              \
43e71b7053SJung-uk Kim     }                                           \
44e71b7053SJung-uk Kim     static int init(void)
456935a639SJung-uk Kim 
466935a639SJung-uk Kim /*
476935a639SJung-uk Kim  * DECLARE_RUN_ONCE: Declare an initialiser function that should be run exactly
486935a639SJung-uk Kim  * once that has been defined in another file via DEFINE_RUN_ONCE().
496935a639SJung-uk Kim  */
50e71b7053SJung-uk Kim #  define DECLARE_RUN_ONCE(init)                  \
51e71b7053SJung-uk Kim     extern int init##_ossl_ret_;                \
52e71b7053SJung-uk Kim     void init##_ossl_(void);
53e71b7053SJung-uk Kim 
546935a639SJung-uk Kim /*
556935a639SJung-uk Kim  * DEFINE_RUN_ONCE_STATIC: Define an initialiser function that should be run
566935a639SJung-uk Kim  * exactly once. This function will be declared as static within the file. It
57*b077aed3SPierre Pronchery  * takes no arguments and returns an int result (1 for success or 0 for
586935a639SJung-uk Kim  * failure). Typical usage might be:
596935a639SJung-uk Kim  *
606935a639SJung-uk Kim  * DEFINE_RUN_ONCE_STATIC(myinitfunc)
616935a639SJung-uk Kim  * {
626935a639SJung-uk Kim  *     do_some_initialisation();
636935a639SJung-uk Kim  *     if (init_is_successful())
646935a639SJung-uk Kim  *         return 1;
656935a639SJung-uk Kim  *
666935a639SJung-uk Kim  *     return 0;
676935a639SJung-uk Kim  * }
686935a639SJung-uk Kim  */
69e71b7053SJung-uk Kim #  define DEFINE_RUN_ONCE_STATIC(init)            \
70e71b7053SJung-uk Kim     static int init(void);                     \
71e71b7053SJung-uk Kim     static int init##_ossl_ret_ = 0;            \
72e71b7053SJung-uk Kim     static void init##_ossl_(void)              \
73e71b7053SJung-uk Kim     {                                           \
74e71b7053SJung-uk Kim         init##_ossl_ret_ = init();              \
75e71b7053SJung-uk Kim     }                                           \
76e71b7053SJung-uk Kim     static int init(void)
77e71b7053SJung-uk Kim 
78e71b7053SJung-uk Kim /*
796935a639SJung-uk Kim  * DEFINE_RUN_ONCE_STATIC_ALT: Define an alternative initialiser function. This
806935a639SJung-uk Kim  * function will be declared as static within the file. It takes no arguments
816935a639SJung-uk Kim  * and returns an int result (1 for success or 0 for failure). An alternative
826935a639SJung-uk Kim  * initialiser function is expected to be associated with a primary initialiser
836935a639SJung-uk Kim  * function defined via DEFINE_ONCE_STATIC where both functions use the same
846935a639SJung-uk Kim  * CRYPTO_ONCE object to synchronise. Where an alternative initialiser function
856935a639SJung-uk Kim  * is used only one of the primary or the alternative initialiser function will
86da327cd2SJung-uk Kim  * ever be called - and that function will be called exactly once. Definition
876935a639SJung-uk Kim  * of an alternative initialiser function MUST occur AFTER the definition of the
886935a639SJung-uk Kim  * primary initialiser function.
896935a639SJung-uk Kim  *
906935a639SJung-uk Kim  * Typical usage might be:
916935a639SJung-uk Kim  *
926935a639SJung-uk Kim  * DEFINE_RUN_ONCE_STATIC(myinitfunc)
936935a639SJung-uk Kim  * {
946935a639SJung-uk Kim  *     do_some_initialisation();
956935a639SJung-uk Kim  *     if (init_is_successful())
966935a639SJung-uk Kim  *         return 1;
976935a639SJung-uk Kim  *
986935a639SJung-uk Kim  *     return 0;
996935a639SJung-uk Kim  * }
1006935a639SJung-uk Kim  *
1016935a639SJung-uk Kim  * DEFINE_RUN_ONCE_STATIC_ALT(myaltinitfunc, myinitfunc)
1026935a639SJung-uk Kim  * {
1036935a639SJung-uk Kim  *     do_some_alternative_initialisation();
1046935a639SJung-uk Kim  *     if (init_is_successful())
1056935a639SJung-uk Kim  *         return 1;
1066935a639SJung-uk Kim  *
1076935a639SJung-uk Kim  *     return 0;
1086935a639SJung-uk Kim  * }
1096935a639SJung-uk Kim  */
1106935a639SJung-uk Kim #  define DEFINE_RUN_ONCE_STATIC_ALT(initalt, init) \
1116935a639SJung-uk Kim     static int initalt(void);                     \
1126935a639SJung-uk Kim     static void initalt##_ossl_(void)             \
1136935a639SJung-uk Kim     {                                             \
1146935a639SJung-uk Kim         init##_ossl_ret_ = initalt();             \
1156935a639SJung-uk Kim     }                                             \
1166935a639SJung-uk Kim     static int initalt(void)
1176935a639SJung-uk Kim 
1186935a639SJung-uk Kim /*
119e71b7053SJung-uk Kim  * RUN_ONCE - use CRYPTO_THREAD_run_once, and check if the init succeeded
120e71b7053SJung-uk Kim  * @once: pointer to static object of type CRYPTO_ONCE
121e71b7053SJung-uk Kim  * @init: function name that was previously given to DEFINE_RUN_ONCE,
122e71b7053SJung-uk Kim  *        DEFINE_RUN_ONCE_STATIC or DECLARE_RUN_ONCE.  This function
123e71b7053SJung-uk Kim  *        must return 1 for success or 0 for failure.
124e71b7053SJung-uk Kim  *
125e71b7053SJung-uk Kim  * The return value is 1 on success (*) or 0 in case of error.
126e71b7053SJung-uk Kim  *
127e71b7053SJung-uk Kim  * (*) by convention, since the init function must return 1 on success.
128e71b7053SJung-uk Kim  */
129e71b7053SJung-uk Kim #  define RUN_ONCE(once, init)                                            \
130e71b7053SJung-uk Kim     (CRYPTO_THREAD_run_once(once, init##_ossl_) ? init##_ossl_ret_ : 0)
1316935a639SJung-uk Kim 
1326935a639SJung-uk Kim /*
1336935a639SJung-uk Kim  * RUN_ONCE_ALT - use CRYPTO_THREAD_run_once, to run an alternative initialiser
1346935a639SJung-uk Kim  *                function and check if that initialisation succeeded
1356935a639SJung-uk Kim  * @once:    pointer to static object of type CRYPTO_ONCE
1366935a639SJung-uk Kim  * @initalt: alternative initialiser function name that was previously given to
1376935a639SJung-uk Kim  *           DEFINE_RUN_ONCE_STATIC_ALT.  This function must return 1 for
1386935a639SJung-uk Kim  *           success or 0 for failure.
1396935a639SJung-uk Kim  * @init:    primary initialiser function name that was previously given to
1406935a639SJung-uk Kim  *           DEFINE_RUN_ONCE_STATIC.  This function must return 1 for success or
1416935a639SJung-uk Kim  *           0 for failure.
1426935a639SJung-uk Kim  *
1436935a639SJung-uk Kim  * The return value is 1 on success (*) or 0 in case of error.
1446935a639SJung-uk Kim  *
1456935a639SJung-uk Kim  * (*) by convention, since the init function must return 1 on success.
1466935a639SJung-uk Kim  */
1476935a639SJung-uk Kim #  define RUN_ONCE_ALT(once, initalt, init)                               \
1486935a639SJung-uk Kim     (CRYPTO_THREAD_run_once(once, initalt##_ossl_) ? init##_ossl_ret_ : 0)
149*b077aed3SPierre Pronchery 
150*b077aed3SPierre Pronchery # endif /* FIPS_MODULE */
151*b077aed3SPierre Pronchery #endif /* OSSL_INTERNAL_THREAD_ONCE_H */
152