threading.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Threading abstraction layer
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * Ensure gmtime_r is available even with -std=c99; must be defined before
  21. * config.h, which pulls in glibc's features.h. Harmless on other platforms.
  22. */
  23. #if !defined(_POSIX_C_SOURCE)
  24. #define _POSIX_C_SOURCE 200112L
  25. #endif
  26. #include "common.h"
  27. #if defined(MBEDTLS_THREADING_C)
  28. #include "mbedtls/threading.h"
  29. #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
  30. #if !defined(_WIN32) && (defined(unix) || \
  31. defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
  32. defined(__MACH__)))
  33. #include <unistd.h>
  34. #endif /* !_WIN32 && (unix || __unix || __unix__ ||
  35. * (__APPLE__ && __MACH__)) */
  36. #if !((defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L) || \
  37. (defined(_POSIX_THREAD_SAFE_FUNCTIONS) && \
  38. _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L))
  39. /*
  40. * This is a convenience shorthand macro to avoid checking the long
  41. * preprocessor conditions above. Ideally, we could expose this macro in
  42. * platform_util.h and simply use it in platform_util.c, threading.c and
  43. * threading.h. However, this macro is not part of the Mbed TLS public API, so
  44. * we keep it private by only defining it in this file
  45. */
  46. #if !(defined(_WIN32) && !defined(EFIX64) && !defined(EFI32))
  47. #define THREADING_USE_GMTIME
  48. #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
  49. #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
  50. ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
  51. _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
  52. #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
  53. #if defined(MBEDTLS_THREADING_PTHREAD)
  54. static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex)
  55. {
  56. if (mutex == NULL) {
  57. return;
  58. }
  59. /* A nonzero value of is_valid indicates a successfully initialized
  60. * mutex. This is a workaround for not being able to return an error
  61. * code for this function. The lock/unlock functions return an error
  62. * if is_valid is nonzero. The Mbed TLS unit test code uses this field
  63. * to distinguish more states of the mutex; see
  64. * tests/src/threading_helpers for details. */
  65. mutex->is_valid = pthread_mutex_init(&mutex->mutex, NULL) == 0;
  66. }
  67. static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex)
  68. {
  69. if (mutex == NULL || !mutex->is_valid) {
  70. return;
  71. }
  72. (void) pthread_mutex_destroy(&mutex->mutex);
  73. mutex->is_valid = 0;
  74. }
  75. static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
  76. {
  77. if (mutex == NULL || !mutex->is_valid) {
  78. return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
  79. }
  80. if (pthread_mutex_lock(&mutex->mutex) != 0) {
  81. return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
  82. }
  83. return 0;
  84. }
  85. static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex)
  86. {
  87. if (mutex == NULL || !mutex->is_valid) {
  88. return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
  89. }
  90. if (pthread_mutex_unlock(&mutex->mutex) != 0) {
  91. return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
  92. }
  93. return 0;
  94. }
  95. void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_init_pthread;
  96. void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_free_pthread;
  97. int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_lock_pthread;
  98. int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_unlock_pthread;
  99. /*
  100. * With pthreads we can statically initialize mutexes
  101. */
  102. #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
  103. #endif /* MBEDTLS_THREADING_PTHREAD */
  104. #if defined(MBEDTLS_THREADING_ALT)
  105. static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex)
  106. {
  107. ((void) mutex);
  108. return MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
  109. }
  110. static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex)
  111. {
  112. ((void) mutex);
  113. return;
  114. }
  115. void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
  116. void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *) = threading_mutex_dummy;
  117. int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
  118. int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
  119. /*
  120. * Set functions pointers and initialize global mutexes
  121. */
  122. void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
  123. void (*mutex_free)(mbedtls_threading_mutex_t *),
  124. int (*mutex_lock)(mbedtls_threading_mutex_t *),
  125. int (*mutex_unlock)(mbedtls_threading_mutex_t *))
  126. {
  127. mbedtls_mutex_init = mutex_init;
  128. mbedtls_mutex_free = mutex_free;
  129. mbedtls_mutex_lock = mutex_lock;
  130. mbedtls_mutex_unlock = mutex_unlock;
  131. #if defined(MBEDTLS_FS_IO)
  132. mbedtls_mutex_init(&mbedtls_threading_readdir_mutex);
  133. #endif
  134. #if defined(THREADING_USE_GMTIME)
  135. mbedtls_mutex_init(&mbedtls_threading_gmtime_mutex);
  136. #endif
  137. }
  138. /*
  139. * Free global mutexes
  140. */
  141. void mbedtls_threading_free_alt(void)
  142. {
  143. #if defined(MBEDTLS_FS_IO)
  144. mbedtls_mutex_free(&mbedtls_threading_readdir_mutex);
  145. #endif
  146. #if defined(THREADING_USE_GMTIME)
  147. mbedtls_mutex_free(&mbedtls_threading_gmtime_mutex);
  148. #endif
  149. }
  150. #endif /* MBEDTLS_THREADING_ALT */
  151. /*
  152. * Define global mutexes
  153. */
  154. #ifndef MUTEX_INIT
  155. #define MUTEX_INIT
  156. #endif
  157. #if defined(MBEDTLS_FS_IO)
  158. mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
  159. #endif
  160. #if defined(THREADING_USE_GMTIME)
  161. mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
  162. #endif
  163. #endif /* MBEDTLS_THREADING_C */