Jaeden Amero | e54e693 | 2018-08-06 16:19:58 +0100 | [diff] [blame] | 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 Crypto (https://tls.mbed.org) |
| 23 | */ |
| 24 | #ifndef MBEDCRYPTO_THREADING_H |
| 25 | #define MBEDCRYPTO_THREADING_H |
| 26 | |
| 27 | #if !defined(MBEDCRYPTO_CONFIG_FILE) |
| 28 | #include "config.h" |
| 29 | #else |
| 30 | #include MBEDCRYPTO_CONFIG_FILE |
| 31 | #endif |
| 32 | |
| 33 | #include <stdlib.h> |
| 34 | |
| 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
| 39 | #define MBEDCRYPTO_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A /**< The selected feature is not available. */ |
| 40 | #define MBEDCRYPTO_ERR_THREADING_BAD_INPUT_DATA -0x001C /**< Bad input parameters to function. */ |
| 41 | #define MBEDCRYPTO_ERR_THREADING_MUTEX_ERROR -0x001E /**< Locking / unlocking / free failed with error code. */ |
| 42 | |
| 43 | #if defined(MBEDCRYPTO_THREADING_PTHREAD) |
| 44 | #include <pthread.h> |
| 45 | typedef struct |
| 46 | { |
| 47 | pthread_mutex_t mutex; |
| 48 | char is_valid; |
| 49 | } mbedcrypto_threading_mutex_t; |
| 50 | #endif |
| 51 | |
| 52 | #if defined(MBEDCRYPTO_THREADING_ALT) |
| 53 | /* You should define the mbedcrypto_threading_mutex_t type in your header */ |
| 54 | #include "threading_alt.h" |
| 55 | |
| 56 | /** |
| 57 | * \brief Set your alternate threading implementation function |
| 58 | * pointers and initialize global mutexes. If used, this |
| 59 | * function must be called once in the main thread before any |
| 60 | * other Mbed Crypto function is called, and |
| 61 | * mbedcrypto_threading_free_alt() must be called once in the main |
| 62 | * thread after all other Mbed Crypto functions. |
| 63 | * |
| 64 | * \note mutex_init() and mutex_free() don't return a status code. |
| 65 | * If mutex_init() fails, it should leave its argument (the |
| 66 | * mutex) in a state such that mutex_lock() will fail when |
| 67 | * called with this argument. |
| 68 | * |
| 69 | * \param mutex_init the init function implementation |
| 70 | * \param mutex_free the free function implementation |
| 71 | * \param mutex_lock the lock function implementation |
| 72 | * \param mutex_unlock the unlock function implementation |
| 73 | */ |
| 74 | void mbedcrypto_threading_set_alt( void (*mutex_init)( mbedcrypto_threading_mutex_t * ), |
| 75 | void (*mutex_free)( mbedcrypto_threading_mutex_t * ), |
| 76 | int (*mutex_lock)( mbedcrypto_threading_mutex_t * ), |
| 77 | int (*mutex_unlock)( mbedcrypto_threading_mutex_t * ) ); |
| 78 | |
| 79 | /** |
| 80 | * \brief Free global mutexes. |
| 81 | */ |
| 82 | void mbedcrypto_threading_free_alt( void ); |
| 83 | #endif /* MBEDCRYPTO_THREADING_ALT */ |
| 84 | |
| 85 | #if defined(MBEDCRYPTO_THREADING_C) |
| 86 | /* |
| 87 | * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock |
| 88 | * |
| 89 | * All these functions are expected to work or the result will be undefined. |
| 90 | */ |
| 91 | extern void (*mbedcrypto_mutex_init)( mbedcrypto_threading_mutex_t *mutex ); |
| 92 | extern void (*mbedcrypto_mutex_free)( mbedcrypto_threading_mutex_t *mutex ); |
| 93 | extern int (*mbedcrypto_mutex_lock)( mbedcrypto_threading_mutex_t *mutex ); |
| 94 | extern int (*mbedcrypto_mutex_unlock)( mbedcrypto_threading_mutex_t *mutex ); |
| 95 | |
| 96 | /* |
| 97 | * Global mutexes |
| 98 | */ |
| 99 | #if defined(MBEDCRYPTO_FS_IO) |
| 100 | extern mbedcrypto_threading_mutex_t mbedcrypto_threading_readdir_mutex; |
| 101 | #endif |
| 102 | #if defined(MBEDCRYPTO_HAVE_TIME_DATE) |
| 103 | extern mbedcrypto_threading_mutex_t mbedcrypto_threading_gmtime_mutex; |
| 104 | #endif |
| 105 | #endif /* MBEDCRYPTO_THREADING_C */ |
| 106 | |
| 107 | #ifdef __cplusplus |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | #endif /* threading.h */ |