threading.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * \file threading.h
  3. *
  4. * \brief Threading abstraction layer
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. */
  10. #ifndef MBEDTLS_THREADING_H
  11. #define MBEDTLS_THREADING_H
  12. #include "mbedtls/private_access.h"
  13. #include "mbedtls/build_info.h"
  14. #include <stdlib.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /** Bad input parameters to function. */
  19. #define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA -0x001C
  20. /** Locking / unlocking / free failed with error code. */
  21. #define MBEDTLS_ERR_THREADING_MUTEX_ERROR -0x001E
  22. #if defined(MBEDTLS_THREADING_PTHREAD)
  23. #include <pthread.h>
  24. typedef struct mbedtls_threading_mutex_t {
  25. pthread_mutex_t MBEDTLS_PRIVATE(mutex);
  26. /* WARNING - state should only be accessed when holding the mutex lock in
  27. * framework/tests/src/threading_helpers.c, otherwise corruption can occur.
  28. * state will be 0 after a failed init or a free, and nonzero after a
  29. * successful init. This field is for testing only and thus not considered
  30. * part of the public API of Mbed TLS and may change without notice.*/
  31. char MBEDTLS_PRIVATE(state);
  32. } mbedtls_threading_mutex_t;
  33. #endif
  34. #if defined(MBEDTLS_THREADING_ALT)
  35. /* You should define the mbedtls_threading_mutex_t type in your header */
  36. #include "threading_alt.h"
  37. /**
  38. * \brief Set your alternate threading implementation function
  39. * pointers and initialize global mutexes. If used, this
  40. * function must be called once in the main thread before any
  41. * other Mbed TLS function is called, and
  42. * mbedtls_threading_free_alt() must be called once in the main
  43. * thread after all other Mbed TLS functions.
  44. *
  45. * \warning \p mutex_init and \p mutex_free don't return a status code.
  46. * If \p mutex_init fails, it should leave the mutex in
  47. * a state such that \p mutex_lock will reliably return
  48. * #MBEDTLS_ERR_THREADING_MUTEX_ERROR called on this mutex,
  49. * and \p mutex_free will do nothing.
  50. *
  51. * \param mutex_init The init function implementation. <br>
  52. * The behavior is undefined if the mutex is already
  53. * initialized and has not been destroyed.
  54. * On platforms where mutex initialization can fail,
  55. * since this function does not return a status code,
  56. * it must leave the mutex object in a safe state where
  57. * subsequent function calls will not cause undefined
  58. * behavior: after a call to \p mutex_init, the
  59. * function \p mutex_lock must either succeed or
  60. * fail with a nonzero status code, and the function
  61. * \p mutex_free must free any resources associated
  62. * with the mutex..
  63. * \param mutex_free The destroy function implementation. <br>
  64. * This function must free any resources associated
  65. * with the mutex object. <br>
  66. * This function must work reliably if \p mutex_init
  67. * has been called on the mutex and \p mutex_free
  68. * has not yet been called. <br>
  69. * The behavior is undefined if the mutex was not
  70. * initialized, if it has already been destroyed,
  71. * if it is currently locked, or if this function
  72. * is called concurrently from multiple threads.
  73. * \param mutex_lock The lock function implementation. <br>
  74. * This function must work reliably on any mutex
  75. * which is not currently locked and on which
  76. * \p mutex_init has already been called but
  77. * \p mutex_free has not been called yet. <br>
  78. * The behavior is undefined if the mutex was not
  79. * initialized, if it has already been destroyed, or if
  80. * it is currently locked by the calling thread.
  81. * \param mutex_unlock The unlock function implementation. <br>
  82. * The behavior is undefined if the mutex is not
  83. * currently locked by the calling thread.
  84. */
  85. void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
  86. void (*mutex_free)(mbedtls_threading_mutex_t *),
  87. int (*mutex_lock)(mbedtls_threading_mutex_t *),
  88. int (*mutex_unlock)(mbedtls_threading_mutex_t *));
  89. /**
  90. * \brief Free global mutexes.
  91. */
  92. void mbedtls_threading_free_alt(void);
  93. #endif /* MBEDTLS_THREADING_ALT */
  94. #if defined(MBEDTLS_THREADING_C)
  95. /*
  96. * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock
  97. *
  98. * All these functions are expected to work or the result will be undefined.
  99. */
  100. extern void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *mutex);
  101. extern void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *mutex);
  102. extern int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *mutex);
  103. extern int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *mutex);
  104. /*
  105. * Global mutexes
  106. */
  107. #if defined(MBEDTLS_FS_IO)
  108. extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
  109. #endif
  110. #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
  111. /* This mutex may or may not be used in the default definition of
  112. * mbedtls_platform_gmtime_r(), but in order to determine that,
  113. * we need to check POSIX features, hence modify _POSIX_C_SOURCE.
  114. * With the current approach, this declaration is orphaned, lacking
  115. * an accompanying definition, in case mbedtls_platform_gmtime_r()
  116. * doesn't need it, but that's not a problem. */
  117. extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
  118. #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
  119. #if defined(MBEDTLS_PSA_CRYPTO_C)
  120. /*
  121. * A mutex used to make the PSA subsystem thread safe.
  122. *
  123. * key_slot_mutex protects the registered_readers and
  124. * state variable for all key slots in &global_data.key_slots.
  125. *
  126. * This mutex must be held when any read from or write to a state or
  127. * registered_readers field is performed, i.e. when calling functions:
  128. * psa_key_slot_state_transition(), psa_register_read(), psa_unregister_read(),
  129. * psa_key_slot_has_readers() and psa_wipe_key_slot(). */
  130. extern mbedtls_threading_mutex_t mbedtls_threading_key_slot_mutex;
  131. /*
  132. * A mutex used to make the non-rng PSA global_data struct members thread safe.
  133. *
  134. * This mutex must be held when reading or writing to any of the PSA global_data
  135. * structure members, other than the rng_state or rng struct. */
  136. extern mbedtls_threading_mutex_t mbedtls_threading_psa_globaldata_mutex;
  137. /*
  138. * A mutex used to make the PSA global_data rng data thread safe.
  139. *
  140. * This mutex must be held when reading or writing to the PSA
  141. * global_data rng_state or rng struct members. */
  142. extern mbedtls_threading_mutex_t mbedtls_threading_psa_rngdata_mutex;
  143. #endif
  144. #endif /* MBEDTLS_THREADING_C */
  145. #ifdef __cplusplus
  146. }
  147. #endif
  148. #endif /* threading.h */