threading.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * \file threading.h
  3. *
  4. * \brief Threading abstraction layer
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. *
  10. * This file is provided under the Apache License 2.0, or the
  11. * GNU General Public License v2.0 or later.
  12. *
  13. * **********
  14. * Apache License 2.0:
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. * **********
  29. *
  30. * **********
  31. * GNU General Public License v2.0 or later:
  32. *
  33. * This program is free software; you can redistribute it and/or modify
  34. * it under the terms of the GNU General Public License as published by
  35. * the Free Software Foundation; either version 2 of the License, or
  36. * (at your option) any later version.
  37. *
  38. * This program is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. * GNU General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU General Public License along
  44. * with this program; if not, write to the Free Software Foundation, Inc.,
  45. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  46. *
  47. * **********
  48. */
  49. #ifndef MBEDTLS_THREADING_H
  50. #define MBEDTLS_THREADING_H
  51. #if !defined(MBEDTLS_CONFIG_FILE)
  52. #include "config.h"
  53. #else
  54. #include MBEDTLS_CONFIG_FILE
  55. #endif
  56. #include <stdlib.h>
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. /* MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE is deprecated and should not be
  61. * used. */
  62. #define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A /**< The selected feature is not available. */
  63. #define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA -0x001C /**< Bad input parameters to function. */
  64. #define MBEDTLS_ERR_THREADING_MUTEX_ERROR -0x001E /**< Locking / unlocking / free failed with error code. */
  65. #if defined(MBEDTLS_THREADING_PTHREAD)
  66. #include <pthread.h>
  67. typedef struct mbedtls_threading_mutex_t
  68. {
  69. pthread_mutex_t mutex;
  70. /* is_valid is 0 after a failed init or a free, and nonzero after a
  71. * successful init. This field is not considered part of the public
  72. * API of Mbed TLS and may change without notice. */
  73. char is_valid;
  74. } mbedtls_threading_mutex_t;
  75. #endif
  76. #if defined(MBEDTLS_THREADING_ALT)
  77. /* You should define the mbedtls_threading_mutex_t type in your header */
  78. #include "threading_alt.h"
  79. /**
  80. * \brief Set your alternate threading implementation function
  81. * pointers and initialize global mutexes. If used, this
  82. * function must be called once in the main thread before any
  83. * other mbed TLS function is called, and
  84. * mbedtls_threading_free_alt() must be called once in the main
  85. * thread after all other mbed TLS functions.
  86. *
  87. * \note mutex_init() and mutex_free() don't return a status code.
  88. * If mutex_init() fails, it should leave its argument (the
  89. * mutex) in a state such that mutex_lock() will fail when
  90. * called with this argument.
  91. *
  92. * \param mutex_init the init function implementation
  93. * \param mutex_free the free function implementation
  94. * \param mutex_lock the lock function implementation
  95. * \param mutex_unlock the unlock function implementation
  96. */
  97. void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
  98. void (*mutex_free)( mbedtls_threading_mutex_t * ),
  99. int (*mutex_lock)( mbedtls_threading_mutex_t * ),
  100. int (*mutex_unlock)( mbedtls_threading_mutex_t * ) );
  101. /**
  102. * \brief Free global mutexes.
  103. */
  104. void mbedtls_threading_free_alt( void );
  105. #endif /* MBEDTLS_THREADING_ALT */
  106. #if defined(MBEDTLS_THREADING_C)
  107. /*
  108. * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock
  109. *
  110. * All these functions are expected to work or the result will be undefined.
  111. */
  112. extern void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t *mutex );
  113. extern void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t *mutex );
  114. extern int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t *mutex );
  115. extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex );
  116. /*
  117. * Global mutexes
  118. */
  119. #if defined(MBEDTLS_FS_IO)
  120. extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
  121. #endif
  122. #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
  123. /* This mutex may or may not be used in the default definition of
  124. * mbedtls_platform_gmtime_r(), but in order to determine that,
  125. * we need to check POSIX features, hence modify _POSIX_C_SOURCE.
  126. * With the current approach, this declaration is orphaned, lacking
  127. * an accompanying definition, in case mbedtls_platform_gmtime_r()
  128. * doesn't need it, but that's not a problem. */
  129. extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
  130. #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
  131. #endif /* MBEDTLS_THREADING_C */
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135. #endif /* threading.h */