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