threading.h 3.6 KB

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