threading.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_THREADING_C)
  27. #include "mbedtls/threading.h"
  28. #if defined(MBEDTLS_THREADING_PTHREAD)
  29. static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
  30. {
  31. if( mutex == NULL )
  32. return;
  33. mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
  34. }
  35. static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
  36. {
  37. if( mutex == NULL )
  38. return;
  39. (void) pthread_mutex_destroy( &mutex->mutex );
  40. }
  41. static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
  42. {
  43. if( mutex == NULL || ! mutex->is_valid )
  44. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  45. if( pthread_mutex_lock( &mutex->mutex ) != 0 )
  46. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  47. return( 0 );
  48. }
  49. static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
  50. {
  51. if( mutex == NULL || ! mutex->is_valid )
  52. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  53. if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
  54. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  55. return( 0 );
  56. }
  57. void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
  58. void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
  59. int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
  60. int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
  61. /*
  62. * With phtreads we can statically initialize mutexes
  63. */
  64. #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
  65. #endif /* MBEDTLS_THREADING_PTHREAD */
  66. #if defined(MBEDTLS_THREADING_ALT)
  67. static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
  68. {
  69. ((void) mutex );
  70. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  71. }
  72. static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
  73. {
  74. ((void) mutex );
  75. return;
  76. }
  77. void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
  78. void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
  79. int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
  80. int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
  81. /*
  82. * Set functions pointers and initialize global mutexes
  83. */
  84. void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
  85. void (*mutex_free)( mbedtls_threading_mutex_t * ),
  86. int (*mutex_lock)( mbedtls_threading_mutex_t * ),
  87. int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
  88. {
  89. mbedtls_mutex_init = mutex_init;
  90. mbedtls_mutex_free = mutex_free;
  91. mbedtls_mutex_lock = mutex_lock;
  92. mbedtls_mutex_unlock = mutex_unlock;
  93. mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
  94. mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
  95. }
  96. /*
  97. * Free global mutexes
  98. */
  99. void mbedtls_threading_free_alt( void )
  100. {
  101. mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
  102. mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
  103. }
  104. #endif /* MBEDTLS_THREADING_ALT */
  105. /*
  106. * Define global mutexes
  107. */
  108. #ifndef MUTEX_INIT
  109. #define MUTEX_INIT
  110. #endif
  111. mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
  112. mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
  113. #endif /* MBEDTLS_THREADING_C */