threading.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Threading abstraction layer
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /*
  22. * Ensure gmtime_r is available even with -std=c99; must be defined before
  23. * config.h, which pulls in glibc's features.h. Harmless on other platforms.
  24. */
  25. #if !defined(_POSIX_C_SOURCE)
  26. #define _POSIX_C_SOURCE 200112L
  27. #endif
  28. #if !defined(MBEDTLS_CONFIG_FILE)
  29. #include "mbedtls/config.h"
  30. #else
  31. #include MBEDTLS_CONFIG_FILE
  32. #endif
  33. #if defined(MBEDTLS_THREADING_C)
  34. #include "mbedtls/threading.h"
  35. #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
  36. #if !defined(_WIN32) && (defined(unix) || \
  37. defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
  38. defined(__MACH__)))
  39. #include <unistd.h>
  40. #endif /* !_WIN32 && (unix || __unix || __unix__ ||
  41. * (__APPLE__ && __MACH__)) */
  42. #if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
  43. ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
  44. _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) )
  45. /*
  46. * This is a convenience shorthand macro to avoid checking the long
  47. * preprocessor conditions above. Ideally, we could expose this macro in
  48. * platform_util.h and simply use it in platform_util.c, threading.c and
  49. * threading.h. However, this macro is not part of the Mbed TLS public API, so
  50. * we keep it private by only defining it in this file
  51. */
  52. #if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
  53. #define THREADING_USE_GMTIME
  54. #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
  55. #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
  56. ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
  57. _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */
  58. #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
  59. #if defined(MBEDTLS_THREADING_PTHREAD)
  60. static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
  61. {
  62. if( mutex == NULL )
  63. return;
  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 */