blob: 2a03afeef9c350c1d08a805042423e9bc31f2f87 [file] [log] [blame]
Paul Bakker2466d932013-09-28 14:40:38 +02001/**
2 * \file threading.h
3 *
4 * \brief Threading abstraction layer
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker2466d932013-09-28 14:40:38 +02009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_THREADING_H
11#define MBEDTLS_THREADING_H
Paul Bakker2466d932013-09-28 14:40:38 +020012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010014#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020015#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020017#endif
Paul Bakker2466d932013-09-28 14:40:38 +020018
19#include <stdlib.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
Ron Eldor9924bdc2018-10-04 10:59:13 +030025/* MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE is deprecated and should not be
26 * used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020027/** The selected feature is not available. */
28#define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A
Ron Eldor9924bdc2018-10-04 10:59:13 +030029
Gilles Peskinea3974432021-07-26 18:48:10 +020030/** 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
Paul Bakker2466d932013-09-28 14:40:38 +020034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_THREADING_PTHREAD)
Paul Bakker2466d932013-09-28 14:40:38 +020036#include <pthread.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010037typedef struct mbedtls_threading_mutex_t {
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020038 pthread_mutex_t mutex;
Gilles Peskine39a1a262021-02-09 15:35:29 +010039 /* 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. */
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020042 char is_valid;
43} mbedtls_threading_mutex_t;
Paul Bakker2466d932013-09-28 14:40:38 +020044#endif
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_THREADING_ALT)
47/* You should define the mbedtls_threading_mutex_t type in your header */
Paul Bakker2466d932013-09-28 14:40:38 +020048#include "threading_alt.h"
49
50/**
51 * \brief Set your alternate threading implementation function
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +020052 * pointers and initialize global mutexes. If used, this
53 * function must be called once in the main thread before any
Gilles Peskinef08ca832023-09-12 19:21:54 +020054 * other Mbed TLS function is called, and
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +020055 * mbedtls_threading_free_alt() must be called once in the main
Gilles Peskinef08ca832023-09-12 19:21:54 +020056 * thread after all other Mbed TLS functions.
Paul Bakker2466d932013-09-28 14:40:38 +020057 *
Manuel Pégourié-Gonnard8f5fd312015-04-24 14:42:34 +020058 * \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 *
Paul Bakker6838bd12013-09-30 13:56:38 +020063 * \param mutex_init the init function implementation
Paul Bakker2466d932013-09-28 14:40:38 +020064 * \param mutex_free the free function implementation
65 * \param mutex_lock the lock function implementation
66 * \param mutex_unlock the unlock function implementation
Paul Bakker2466d932013-09-28 14:40:38 +020067 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068void 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 *));
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +020072
73/**
74 * \brief Free global mutexes.
75 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076void mbedtls_threading_free_alt(void);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#endif /* MBEDTLS_THREADING_ALT */
Paul Bakker2466d932013-09-28 14:40:38 +020078
Embedthis Software17ddff52015-09-10 11:45:13 -070079#if defined(MBEDTLS_THREADING_C)
Paul Bakker2466d932013-09-28 14:40:38 +020080/*
81 * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock
Paul Bakker6838bd12013-09-30 13:56:38 +020082 *
83 * All these functions are expected to work or the result will be undefined.
Paul Bakker2466d932013-09-28 14:40:38 +020084 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085extern void (*mbedtls_mutex_init)(mbedtls_threading_mutex_t *mutex);
86extern void (*mbedtls_mutex_free)(mbedtls_threading_mutex_t *mutex);
87extern int (*mbedtls_mutex_lock)(mbedtls_threading_mutex_t *mutex);
88extern int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *mutex);
Paul Bakker2466d932013-09-28 14:40:38 +020089
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +020090/*
91 * Global mutexes
92 */
Gergely Budai13f7fb32017-08-23 14:23:58 +020093#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +020094extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
Gergely Budai13f7fb32017-08-23 14:23:58 +020095#endif
Hanno Beckerd2ef2542018-09-06 14:53:25 +010096
Hanno Becker6a739782018-09-05 15:06:19 +010097#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
Hanno Beckerd2ef2542018-09-06 14:53:25 +010098/* 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. */
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100104extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
Hanno Becker6a739782018-09-05 15:06:19 +0100105#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
Hanno Beckerd2ef2542018-09-06 14:53:25 +0100106
Paul Bakkere049ccd2016-05-10 16:17:27 +0100107#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200108
Paul Bakker2466d932013-09-28 14:40:38 +0200109#ifdef __cplusplus
110}
111#endif
112
113#endif /* threading.h */