thread_once.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright 1995-2016 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. #define DEFINE_RUN_ONCE(init) \
  11. static int init(void); \
  12. int init##_ossl_ret_ = 0; \
  13. void init##_ossl_(void) \
  14. { \
  15. init##_ossl_ret_ = init(); \
  16. } \
  17. static int init(void)
  18. #define DECLARE_RUN_ONCE(init) \
  19. extern int init##_ossl_ret_; \
  20. void init##_ossl_(void);
  21. #define DEFINE_RUN_ONCE_STATIC(init) \
  22. static int init(void); \
  23. static int init##_ossl_ret_ = 0; \
  24. static void init##_ossl_(void) \
  25. { \
  26. init##_ossl_ret_ = init(); \
  27. } \
  28. static int init(void)
  29. /*
  30. * RUN_ONCE - use CRYPTO_THREAD_run_once, and check if the init succeeded
  31. * @once: pointer to static object of type CRYPTO_ONCE
  32. * @init: function name that was previously given to DEFINE_RUN_ONCE,
  33. * DEFINE_RUN_ONCE_STATIC or DECLARE_RUN_ONCE. This function
  34. * must return 1 for success or 0 for failure.
  35. *
  36. * The return value is 1 on success (*) or 0 in case of error.
  37. *
  38. * (*) by convention, since the init function must return 1 on success.
  39. */
  40. #define RUN_ONCE(once, init) \
  41. (CRYPTO_THREAD_run_once(once, init##_ossl_) ? init##_ossl_ret_ : 0)