Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:37:34

0001 /**
0002  * \file threading.h
0003  *
0004  * \brief Threading abstraction layer
0005  */
0006 /*
0007  *  Copyright The Mbed TLS Contributors
0008  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
0009  */
0010 #ifndef MBEDTLS_THREADING_H
0011 #define MBEDTLS_THREADING_H
0012 #include "mbedtls/private_access.h"
0013 
0014 #include "mbedtls/build_info.h"
0015 
0016 #include <stdlib.h>
0017 
0018 #ifdef __cplusplus
0019 extern "C" {
0020 #endif
0021 
0022 /** Bad input parameters to function. */
0023 #define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA              -0x001C
0024 /** Locking / unlocking / free failed with error code. */
0025 #define MBEDTLS_ERR_THREADING_MUTEX_ERROR                 -0x001E
0026 
0027 #if defined(MBEDTLS_THREADING_PTHREAD)
0028 #include <pthread.h>
0029 typedef struct mbedtls_threading_mutex_t {
0030     pthread_mutex_t MBEDTLS_PRIVATE(mutex);
0031 
0032     /* WARNING - state should only be accessed when holding the mutex lock in
0033      * tests/src/threading_helpers.c, otherwise corruption can occur.
0034      * state will be 0 after a failed init or a free, and nonzero after a
0035      * successful init. This field is for testing only and thus not considered
0036      * part of the public API of Mbed TLS and may change without notice.*/
0037     char MBEDTLS_PRIVATE(state);
0038 
0039 } mbedtls_threading_mutex_t;
0040 #endif
0041 
0042 #if defined(MBEDTLS_THREADING_ALT)
0043 /* You should define the mbedtls_threading_mutex_t type in your header */
0044 #include "threading_alt.h"
0045 
0046 /**
0047  * \brief           Set your alternate threading implementation function
0048  *                  pointers and initialize global mutexes. If used, this
0049  *                  function must be called once in the main thread before any
0050  *                  other Mbed TLS function is called, and
0051  *                  mbedtls_threading_free_alt() must be called once in the main
0052  *                  thread after all other Mbed TLS functions.
0053  *
0054  * \note            mutex_init() and mutex_free() don't return a status code.
0055  *                  If mutex_init() fails, it should leave its argument (the
0056  *                  mutex) in a state such that mutex_lock() will fail when
0057  *                  called with this argument.
0058  *
0059  * \param mutex_init    the init function implementation
0060  * \param mutex_free    the free function implementation
0061  * \param mutex_lock    the lock function implementation
0062  * \param mutex_unlock  the unlock function implementation
0063  */
0064 void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
0065                                void (*mutex_free)(mbedtls_threading_mutex_t *),
0066                                int (*mutex_lock)(mbedtls_threading_mutex_t *),
0067                                int (*mutex_unlock)(mbedtls_threading_mutex_t *));
0068 
0069 /**
0070  * \brief               Free global mutexes.
0071  */
0072 void mbedtls_threading_free_alt(void);
0073 #endif /* MBEDTLS_THREADING_ALT */
0074 
0075 #if defined(MBEDTLS_THREADING_C)
0076 /*
0077  * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock
0078  *
0079  * All these functions are expected to work or the result will be undefined.
0080  */
0081 extern void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *mutex);
0082 extern void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *mutex);
0083 extern int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *mutex);
0084 extern int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *mutex);
0085 
0086 /*
0087  * Global mutexes
0088  */
0089 #if defined(MBEDTLS_FS_IO)
0090 extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
0091 #endif
0092 
0093 #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
0094 /* This mutex may or may not be used in the default definition of
0095  * mbedtls_platform_gmtime_r(), but in order to determine that,
0096  * we need to check POSIX features, hence modify _POSIX_C_SOURCE.
0097  * With the current approach, this declaration is orphaned, lacking
0098  * an accompanying definition, in case mbedtls_platform_gmtime_r()
0099  * doesn't need it, but that's not a problem. */
0100 extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
0101 #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
0102 
0103 #if defined(MBEDTLS_PSA_CRYPTO_C)
0104 /*
0105  * A mutex used to make the PSA subsystem thread safe.
0106  *
0107  * key_slot_mutex protects the registered_readers and
0108  * state variable for all key slots in &global_data.key_slots.
0109  *
0110  * This mutex must be held when any read from or write to a state or
0111  * registered_readers field is performed, i.e. when calling functions:
0112  * psa_key_slot_state_transition(), psa_register_read(), psa_unregister_read(),
0113  * psa_key_slot_has_readers() and psa_wipe_key_slot(). */
0114 extern mbedtls_threading_mutex_t mbedtls_threading_key_slot_mutex;
0115 
0116 /*
0117  * A mutex used to make the non-rng PSA global_data struct members thread safe.
0118  *
0119  * This mutex must be held when reading or writing to any of the PSA global_data
0120  * structure members, other than the rng_state or rng struct. */
0121 extern mbedtls_threading_mutex_t mbedtls_threading_psa_globaldata_mutex;
0122 
0123 /*
0124  * A mutex used to make the PSA global_data rng data thread safe.
0125  *
0126  * This mutex must be held when reading or writing to the PSA
0127  * global_data rng_state or rng struct members. */
0128 extern mbedtls_threading_mutex_t mbedtls_threading_psa_rngdata_mutex;
0129 #endif
0130 
0131 #endif /* MBEDTLS_THREADING_C */
0132 
0133 #ifdef __cplusplus
0134 }
0135 #endif
0136 
0137 #endif /* threading.h */