threading.c 4.2 KB

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