4 * \brief Threading abstraction layer
7 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8 * SPDX-License-Identifier: GPL-2.0
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * This file is part of mbed TLS (https://tls.mbed.org)
26 #ifndef MBEDTLS_THREADING_H
27 #define MBEDTLS_THREADING_H
29 #if !defined(MBEDTLS_CONFIG_FILE)
32 #include MBEDTLS_CONFIG_FILE
41 #define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A /**< The selected feature is not available. */
42 #define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA -0x001C /**< Bad input parameters to function. */
43 #define MBEDTLS_ERR_THREADING_MUTEX_ERROR -0x001E /**< Locking / unlocking / free failed with error code. */
45 #if defined(MBEDTLS_THREADING_PTHREAD)
47 typedef struct mbedtls_threading_mutex_t
49 pthread_mutex_t mutex
;
51 } mbedtls_threading_mutex_t
;
54 #if defined(MBEDTLS_THREADING_ALT)
55 /* You should define the mbedtls_threading_mutex_t type in your header */
56 #include "threading_alt.h"
59 * \brief Set your alternate threading implementation function
60 * pointers and initialize global mutexes. If used, this
61 * function must be called once in the main thread before any
62 * other mbed TLS function is called, and
63 * mbedtls_threading_free_alt() must be called once in the main
64 * thread after all other mbed TLS functions.
66 * \note mutex_init() and mutex_free() don't return a status code.
67 * If mutex_init() fails, it should leave its argument (the
68 * mutex) in a state such that mutex_lock() will fail when
69 * called with this argument.
71 * \param mutex_init the init function implementation
72 * \param mutex_free the free function implementation
73 * \param mutex_lock the lock function implementation
74 * \param mutex_unlock the unlock function implementation
76 void mbedtls_threading_set_alt( void (*mutex_init
)( mbedtls_threading_mutex_t
* ),
77 void (*mutex_free
)( mbedtls_threading_mutex_t
* ),
78 int (*mutex_lock
)( mbedtls_threading_mutex_t
* ),
79 int (*mutex_unlock
)( mbedtls_threading_mutex_t
* ) );
82 * \brief Free global mutexes.
84 void mbedtls_threading_free_alt( void );
85 #endif /* MBEDTLS_THREADING_ALT */
87 #if defined(MBEDTLS_THREADING_C)
89 * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock
91 * All these functions are expected to work or the result will be undefined.
93 extern void (*mbedtls_mutex_init
)( mbedtls_threading_mutex_t
*mutex
);
94 extern void (*mbedtls_mutex_free
)( mbedtls_threading_mutex_t
*mutex
);
95 extern int (*mbedtls_mutex_lock
)( mbedtls_threading_mutex_t
*mutex
);
96 extern int (*mbedtls_mutex_unlock
)( mbedtls_threading_mutex_t
*mutex
);
101 #if defined(MBEDTLS_FS_IO)
102 extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex
;
104 #endif /* MBEDTLS_THREADING_C */
110 #endif /* threading.h */