thread_arch.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2019-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_ARCH_H
  10. # define OSSL_INTERNAL_THREAD_ARCH_H
  11. # include <openssl/configuration.h>
  12. # include <openssl/e_os2.h>
  13. # if defined(_WIN32)
  14. # include <windows.h>
  15. # endif
  16. # if defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_UNIX)
  17. # define OPENSSL_THREADS_POSIX
  18. # elif defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_WINDOWS) && \
  19. defined(_WIN32_WINNT)
  20. # if _WIN32_WINNT >= 0x0600
  21. # define OPENSSL_THREADS_WINNT
  22. # else
  23. # define OPENSSL_THREADS_NONE
  24. # endif
  25. # else
  26. # define OPENSSL_THREADS_NONE
  27. # endif
  28. # include <openssl/crypto.h>
  29. typedef void CRYPTO_MUTEX;
  30. typedef void CRYPTO_CONDVAR;
  31. CRYPTO_MUTEX *ossl_crypto_mutex_new(void);
  32. void ossl_crypto_mutex_lock(CRYPTO_MUTEX *mutex);
  33. int ossl_crypto_mutex_try_lock(CRYPTO_MUTEX *mutex);
  34. void ossl_crypto_mutex_unlock(CRYPTO_MUTEX *mutex);
  35. void ossl_crypto_mutex_free(CRYPTO_MUTEX **mutex);
  36. CRYPTO_CONDVAR *ossl_crypto_condvar_new(void);
  37. void ossl_crypto_condvar_wait(CRYPTO_CONDVAR *cv, CRYPTO_MUTEX *mutex);
  38. void ossl_crypto_condvar_broadcast(CRYPTO_CONDVAR *cv);
  39. void ossl_crypto_condvar_free(CRYPTO_CONDVAR **cv);
  40. typedef uint32_t CRYPTO_THREAD_RETVAL;
  41. typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE)(void *);
  42. typedef CRYPTO_THREAD_RETVAL (*CRYPTO_THREAD_ROUTINE_CB)(void *,
  43. void (**)(void *),
  44. void **);
  45. # define CRYPTO_THREAD_NO_STATE 0UL
  46. # define CRYPTO_THREAD_FINISHED (1UL << 0)
  47. # define CRYPTO_THREAD_JOIN_AWAIT (1UL << 1)
  48. # define CRYPTO_THREAD_JOINED (1UL << 2)
  49. # define CRYPTO_THREAD_GET_STATE(THREAD, FLAG) ((THREAD)->state & (FLAG))
  50. # define CRYPTO_THREAD_GET_ERROR(THREAD, FLAG) (((THREAD)->state >> 16) & (FLAG))
  51. typedef struct crypto_thread_st {
  52. uint32_t state;
  53. void *data;
  54. CRYPTO_THREAD_ROUTINE routine;
  55. CRYPTO_THREAD_RETVAL retval;
  56. void *handle;
  57. CRYPTO_MUTEX *lock;
  58. CRYPTO_MUTEX *statelock;
  59. CRYPTO_CONDVAR *condvar;
  60. unsigned long thread_id;
  61. int joinable;
  62. OSSL_LIB_CTX *ctx;
  63. } CRYPTO_THREAD;
  64. # if defined(OPENSSL_THREADS)
  65. # define CRYPTO_THREAD_UNSET_STATE(THREAD, FLAG) \
  66. do { \
  67. (THREAD)->state &= ~(FLAG); \
  68. } while ((void)0, 0)
  69. # define CRYPTO_THREAD_SET_STATE(THREAD, FLAG) \
  70. do { \
  71. (THREAD)->state |= (FLAG); \
  72. } while ((void)0, 0)
  73. # define CRYPTO_THREAD_SET_ERROR(THREAD, FLAG) \
  74. do { \
  75. (THREAD)->state |= ((FLAG) << 16); \
  76. } while ((void)0, 0)
  77. # define CRYPTO_THREAD_UNSET_ERROR(THREAD, FLAG) \
  78. do { \
  79. (THREAD)->state &= ~((FLAG) << 16); \
  80. } while ((void)0, 0)
  81. # else
  82. # define CRYPTO_THREAD_UNSET_STATE(THREAD, FLAG)
  83. # define CRYPTO_THREAD_SET_STATE(THREAD, FLAG)
  84. # define CRYPTO_THREAD_SET_ERROR(THREAD, FLAG)
  85. # define CRYPTO_THREAD_UNSET_ERROR(THREAD, FLAG)
  86. # endif /* defined(OPENSSL_THREADS) */
  87. CRYPTO_THREAD * ossl_crypto_thread_native_start(CRYPTO_THREAD_ROUTINE routine,
  88. void *data, int joinable);
  89. int ossl_crypto_thread_native_spawn(CRYPTO_THREAD *thread);
  90. int ossl_crypto_thread_native_join(CRYPTO_THREAD *thread,
  91. CRYPTO_THREAD_RETVAL *retval);
  92. int ossl_crypto_thread_native_perform_join(CRYPTO_THREAD *thread,
  93. CRYPTO_THREAD_RETVAL *retval);
  94. int ossl_crypto_thread_native_exit(void);
  95. int ossl_crypto_thread_native_is_self(CRYPTO_THREAD *thread);
  96. int ossl_crypto_thread_native_clean(CRYPTO_THREAD *thread);
  97. void ossl_crypto_mem_barrier(void);
  98. #endif /* OSSL_INTERNAL_THREAD_ARCH_H */