blob: c22a1dadc510012e47a4f59c5bb0d6d90b4c5af7 [file] [log] [blame]
Paul Bakker2466d932013-09-28 14:40:38 +02001/*
2 * Threading abstraction layer
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker2466d932013-09-28 14:40:38 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker2466d932013-09-28 14:40:38 +020020 */
21
Andres Amaya Garciae9b10b22018-09-05 11:25:30 +010022/*
23 * Ensure gmtime_r is available even with -std=c99; must be included before
24 * config.h, which pulls in glibc's features.h. Harmless on other platforms.
25 */
26#define _POSIX_C_SOURCE 200112L
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Paul Bakker2466d932013-09-28 14:40:38 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_THREADING_C)
Paul Bakker2466d932013-09-28 14:40:38 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/threading.h"
Paul Bakker2466d932013-09-28 14:40:38 +020037
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +010038#if !defined(_WIN32) && (defined(__unix__) || \
39 (defined(__APPLE__) && defined(__MACH__)))
40#include <unistd.h>
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010041#if !defined(_POSIX_VERSION) || 200112L > _POSIX_THREAD_SAFE_FUNCTIONS
Andres Amaya Garciad7177432018-08-08 09:41:17 +010042#define THREADING_USE_GMTIME
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010043#endif /* !_POSIX_VERSION || 200112L > _POSIX_THREAD_SAFE_FUNCTIONS */
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +010044#endif /* !_WIN32 && (__unix__ || (__APPLE__ && __MACH__)) */
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020047static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020048{
Janos Follath6e876982016-10-28 14:59:12 +010049 if( mutex == NULL )
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020050 return;
Paul Bakker2466d932013-09-28 14:40:38 +020051
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020052 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
Paul Bakker2466d932013-09-28 14:40:38 +020053}
54
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020055static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020056{
Janos Follath5437a752016-09-26 09:15:44 +010057 if( mutex == NULL || !mutex->is_valid )
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020058 return;
Paul Bakker2466d932013-09-28 14:40:38 +020059
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020060 (void) pthread_mutex_destroy( &mutex->mutex );
Janos Follath5437a752016-09-26 09:15:44 +010061 mutex->is_valid = 0;
Paul Bakker2466d932013-09-28 14:40:38 +020062}
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020065{
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020066 if( mutex == NULL || ! mutex->is_valid )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakker2466d932013-09-28 14:40:38 +020068
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020069 if( pthread_mutex_lock( &mutex->mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker2466d932013-09-28 14:40:38 +020071
72 return( 0 );
73}
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020076{
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020077 if( mutex == NULL || ! mutex->is_valid )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakker2466d932013-09-28 14:40:38 +020079
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020080 if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker2466d932013-09-28 14:40:38 +020082
83 return( 0 );
84}
85
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020086void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
87void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
89int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +020090
91/*
92 * With phtreads we can statically initialize mutexes
93 */
94#define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#endif /* MBEDTLS_THREADING_PTHREAD */
Paul Bakker2466d932013-09-28 14:40:38 +020097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_THREADING_ALT)
99static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
Paul Bakkerc78c8422013-12-31 11:55:27 +0100100{
101 ((void) mutex );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakkerc78c8422013-12-31 11:55:27 +0100103}
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200104static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
105{
106 ((void) mutex );
107 return;
108}
Paul Bakkerc78c8422013-12-31 11:55:27 +0100109
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200110void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
111void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
113int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +0200114
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200115/*
116 * Set functions pointers and initialize global mutexes
117 */
118void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
119 void (*mutex_free)( mbedtls_threading_mutex_t * ),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
121 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
Paul Bakker2466d932013-09-28 14:40:38 +0200122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_mutex_init = mutex_init;
124 mbedtls_mutex_free = mutex_free;
125 mbedtls_mutex_lock = mutex_lock;
126 mbedtls_mutex_unlock = mutex_unlock;
Paul Bakker2466d932013-09-28 14:40:38 +0200127
Gergely Budai13f7fb32017-08-23 14:23:58 +0200128#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200129 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
Gergely Budai13f7fb32017-08-23 14:23:58 +0200130#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100131#if defined(THREADING_USE_GMTIME)
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100132 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
133#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200134}
135
136/*
137 * Free global mutexes
138 */
139void mbedtls_threading_free_alt( void )
140{
Gergely Budai13f7fb32017-08-23 14:23:58 +0200141#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200142 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
Gergely Budai13f7fb32017-08-23 14:23:58 +0200143#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100144#if defined(THREADING_USE_GMTIME)
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100145 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
146#endif
Paul Bakker2466d932013-09-28 14:40:38 +0200147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#endif /* MBEDTLS_THREADING_ALT */
Paul Bakker2466d932013-09-28 14:40:38 +0200149
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200150/*
151 * Define global mutexes
152 */
153#ifndef MUTEX_INIT
154#define MUTEX_INIT
155#endif
Gergely Budai13f7fb32017-08-23 14:23:58 +0200156#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200157mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
Gergely Budai13f7fb32017-08-23 14:23:58 +0200158#endif
Andres Amaya Garciad7177432018-08-08 09:41:17 +0100159#if defined(THREADING_USE_GMTIME)
Andres Amaya Garciace6eebb2018-08-07 20:26:55 +0100160mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
161#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#endif /* MBEDTLS_THREADING_C */