threading.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Threading abstraction layer
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_THREADING_C)
  29. #include "mbedtls/threading.h"
  30. #if defined(MBEDTLS_THREADING_PTHREAD)
  31. static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
  32. {
  33. if( mutex == NULL || mutex->is_valid )
  34. return;
  35. mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
  36. }
  37. static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
  38. {
  39. if( mutex == NULL || !mutex->is_valid )
  40. return;
  41. (void) pthread_mutex_destroy( &mutex->mutex );
  42. mutex->is_valid = 0;
  43. }
  44. static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
  45. {
  46. if( mutex == NULL || ! mutex->is_valid )
  47. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  48. if( pthread_mutex_lock( &mutex->mutex ) != 0 )
  49. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  50. return( 0 );
  51. }
  52. static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
  53. {
  54. if( mutex == NULL || ! mutex->is_valid )
  55. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  56. if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
  57. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  58. return( 0 );
  59. }
  60. void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
  61. void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
  62. int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
  63. int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
  64. /*
  65. * With phtreads we can statically initialize mutexes
  66. */
  67. #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
  68. #endif /* MBEDTLS_THREADING_PTHREAD */
  69. #if defined(MBEDTLS_THREADING_ALT)
  70. static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
  71. {
  72. ((void) mutex );
  73. return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
  74. }
  75. static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
  76. {
  77. ((void) mutex );
  78. return;
  79. }
  80. void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
  81. void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
  82. int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
  83. int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
  84. /*
  85. * Set functions pointers and initialize global mutexes
  86. */
  87. void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
  88. void (*mutex_free)( mbedtls_threading_mutex_t * ),
  89. int (*mutex_lock)( mbedtls_threading_mutex_t * ),
  90. int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
  91. {
  92. mbedtls_mutex_init = mutex_init;
  93. mbedtls_mutex_free = mutex_free;
  94. mbedtls_mutex_lock = mutex_lock;
  95. mbedtls_mutex_unlock = mutex_unlock;
  96. mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
  97. mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
  98. }
  99. /*
  100. * Free global mutexes
  101. */
  102. void mbedtls_threading_free_alt( void )
  103. {
  104. mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
  105. mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
  106. }
  107. #endif /* MBEDTLS_THREADING_ALT */
  108. /*
  109. * Define global mutexes
  110. */
  111. #ifndef MUTEX_INIT
  112. #define MUTEX_INIT
  113. #endif
  114. mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
  115. mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
  116. #endif /* MBEDTLS_THREADING_C */