2
0

thread_arch.h 4.7 KB

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