threading.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A /**< The selected feature is not available. */
  36. #define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA -0x001C /**< Bad input parameters to function. */
  37. #define MBEDTLS_ERR_THREADING_MUTEX_ERROR -0x001E /**< Locking / unlocking / free failed with error code. */
  38. #if defined(MBEDTLS_THREADING_PTHREAD)
  39. #include <pthread.h>
  40. typedef struct
  41. {
  42. pthread_mutex_t mutex;
  43. char is_valid;
  44. } mbedtls_threading_mutex_t;
  45. #endif
  46. #if defined(MBEDTLS_THREADING_ALT)
  47. /* You should define the mbedtls_threading_mutex_t type in your header */
  48. #include "threading_alt.h"
  49. /**
  50. * \brief Set your alternate threading implementation function
  51. * pointers and initialize global mutexes. If used, this
  52. * function must be called once in the main thread before any
  53. * other mbed TLS function is called, and
  54. * mbedtls_threading_free_alt() must be called once in the main
  55. * thread after all other mbed TLS functions.
  56. *
  57. * \note mutex_init() and mutex_free() don't return a status code.
  58. * If mutex_init() fails, it should leave its argument (the
  59. * mutex) in a state such that mutex_lock() will fail when
  60. * called with this argument.
  61. *
  62. * \param mutex_init the init function implementation
  63. * \param mutex_free the free function implementation
  64. * \param mutex_lock the lock function implementation
  65. * \param mutex_unlock the unlock function implementation
  66. */
  67. void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
  68. void (*mutex_free)( mbedtls_threading_mutex_t * ),
  69. int (*mutex_lock)( mbedtls_threading_mutex_t * ),
  70. int (*mutex_unlock)( mbedtls_threading_mutex_t * ) );
  71. /**
  72. * \brief Free global mutexes.
  73. */
  74. void mbedtls_threading_free_alt( void );
  75. #endif /* MBEDTLS_THREADING_ALT */
  76. #if defined(MBEDTLS_THREADING_C)
  77. /*
  78. * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock
  79. *
  80. * All these functions are expected to work or the result will be undefined.
  81. */
  82. extern void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t *mutex );
  83. extern void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t *mutex );
  84. extern int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t *mutex );
  85. extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex );
  86. /*
  87. * Global mutexes
  88. */
  89. extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
  90. extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
  91. #endif /* MBEDTLS_THREADING_C */
  92. #ifdef __cplusplus
  93. }
  94. #endif
  95. #endif /* threading.h */