threading.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_THREADING_H
  26. #define MBEDTLS_THREADING_H
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #include <stdlib.h>
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A /**< The selected feature is not available. */
  37. #define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA -0x001C /**< Bad input parameters to function. */
  38. #define MBEDTLS_ERR_THREADING_MUTEX_ERROR -0x001E /**< Locking / unlocking / free failed with error code. */
  39. #if defined(MBEDTLS_THREADING_PTHREAD)
  40. #include <pthread.h>
  41. typedef struct
  42. {
  43. pthread_mutex_t mutex;
  44. char is_valid;
  45. } mbedtls_threading_mutex_t;
  46. #endif
  47. #if defined(MBEDTLS_THREADING_ALT)
  48. /* You should define the mbedtls_threading_mutex_t type in your header */
  49. #include "threading_alt.h"
  50. /**
  51. * \brief Set your alternate threading implementation function
  52. * pointers and initialize global mutexes. If used, this
  53. * function must be called once in the main thread before any
  54. * other mbed TLS function is called, and
  55. * mbedtls_threading_free_alt() must be called once in the main
  56. * thread after all other mbed TLS functions.
  57. *
  58. * \note mutex_init() and mutex_free() don't return a status code.
  59. * If mutex_init() fails, it should leave its argument (the
  60. * mutex) in a state such that mutex_lock() will fail when
  61. * called with this argument.
  62. *
  63. * \param mutex_init the init function implementation
  64. * \param mutex_free the free function implementation
  65. * \param mutex_lock the lock function implementation
  66. * \param mutex_unlock the unlock function implementation
  67. */
  68. void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
  69. void (*mutex_free)( mbedtls_threading_mutex_t * ),
  70. int (*mutex_lock)( mbedtls_threading_mutex_t * ),
  71. int (*mutex_unlock)( mbedtls_threading_mutex_t * ) );
  72. /**
  73. * \brief Free global mutexes.
  74. */
  75. void mbedtls_threading_free_alt( void );
  76. #endif /* MBEDTLS_THREADING_ALT */
  77. #if defined(MBEDTLS_THREADING_C)
  78. /*
  79. * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock
  80. *
  81. * All these functions are expected to work or the result will be undefined.
  82. */
  83. extern void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t *mutex );
  84. extern void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t *mutex );
  85. extern int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t *mutex );
  86. extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex );
  87. /*
  88. * Global mutexes
  89. */
  90. extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
  91. extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
  92. #endif /* MBEDTLS_THREADING_C */
  93. #ifdef __cplusplus
  94. }
  95. #endif
  96. #endif /* threading.h */