threading.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
  59. }
  60. static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
  61. {
  62. if( mutex == NULL || !mutex->is_valid )
  63. return;
  64. (void) pthread_mutex_destroy( &mutex->mutex );
  65. mutex->is_valid = 0;
  66. }
  67. static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
  68. {
  69. if( mutex == NULL || ! mutex->is_valid )
  70. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  71. if( pthread_mutex_lock( &mutex->mutex ) != 0 )
  72. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  73. return( 0 );
  74. }
  75. static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
  76. {
  77. if( mutex == NULL || ! mutex->is_valid )
  78. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  79. if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
  80. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  81. return( 0 );
  82. }
  83. void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
  84. void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
  85. int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
  86. int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
  87. /*
  88. * With phtreads we can statically initialize mutexes
  89. */
  90. #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
  91. #endif /* MBEDTLS_THREADING_PTHREAD */
  92. #if defined(MBEDTLS_THREADING_ALT)
  93. static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
  94. {
  95. ((void) mutex );
  96. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  97. }
  98. static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
  99. {
  100. ((void) mutex );
  101. return;
  102. }
  103. void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
  104. void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
  105. int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
  106. int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
  107. /*
  108. * Set functions pointers and initialize global mutexes
  109. */
  110. void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
  111. void (*mutex_free)( mbedtls_threading_mutex_t * ),
  112. int (*mutex_lock)( mbedtls_threading_mutex_t * ),
  113. int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
  114. {
  115. mbedtls_mutex_init = mutex_init;
  116. mbedtls_mutex_free = mutex_free;
  117. mbedtls_mutex_lock = mutex_lock;
  118. mbedtls_mutex_unlock = mutex_unlock;
  119. #if defined(MBEDTLS_FS_IO)
  120. mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
  121. #endif
  122. #if defined(THREADING_USE_GMTIME)
  123. mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
  124. #endif
  125. }
  126. /*
  127. * Free global mutexes
  128. */
  129. void mbedtls_threading_free_alt( void )
  130. {
  131. #if defined(MBEDTLS_FS_IO)
  132. mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
  133. #endif
  134. #if defined(THREADING_USE_GMTIME)
  135. mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
  136. #endif
  137. }
  138. #endif /* MBEDTLS_THREADING_ALT */
  139. /*
  140. * Define global mutexes
  141. */
  142. #ifndef MUTEX_INIT
  143. #define MUTEX_INIT
  144. #endif
  145. #if defined(MBEDTLS_FS_IO)
  146. mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
  147. #endif
  148. #if defined(THREADING_USE_GMTIME)
  149. mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
  150. #endif
  151. #endif /* MBEDTLS_THREADING_C */