• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * \file threading.h
3  *
4  * \brief Threading abstraction layer
5  */
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 
13 #if !defined(MBEDTLS_CONFIG_FILE)
14 #include "mbedtls/config.h"
15 #else
16 #include MBEDTLS_CONFIG_FILE
17 #endif
18 
19 #include <stdlib.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /* MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE is deprecated and should not be
26  * used. */
27 /** The selected feature is not available. */
28 #define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE         -0x001A
29 
30 /** Bad input parameters to function. */
31 #define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA              -0x001C
32 /** Locking / unlocking / free failed with error code. */
33 #define MBEDTLS_ERR_THREADING_MUTEX_ERROR                 -0x001E
34 
35 #if defined(MBEDTLS_THREADING_PTHREAD)
36 #include <pthread.h>
37 typedef struct mbedtls_threading_mutex_t {
38     pthread_mutex_t mutex;
39     /* is_valid is 0 after a failed init or a free, and nonzero after a
40      * successful init. This field is not considered part of the public
41      * API of Mbed TLS and may change without notice. */
42     char is_valid;
43 } mbedtls_threading_mutex_t;
44 #endif
45 
46 #if defined(MBEDTLS_THREADING_ALT)
47 /* You should define the mbedtls_threading_mutex_t type in your header */
48 #include "threading_alt.h"
49 
50 /**
51  * \brief           Set your alternate threading implementation function
52  *                  pointers and initialize global mutexes. If used, this
53  *                  function must be called once in the main thread before any
54  *                  other Mbed TLS function is called, and
55  *                  mbedtls_threading_free_alt() must be called once in the main
56  *                  thread after all other Mbed TLS functions.
57  *
58  * \note            mutex_init() and mutex_free() don't return a status code.
59  *                  If mutex_init() fails, it should leave its argument (the
60  *                  mutex) in a state such that mutex_lock() will fail when
61  *                  called with this argument.
62  *
63  * \param mutex_init    the init function implementation
64  * \param mutex_free    the free function implementation
65  * \param mutex_lock    the lock function implementation
66  * \param mutex_unlock  the unlock function implementation
67  */
68 void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
69                                void (*mutex_free)(mbedtls_threading_mutex_t *),
70                                int (*mutex_lock)(mbedtls_threading_mutex_t *),
71                                int (*mutex_unlock)(mbedtls_threading_mutex_t *));
72 
73 /**
74  * \brief               Free global mutexes.
75  */
76 void mbedtls_threading_free_alt(void);
77 #endif /* MBEDTLS_THREADING_ALT */
78 
79 #if defined(MBEDTLS_THREADING_C)
80 /*
81  * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock
82  *
83  * All these functions are expected to work or the result will be undefined.
84  */
85 extern void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *mutex);
86 extern void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *mutex);
87 extern int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *mutex);
88 extern int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *mutex);
89 
90 /*
91  * Global mutexes
92  */
93 #if defined(MBEDTLS_FS_IO)
94 extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
95 #endif
96 
97 #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
98 /* This mutex may or may not be used in the default definition of
99  * mbedtls_platform_gmtime_r(), but in order to determine that,
100  * we need to check POSIX features, hence modify _POSIX_C_SOURCE.
101  * With the current approach, this declaration is orphaned, lacking
102  * an accompanying definition, in case mbedtls_platform_gmtime_r()
103  * doesn't need it, but that's not a problem. */
104 extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
105 #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
106 
107 #endif /* MBEDTLS_THREADING_C */
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 #endif /* threading.h */
114