threading.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /* A nonzero value of is_valid indicates a successfully initialized
  59. * mutex. This is a workaround for not being able to return an error
  60. * code for this function. The lock/unlock functions return an error
  61. * if is_valid is nonzero. The Mbed TLS unit test code uses this field
  62. * to distinguish more states of the mutex; see
  63. * tests/src/threading_helpers for details. */
  64. mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
  65. }
  66. static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
  67. {
  68. if( mutex == NULL || !mutex->is_valid )
  69. return;
  70. (void) pthread_mutex_destroy( &mutex->mutex );
  71. mutex->is_valid = 0;
  72. }
  73. static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
  74. {
  75. if( mutex == NULL || ! mutex->is_valid )
  76. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  77. if( pthread_mutex_lock( &mutex->mutex ) != 0 )
  78. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  79. return( 0 );
  80. }
  81. static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
  82. {
  83. if( mutex == NULL || ! mutex->is_valid )
  84. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  85. if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
  86. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  87. return( 0 );
  88. }
  89. void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
  90. void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
  91. int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
  92. int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
  93. /*
  94. * With phtreads we can statically initialize mutexes
  95. */
  96. #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
  97. #endif /* MBEDTLS_THREADING_PTHREAD */
  98. #if defined(MBEDTLS_THREADING_ALT)
  99. static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
  100. {
  101. ((void) mutex );
  102. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  103. }
  104. static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
  105. {
  106. ((void) mutex );
  107. return;
  108. }
  109. void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
  110. void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
  111. int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
  112. int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
  113. /*
  114. * Set functions pointers and initialize global mutexes
  115. */
  116. void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
  117. void (*mutex_free)( mbedtls_threading_mutex_t * ),
  118. int (*mutex_lock)( mbedtls_threading_mutex_t * ),
  119. int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
  120. {
  121. mbedtls_mutex_init = mutex_init;
  122. mbedtls_mutex_free = mutex_free;
  123. mbedtls_mutex_lock = mutex_lock;
  124. mbedtls_mutex_unlock = mutex_unlock;
  125. #if defined(MBEDTLS_FS_IO)
  126. mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
  127. #endif
  128. #if defined(THREADING_USE_GMTIME)
  129. mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
  130. #endif
  131. }
  132. /*
  133. * Free global mutexes
  134. */
  135. void mbedtls_threading_free_alt( void )
  136. {
  137. #if defined(MBEDTLS_FS_IO)
  138. mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
  139. #endif
  140. #if defined(THREADING_USE_GMTIME)
  141. mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
  142. #endif
  143. }
  144. #endif /* MBEDTLS_THREADING_ALT */
  145. /*
  146. * Define global mutexes
  147. */
  148. #ifndef MUTEX_INIT
  149. #define MUTEX_INIT
  150. #endif
  151. #if defined(MBEDTLS_FS_IO)
  152. mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
  153. #endif
  154. #if defined(THREADING_USE_GMTIME)
  155. mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
  156. #endif
  157. #endif /* MBEDTLS_THREADING_C */