threading.c 7.2 KB

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