Blame include/internal/thread_once.h

Packit c4476c
/*
Packit c4476c
 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
 *
Packit c4476c
 * Licensed under the OpenSSL license (the "License").  You may not use
Packit c4476c
 * this file except in compliance with the License.  You can obtain a copy
Packit c4476c
 * in the file LICENSE in the source distribution or at
Packit c4476c
 * https://www.openssl.org/source/license.html
Packit c4476c
 */
Packit c4476c
Packit c4476c
#include <openssl/crypto.h>
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * DEFINE_RUN_ONCE: Define an initialiser function that should be run exactly
Packit c4476c
 * once. It takes no arguments and returns and int result (1 for success or
Packit c4476c
 * 0 for failure). Typical usage might be:
Packit c4476c
 *
Packit c4476c
 * DEFINE_RUN_ONCE(myinitfunc)
Packit c4476c
 * {
Packit c4476c
 *     do_some_initialisation();
Packit c4476c
 *     if (init_is_successful())
Packit c4476c
 *         return 1;
Packit c4476c
 *
Packit c4476c
 *     return 0;
Packit c4476c
 * }
Packit c4476c
 */
Packit c4476c
#define DEFINE_RUN_ONCE(init)                   \
Packit c4476c
    static int init(void);                     \
Packit c4476c
    int init##_ossl_ret_ = 0;                   \
Packit c4476c
    void init##_ossl_(void)                     \
Packit c4476c
    {                                           \
Packit c4476c
        init##_ossl_ret_ = init();              \
Packit c4476c
    }                                           \
Packit c4476c
    static int init(void)
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * DECLARE_RUN_ONCE: Declare an initialiser function that should be run exactly
Packit c4476c
 * once that has been defined in another file via DEFINE_RUN_ONCE().
Packit c4476c
 */
Packit c4476c
#define DECLARE_RUN_ONCE(init)                  \
Packit c4476c
    extern int init##_ossl_ret_;                \
Packit c4476c
    void init##_ossl_(void);
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * DEFINE_RUN_ONCE_STATIC: Define an initialiser function that should be run
Packit c4476c
 * exactly once. This function will be declared as static within the file. It
Packit c4476c
 * takes no arguments and returns and int result (1 for success or 0 for
Packit c4476c
 * failure). Typical usage might be:
Packit c4476c
 *
Packit c4476c
 * DEFINE_RUN_ONCE_STATIC(myinitfunc)
Packit c4476c
 * {
Packit c4476c
 *     do_some_initialisation();
Packit c4476c
 *     if (init_is_successful())
Packit c4476c
 *         return 1;
Packit c4476c
 *
Packit c4476c
 *     return 0;
Packit c4476c
 * }
Packit c4476c
 */
Packit c4476c
#define DEFINE_RUN_ONCE_STATIC(init)            \
Packit c4476c
    static int init(void);                     \
Packit c4476c
    static int init##_ossl_ret_ = 0;            \
Packit c4476c
    static void init##_ossl_(void)              \
Packit c4476c
    {                                           \
Packit c4476c
        init##_ossl_ret_ = init();              \
Packit c4476c
    }                                           \
Packit c4476c
    static int init(void)
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * DEFINE_RUN_ONCE_STATIC_ALT: Define an alternative initialiser function. This
Packit c4476c
 * function will be declared as static within the file. It takes no arguments
Packit c4476c
 * and returns an int result (1 for success or 0 for failure). An alternative
Packit c4476c
 * initialiser function is expected to be associated with a primary initialiser
Packit c4476c
 * function defined via DEFINE_ONCE_STATIC where both functions use the same
Packit c4476c
 * CRYPTO_ONCE object to synchronise. Where an alternative initialiser function
Packit c4476c
 * is used only one of the primary or the alternative initialiser function will
Packit c4476c
 * ever be called - and that function will be called exactly once. Definition
Packit c4476c
 * of an alternative initialiser function MUST occur AFTER the definition of the
Packit c4476c
 * primary initialiser function.
Packit c4476c
 *
Packit c4476c
 * Typical usage might be:
Packit c4476c
 *
Packit c4476c
 * DEFINE_RUN_ONCE_STATIC(myinitfunc)
Packit c4476c
 * {
Packit c4476c
 *     do_some_initialisation();
Packit c4476c
 *     if (init_is_successful())
Packit c4476c
 *         return 1;
Packit c4476c
 *
Packit c4476c
 *     return 0;
Packit c4476c
 * }
Packit c4476c
 *
Packit c4476c
 * DEFINE_RUN_ONCE_STATIC_ALT(myaltinitfunc, myinitfunc)
Packit c4476c
 * {
Packit c4476c
 *     do_some_alternative_initialisation();
Packit c4476c
 *     if (init_is_successful())
Packit c4476c
 *         return 1;
Packit c4476c
 *
Packit c4476c
 *     return 0;
Packit c4476c
 * }
Packit c4476c
 */
Packit c4476c
#define DEFINE_RUN_ONCE_STATIC_ALT(initalt, init) \
Packit c4476c
    static int initalt(void);                     \
Packit c4476c
    static void initalt##_ossl_(void)             \
Packit c4476c
    {                                             \
Packit c4476c
        init##_ossl_ret_ = initalt();             \
Packit c4476c
    }                                             \
Packit c4476c
    static int initalt(void)
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * RUN_ONCE - use CRYPTO_THREAD_run_once, and check if the init succeeded
Packit c4476c
 * @once: pointer to static object of type CRYPTO_ONCE
Packit c4476c
 * @init: function name that was previously given to DEFINE_RUN_ONCE,
Packit c4476c
 *        DEFINE_RUN_ONCE_STATIC or DECLARE_RUN_ONCE.  This function
Packit c4476c
 *        must return 1 for success or 0 for failure.
Packit c4476c
 *
Packit c4476c
 * The return value is 1 on success (*) or 0 in case of error.
Packit c4476c
 *
Packit c4476c
 * (*) by convention, since the init function must return 1 on success.
Packit c4476c
 */
Packit c4476c
#define RUN_ONCE(once, init)                                            \
Packit c4476c
    (CRYPTO_THREAD_run_once(once, init##_ossl_) ? init##_ossl_ret_ : 0)
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * RUN_ONCE_ALT - use CRYPTO_THREAD_run_once, to run an alternative initialiser
Packit c4476c
 *                function and check if that initialisation succeeded
Packit c4476c
 * @once:    pointer to static object of type CRYPTO_ONCE
Packit c4476c
 * @initalt: alternative initialiser function name that was previously given to
Packit c4476c
 *           DEFINE_RUN_ONCE_STATIC_ALT.  This function must return 1 for
Packit c4476c
 *           success or 0 for failure.
Packit c4476c
 * @init:    primary initialiser function name that was previously given to
Packit c4476c
 *           DEFINE_RUN_ONCE_STATIC.  This function must return 1 for success or
Packit c4476c
 *           0 for failure.
Packit c4476c
 *
Packit c4476c
 * The return value is 1 on success (*) or 0 in case of error.
Packit c4476c
 *
Packit c4476c
 * (*) by convention, since the init function must return 1 on success.
Packit c4476c
 */
Packit c4476c
#define RUN_ONCE_ALT(once, initalt, init)                               \
Packit c4476c
    (CRYPTO_THREAD_run_once(once, initalt##_ossl_) ? init##_ossl_ret_ : 0)