2
0

thread_once.h 5.0 KB

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