thread_once.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #ifndef OSSL_INTERNAL_THREAD_ONCE_H
  10. # define OSSL_INTERNAL_THREAD_ONCE_H
  11. # pragma once
  12. # include <openssl/crypto.h>
  13. /*
  14. * Initialisation of global data should never happen via "RUN_ONCE" inside the
  15. * FIPS module. Global data should instead always be associated with a specific
  16. * OSSL_LIB_CTX object. In this way data will get cleaned up correctly when the
  17. * module gets unloaded.
  18. */
  19. # if !defined(FIPS_MODULE) || defined(ALLOW_RUN_ONCE_IN_FIPS)
  20. /*
  21. * DEFINE_RUN_ONCE: Define an initialiser function that should be run exactly
  22. * once. It takes no arguments and returns an int result (1 for success or
  23. * 0 for failure). Typical usage might be:
  24. *
  25. * DEFINE_RUN_ONCE(myinitfunc)
  26. * {
  27. * do_some_initialisation();
  28. * if (init_is_successful())
  29. * return 1;
  30. *
  31. * return 0;
  32. * }
  33. */
  34. # define DEFINE_RUN_ONCE(init) \
  35. static int init(void); \
  36. int init##_ossl_ret_ = 0; \
  37. void init##_ossl_(void) \
  38. { \
  39. init##_ossl_ret_ = init(); \
  40. } \
  41. static int init(void)
  42. /*
  43. * DECLARE_RUN_ONCE: Declare an initialiser function that should be run exactly
  44. * once that has been defined in another file via DEFINE_RUN_ONCE().
  45. */
  46. # define DECLARE_RUN_ONCE(init) \
  47. extern int init##_ossl_ret_; \
  48. void init##_ossl_(void);
  49. /*
  50. * DEFINE_RUN_ONCE_STATIC: Define an initialiser function that should be run
  51. * exactly once. This function will be declared as static within the file. It
  52. * takes no arguments and returns an int result (1 for success or 0 for
  53. * failure). Typical usage might be:
  54. *
  55. * DEFINE_RUN_ONCE_STATIC(myinitfunc)
  56. * {
  57. * do_some_initialisation();
  58. * if (init_is_successful())
  59. * return 1;
  60. *
  61. * return 0;
  62. * }
  63. */
  64. # define DEFINE_RUN_ONCE_STATIC(init) \
  65. static int init(void); \
  66. static int init##_ossl_ret_ = 0; \
  67. static void init##_ossl_(void) \
  68. { \
  69. init##_ossl_ret_ = init(); \
  70. } \
  71. static int init(void)
  72. /*
  73. * DEFINE_RUN_ONCE_STATIC_ALT: Define an alternative initialiser function. This
  74. * function will be declared as static within the file. It takes no arguments
  75. * and returns an int result (1 for success or 0 for failure). An alternative
  76. * initialiser function is expected to be associated with a primary initialiser
  77. * function defined via DEFINE_ONCE_STATIC where both functions use the same
  78. * CRYPTO_ONCE object to synchronise. Where an alternative initialiser function
  79. * is used only one of the primary or the alternative initialiser function will
  80. * ever be called - and that function will be called exactly once. Definition
  81. * of an alternative initialiser function MUST occur AFTER the definition of the
  82. * primary initialiser function.
  83. *
  84. * Typical usage might be:
  85. *
  86. * DEFINE_RUN_ONCE_STATIC(myinitfunc)
  87. * {
  88. * do_some_initialisation();
  89. * if (init_is_successful())
  90. * return 1;
  91. *
  92. * return 0;
  93. * }
  94. *
  95. * DEFINE_RUN_ONCE_STATIC_ALT(myaltinitfunc, myinitfunc)
  96. * {
  97. * do_some_alternative_initialisation();
  98. * if (init_is_successful())
  99. * return 1;
  100. *
  101. * return 0;
  102. * }
  103. */
  104. # define DEFINE_RUN_ONCE_STATIC_ALT(initalt, init) \
  105. static int initalt(void); \
  106. static void initalt##_ossl_(void) \
  107. { \
  108. init##_ossl_ret_ = initalt(); \
  109. } \
  110. static int initalt(void)
  111. /*
  112. * RUN_ONCE - use CRYPTO_THREAD_run_once, and check if the init succeeded
  113. * @once: pointer to static object of type CRYPTO_ONCE
  114. * @init: function name that was previously given to DEFINE_RUN_ONCE,
  115. * DEFINE_RUN_ONCE_STATIC or DECLARE_RUN_ONCE. This function
  116. * must return 1 for success or 0 for failure.
  117. *
  118. * The return value is 1 on success (*) or 0 in case of error.
  119. *
  120. * (*) by convention, since the init function must return 1 on success.
  121. */
  122. # define RUN_ONCE(once, init) \
  123. (CRYPTO_THREAD_run_once(once, init##_ossl_) ? init##_ossl_ret_ : 0)
  124. /*
  125. * RUN_ONCE_ALT - use CRYPTO_THREAD_run_once, to run an alternative initialiser
  126. * function and check if that initialisation succeeded
  127. * @once: pointer to static object of type CRYPTO_ONCE
  128. * @initalt: alternative initialiser function name that was previously given to
  129. * DEFINE_RUN_ONCE_STATIC_ALT. This function must return 1 for
  130. * success or 0 for failure.
  131. * @init: primary initialiser function name that was previously given to
  132. * DEFINE_RUN_ONCE_STATIC. This function must return 1 for success or
  133. * 0 for failure.
  134. *
  135. * The return value is 1 on success (*) or 0 in case of error.
  136. *
  137. * (*) by convention, since the init function must return 1 on success.
  138. */
  139. # define RUN_ONCE_ALT(once, initalt, init) \
  140. (CRYPTO_THREAD_run_once(once, initalt##_ossl_) ? init##_ossl_ret_ : 0)
  141. # endif /* FIPS_MODULE */
  142. #endif /* OSSL_INTERNAL_THREAD_ONCE_H */