Paul Elliott | 17c119a | 2023-12-08 16:55:03 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file threading_helpers.h |
| 3 | * |
| 4 | * \brief This file contains the prototypes of helper functions for the purpose |
| 5 | * of testing threading. |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright The Mbed TLS Contributors |
| 10 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 11 | */ |
| 12 | |
| 13 | #ifndef THREADING_HELPERS_H |
| 14 | #define THREADING_HELPERS_H |
| 15 | |
| 16 | #if defined MBEDTLS_THREADING_C |
| 17 | |
Paul Elliott | 3a4d2f1 | 2023-12-08 20:49:47 +0000 | [diff] [blame] | 18 | #include "mbedtls/private_access.h" |
| 19 | #include "mbedtls/build_info.h" |
| 20 | |
| 21 | /* Most fields of publicly available structs are private and are wrapped with |
| 22 | * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields |
| 23 | * directly (without using the MBEDTLS_PRIVATE wrapper). */ |
| 24 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 25 | |
| 26 | #define MBEDTLS_ERR_THREADING_THREAD_ERROR -0x001F |
| 27 | |
| 28 | #if defined(MBEDTLS_THREADING_PTHREAD) |
| 29 | #include <pthread.h> |
| 30 | |
| 31 | typedef struct mbedtls_test_thread_t { |
| 32 | pthread_t MBEDTLS_PRIVATE(thread); |
| 33 | } mbedtls_test_thread_t; |
| 34 | |
| 35 | #endif /* MBEDTLS_THREADING_PTHREAD */ |
| 36 | |
| 37 | #if defined(MBEDTLS_THREADING_ALT) |
| 38 | /* You should define the mbedtls_test_thread_t type in your header */ |
| 39 | #include "threading_alt.h" |
| 40 | |
| 41 | /** |
| 42 | * \brief Set your alternate threading implementation |
| 43 | * function pointers fgr test threads. If used, |
| 44 | * this function must be called once in the main thread |
| 45 | * before any other MbedTLS function is called. |
| 46 | * |
| 47 | * \note These functions are part of the testing API only and |
| 48 | * thus not considered part of the public API of |
| 49 | * MbedTLS and thus may change without notice. |
| 50 | * |
| 51 | * \param thread_create The thread create function implementation |
| 52 | * \param thread_join The thread join function implementation |
| 53 | |
| 54 | */ |
| 55 | void mbedtls_test_thread_set_alt(int (*thread_create)(mbedtls_test_thread_t *thread, |
| 56 | void *(*thread_func)( |
| 57 | void *), |
| 58 | void *thread_data), |
| 59 | int (*thread_join)(mbedtls_test_thread_t *thread)); |
| 60 | |
| 61 | #endif /* MBEDTLS_THREADING_ALT*/ |
| 62 | |
| 63 | /** |
| 64 | * \brief The function pointers for thread create and thread |
| 65 | * join. |
| 66 | * |
| 67 | * \note These functions are part of the testing API only and |
| 68 | * thus not considered part of the public API of |
| 69 | * MbedTLS and thus may change without notice. |
| 70 | * |
| 71 | * \note All these functions are expected to work or |
| 72 | * the result will be undefined. |
| 73 | */ |
| 74 | extern int (*mbedtls_test_thread_create)(mbedtls_test_thread_t *thread, |
| 75 | void *(*thread_func)(void *), void *thread_data); |
| 76 | extern int (*mbedtls_test_thread_join)(mbedtls_test_thread_t *thread); |
| 77 | |
Paul Elliott | 17c119a | 2023-12-08 16:55:03 +0000 | [diff] [blame] | 78 | #if defined(MBEDTLS_THREADING_PTHREAD) && defined(MBEDTLS_TEST_HOOKS) |
| 79 | #define MBEDTLS_TEST_MUTEX_USAGE |
| 80 | #endif |
| 81 | |
| 82 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 83 | /** |
| 84 | * Activate the mutex usage verification framework. See threading_helpers.c for |
| 85 | * information. |
| 86 | */ |
| 87 | void mbedtls_test_mutex_usage_init(void); |
| 88 | |
| 89 | /** |
| 90 | * Deactivate the mutex usage verification framework. See threading_helpers.c |
| 91 | * for information. |
| 92 | */ |
| 93 | void mbedtls_test_mutex_usage_end(void); |
| 94 | |
| 95 | /** |
| 96 | * Call this function after executing a test case to check for mutex usage |
| 97 | * errors. |
| 98 | */ |
| 99 | void mbedtls_test_mutex_usage_check(void); |
| 100 | #endif /* MBEDTLS_TEST_MUTEX_USAGE */ |
| 101 | |
| 102 | #endif /* MBEDTLS_THREADING_C */ |
| 103 | |
| 104 | #endif /* THREADING_HELPERS_H */ |