Mbed TLS v4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
threading.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright The Mbed TLS Contributors
8  * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  */
10 #ifndef MBEDTLS_THREADING_H
11 #define MBEDTLS_THREADING_H
12 #include "mbedtls/private_access.h"
13 
16 
17 #include <stdlib.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
30 #define MBEDTLS_ERR_THREADING_USAGE_ERROR -0x001E
31 
33 #define MBEDTLS_ERR_THREADING_MUTEX_ERROR MBEDTLS_ERR_THREADING_USAGE_ERROR
34 
35 #if defined(MBEDTLS_THREADING_C)
36 
37 #if defined(MBEDTLS_THREADING_PTHREAD)
38 #include <pthread.h>
39 typedef pthread_mutex_t mbedtls_platform_mutex_t;
41 #endif
42 
43 #if defined(MBEDTLS_THREADING_ALT)
44 /* You should define the types mbedtls_platform_mutex_t and
45  * mbedtls_platform_condition_variable_t in your header. */
46 #include "threading_alt.h"
47 
110  int (*mutex_init)(mbedtls_platform_mutex_t *),
111  void (*mutex_destroy)(mbedtls_platform_mutex_t *),
112  int (*mutex_lock)(mbedtls_platform_mutex_t *),
113  int (*mutex_unlock)(mbedtls_platform_mutex_t *),
114  int (*cond_init)(mbedtls_platform_condition_variable_t *),
115  void (*cond_destroy)(mbedtls_platform_condition_variable_t *),
116  int (*cond_signal)(mbedtls_platform_condition_variable_t *),
117  int (*cond_broadcast)(mbedtls_platform_condition_variable_t *),
118  int (*cond_wait)(mbedtls_platform_condition_variable_t *,
119  mbedtls_platform_mutex_t *));
120 
124 void mbedtls_threading_free_alt(void);
125 #endif /* MBEDTLS_THREADING_ALT */
126 
128  mbedtls_platform_mutex_t MBEDTLS_PRIVATE(mutex);
129 
130  /* Whether the mutex has been initialized successfully.
131  *
132  * Attempting to lock or destroy a platform mutex that hasn't been
133  * successfully initialized can cause a crash or other undefined
134  * behavior on some platforms. Keeping track of a successful
135  * initialization makes it possible to turn such misuse into
136  * a predictable error. This is especially useful because
137  * mbedtls_mutex_init() doesn't return an error code, for
138  * historical reasons, so the application cannot handle such
139  * failures by itself.
140  */
141  char MBEDTLS_PRIVATE(initialized);
142 
143  /* WARNING - state should only be accessed when holding the mutex lock in
144  * framework/tests/src/threading_helpers.c, otherwise corruption can occur.
145  * state will be 0 after a failed init or a free, and nonzero after a
146  * successful init. This field is for testing only and thus not considered
147  * part of the public API of Mbed TLS and may change without notice.*/
148  char MBEDTLS_PRIVATE(state);
149 
151 
153  mbedtls_platform_condition_variable_t MBEDTLS_PRIVATE(cond);
155 
175 
196 
227 
255 
280 
281 
297 
323 
347 
388 
389 /*
390  * Global mutexes
391  */
392 #if defined(MBEDTLS_FS_IO)
394 #endif
395 
396 #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
397 /* This mutex may or may not be used in the default definition of
398  * mbedtls_platform_gmtime_r(), but in order to determine that,
399  * we need to check POSIX features, hence modify _POSIX_C_SOURCE.
400  * With the current approach, this declaration is orphaned, lacking
401  * an accompanying definition, in case mbedtls_platform_gmtime_r()
402  * doesn't need it, but that's not a problem. */
403 extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
404 #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
405 
406 #endif /* MBEDTLS_THREADING_C */
407 
408 #ifdef __cplusplus
409 }
410 #endif
411 
412 #endif /* threading.h */
mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex
void mbedtls_threading_free_alt(void)
Free global mutexes.
void mbedtls_mutex_init(mbedtls_threading_mutex_t *mutex)
void mbedtls_mutex_free(mbedtls_threading_mutex_t *mutex)
struct mbedtls_threading_mutex_t mbedtls_threading_mutex_t
int mbedtls_mutex_lock(mbedtls_threading_mutex_t *mutex)
void mbedtls_condition_variable_free(mbedtls_threading_condition_variable_t *cond)
int mbedtls_condition_variable_broadcast(mbedtls_threading_condition_variable_t *cond)
int mbedtls_condition_variable_init(mbedtls_threading_condition_variable_t *cond)
struct mbedtls_threading_condition_variable_t mbedtls_threading_condition_variable_t
Compatibility definitions for MbedTLS 3.x code built with MbedTLS 4.x or TF-PSA-Crypto 1...
int mbedtls_condition_variable_wait(mbedtls_threading_condition_variable_t *cond, mbedtls_threading_mutex_t *mutex)
void mbedtls_threading_set_alt(int(*mutex_init)(mbedtls_platform_mutex_t *), void(*mutex_destroy)(mbedtls_platform_mutex_t *), int(*mutex_lock)(mbedtls_platform_mutex_t *), int(*mutex_unlock)(mbedtls_platform_mutex_t *), int(*cond_init)(mbedtls_platform_condition_variable_t *), void(*cond_destroy)(mbedtls_platform_condition_variable_t *), int(*cond_signal)(mbedtls_platform_condition_variable_t *), int(*cond_broadcast)(mbedtls_platform_condition_variable_t *), int(*cond_wait)(mbedtls_platform_condition_variable_t *, mbedtls_platform_mutex_t *))
Set your alternate threading implementation function pointers and initialize global mutexes...
pthread_mutex_t mbedtls_platform_mutex_t
Definition: threading.h:39
Build-time configuration info.
int mbedtls_mutex_unlock(mbedtls_threading_mutex_t *mutex)
int mbedtls_condition_variable_signal(mbedtls_threading_condition_variable_t *cond)
pthread_cond_t mbedtls_platform_condition_variable_t
Definition: threading.h:40