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