threading.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * \file threading.h
  3. *
  4. * \brief Threading abstraction layer
  5. */
  6. /*
  7. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. * This file is part of mbed TLS (https://tls.mbed.org)
  23. */
  24. #ifndef MBEDTLS_THREADING_H
  25. #define MBEDTLS_THREADING_H
  26. #if !defined(MBEDTLS_CONFIG_FILE)
  27. #include "config.h"
  28. #else
  29. #include MBEDTLS_CONFIG_FILE
  30. #endif
  31. #include <stdlib.h>
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /* MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE is deprecated and should not be
  36. * used. */
  37. #define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A /**< The selected feature is not available. */
  38. #define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA -0x001C /**< Bad input parameters to function. */
  39. #define MBEDTLS_ERR_THREADING_MUTEX_ERROR -0x001E /**< Locking / unlocking / free failed with error code. */
  40. #if defined(MBEDTLS_THREADING_PTHREAD)
  41. #include <pthread.h>
  42. typedef struct mbedtls_threading_mutex_t
  43. {
  44. pthread_mutex_t mutex;
  45. char is_valid;
  46. } mbedtls_threading_mutex_t;
  47. #endif
  48. #if defined(MBEDTLS_THREADING_ALT)
  49. /* You should define the mbedtls_threading_mutex_t type in your header */
  50. #include "threading_alt.h"
  51. /**
  52. * \brief Set your alternate threading implementation function
  53. * pointers and initialize global mutexes. If used, this
  54. * function must be called once in the main thread before any
  55. * other mbed TLS function is called, and
  56. * mbedtls_threading_free_alt() must be called once in the main
  57. * thread after all other mbed TLS functions.
  58. *
  59. * \note mutex_init() and mutex_free() don't return a status code.
  60. * If mutex_init() fails, it should leave its argument (the
  61. * mutex) in a state such that mutex_lock() will fail when
  62. * called with this argument.
  63. *
  64. * \param mutex_init the init function implementation
  65. * \param mutex_free the free function implementation
  66. * \param mutex_lock the lock function implementation
  67. * \param mutex_unlock the unlock function implementation
  68. */
  69. void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
  70. void (*mutex_free)( mbedtls_threading_mutex_t * ),
  71. int (*mutex_lock)( mbedtls_threading_mutex_t * ),
  72. int (*mutex_unlock)( mbedtls_threading_mutex_t * ) );
  73. /**
  74. * \brief Free global mutexes.
  75. */
  76. void mbedtls_threading_free_alt( void );
  77. #endif /* MBEDTLS_THREADING_ALT */
  78. #if defined(MBEDTLS_THREADING_C)
  79. /*
  80. * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock
  81. *
  82. * All these functions are expected to work or the result will be undefined.
  83. */
  84. extern void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t *mutex );
  85. extern void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t *mutex );
  86. extern int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t *mutex );
  87. extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex );
  88. /*
  89. * Global mutexes
  90. */
  91. #if defined(MBEDTLS_FS_IO)
  92. extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
  93. #endif
  94. #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
  95. /* This mutex may or may not be used in the default definition of
  96. * mbedtls_platform_gmtime_r(), but in order to determine that,
  97. * we need to check POSIX features, hence modify _POSIX_C_SOURCE.
  98. * With the current approach, this declaration is orphaned, lacking
  99. * an accompanying definition, in case mbedtls_platform_gmtime_r()
  100. * doesn't need it, but that's not a problem. */
  101. extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
  102. #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
  103. #endif /* MBEDTLS_THREADING_C */
  104. #ifdef __cplusplus
  105. }
  106. #endif
  107. #endif /* threading.h */