blob: 8fdb6334360744f2dfd005ed2658be49fde2ff3a [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakker2466d932013-09-28 14:40:38 +020021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker2466d932013-09-28 14:40:38 +020023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_THREADING_H
25#define MBEDTLS_THREADING_H
Paul Bakker2466d932013-09-28 14:40:38 +020026
Andres Amaya Garcia193fe892018-09-05 11:47:33 +010027/*
Hanno Becker48a816f2018-09-05 15:22:22 +010028 * Ensure gmtime_r is available even with -std=c99; must be defined before
Andres Amaya Garcia193fe892018-09-05 11:47:33 +010029 * config.h, which pulls in glibc's features.h. Harmless on other platforms.
30 */
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010031#if !defined(_POSIX_C_SOURCE)
Andres Amaya Garcia193fe892018-09-05 11:47:33 +010032#define _POSIX_C_SOURCE 200112L
Andres Amaya Garcia94b540a2018-09-05 12:27:32 +010033#endif
Andres Amaya Garcia193fe892018-09-05 11:47:33 +010034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker2466d932013-09-28 14:40:38 +020036#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020039#endif
Paul Bakker2466d932013-09-28 14:40:38 +020040
41#include <stdlib.h>
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A /**< The selected feature is not available. */
48#define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA -0x001C /**< Bad input parameters to function. */
49#define MBEDTLS_ERR_THREADING_MUTEX_ERROR -0x001E /**< Locking / unlocking / free failed with error code. */
Paul Bakker2466d932013-09-28 14:40:38 +020050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_THREADING_PTHREAD)
Paul Bakker2466d932013-09-28 14:40:38 +020052#include <pthread.h>
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020053typedef struct
54{
55 pthread_mutex_t mutex;
56 char is_valid;
57} mbedtls_threading_mutex_t;
Paul Bakker2466d932013-09-28 14:40:38 +020058#endif
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_THREADING_ALT)
61/* You should define the mbedtls_threading_mutex_t type in your header */
Paul Bakker2466d932013-09-28 14:40:38 +020062#include "threading_alt.h"
63
64/**
65 * \brief Set your alternate threading implementation function
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +020066 * pointers and initialize global mutexes. If used, this
67 * function must be called once in the main thread before any
68 * other mbed TLS function is called, and
69 * mbedtls_threading_free_alt() must be called once in the main
70 * thread after all other mbed TLS functions.
Paul Bakker2466d932013-09-28 14:40:38 +020071 *
Manuel Pégourié-Gonnard8f5fd312015-04-24 14:42:34 +020072 * \note mutex_init() and mutex_free() don't return a status code.
73 * If mutex_init() fails, it should leave its argument (the
74 * mutex) in a state such that mutex_lock() will fail when
75 * called with this argument.
76 *
Paul Bakker6838bd12013-09-30 13:56:38 +020077 * \param mutex_init the init function implementation
Paul Bakker2466d932013-09-28 14:40:38 +020078 * \param mutex_free the free function implementation
79 * \param mutex_lock the lock function implementation
80 * \param mutex_unlock the unlock function implementation
Paul Bakker2466d932013-09-28 14:40:38 +020081 */
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +020082void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
Manuel Pégourié-Gonnard8f5fd312015-04-24 14:42:34 +020083 void (*mutex_free)( mbedtls_threading_mutex_t * ),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
85 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) );
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +020086
87/**
88 * \brief Free global mutexes.
89 */
90void mbedtls_threading_free_alt( void );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#endif /* MBEDTLS_THREADING_ALT */
Paul Bakker2466d932013-09-28 14:40:38 +020092
Embedthis Software17ddff52015-09-10 11:45:13 -070093#if defined(MBEDTLS_THREADING_C)
Paul Bakker2466d932013-09-28 14:40:38 +020094/*
95 * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock
Paul Bakker6838bd12013-09-30 13:56:38 +020096 *
97 * All these functions are expected to work or the result will be undefined.
Paul Bakker2466d932013-09-28 14:40:38 +020098 */
Manuel Pégourié-Gonnard8f5fd312015-04-24 14:42:34 +020099extern void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t *mutex );
100extern void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t *mutex );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101extern int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t *mutex );
102extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex );
Paul Bakker2466d932013-09-28 14:40:38 +0200103
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200104/*
105 * Global mutexes
106 */
Gergely Budai13f7fb32017-08-23 14:23:58 +0200107#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200108extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex;
Gergely Budai13f7fb32017-08-23 14:23:58 +0200109#endif
Hanno Becker6a739782018-09-05 15:06:19 +0100110#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
Hanno Becker323d8012018-09-06 11:30:57 +0100111
Hanno Beckercfeb70c2018-09-05 13:50:22 +0100112#if !defined(_WIN32) && (defined(unix) || \
Andres Amaya Garcia433f9112018-09-05 12:01:57 +0100113 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
114 defined(__MACH__)))
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100115#include <unistd.h>
Hanno Becker323d8012018-09-06 11:30:57 +0100116#endif /* !_WIN32 && (unix || __unix || __unix__ ||
117 * (__APPLE__ && __MACH__)) */
118
Hanno Becker6f705812018-09-06 09:06:33 +0100119#if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
120 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
121 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) )
Andres Amaya Garciaca04a012018-09-05 11:43:57 +0100122/*
Hanno Becker7dd82b42018-09-05 16:25:50 +0100123 * The preprocessor conditions above are the same as in platform_util.c and
Andres Amaya Garciaca04a012018-09-05 11:43:57 +0100124 * threading.c. Remember to update the code there when changing the conditions
Hanno Becker651d5862018-09-05 15:17:43 +0100125 * here.
Andres Amaya Garciaca04a012018-09-05 11:43:57 +0100126 */
Hanno Beckerf5106d52018-09-06 12:09:56 +0100127#if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100128extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex;
Hanno Beckerf5106d52018-09-06 12:09:56 +0100129#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
130
Hanno Becker6f705812018-09-06 09:06:33 +0100131#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
132 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
133 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */
Hanno Becker6a739782018-09-05 15:06:19 +0100134#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
Paul Bakkere049ccd2016-05-10 16:17:27 +0100135#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200136
Paul Bakker2466d932013-09-28 14:40:38 +0200137#ifdef __cplusplus
138}
139#endif
140
141#endif /* threading.h */