2
0

thread_once.h 5.4 KB

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