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