blob: ba965c8775de5982b20aebd1ba5810d1e97a5e13 [file] [log] [blame]
Paul Elliott17c119a2023-12-08 16:55:03 +00001/**
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 Elliott3a4d2f12023-12-08 20:49:47 +000018#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
31typedef 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
Paul Elliotted3ba3c2024-01-19 16:44:23 +000043 * function pointers for test threads. If used, this
44 * function must be called once in the main thread
Paul Elliott3a4d2f12023-12-08 20:49:47 +000045 * 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 *
Paul Elliotted3ba3c2024-01-19 16:44:23 +000051 * \param thread_create The thread create function implementation.
52 * \param thread_join The thread join function implementation.
Paul Elliott3a4d2f12023-12-08 20:49:47 +000053
54 */
55void 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 */
74extern int (*mbedtls_test_thread_create)(mbedtls_test_thread_t *thread,
75 void *(*thread_func)(void *), void *thread_data);
76extern int (*mbedtls_test_thread_join)(mbedtls_test_thread_t *thread);
77
Paul Elliott17c119a2023-12-08 16:55:03 +000078#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 */
87void mbedtls_test_mutex_usage_init(void);
88
89/**
90 * Deactivate the mutex usage verification framework. See threading_helpers.c
91 * for information.
92 */
93void 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 */
99void mbedtls_test_mutex_usage_check(void);
100#endif /* MBEDTLS_TEST_MUTEX_USAGE */
101
102#endif /* MBEDTLS_THREADING_C */
103
104#endif /* THREADING_HELPERS_H */