thread_once.h 5.1 KB

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