blob: 68667a3a892dad3e15fe493801d77d79a6bcb5fc [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker2466d932013-09-28 14:40:38 +020024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker2466d932013-09-28 14:40:38 +020047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker2466d932013-09-28 14:40:38 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_THREADING_C)
Paul Bakker2466d932013-09-28 14:40:38 +020056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/threading.h"
Paul Bakker2466d932013-09-28 14:40:38 +020058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020060static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020061{
Janos Follath6e876982016-10-28 14:59:12 +010062 if( mutex == NULL )
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020063 return;
Paul Bakker2466d932013-09-28 14:40:38 +020064
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020065 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
Paul Bakker2466d932013-09-28 14:40:38 +020066}
67
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020068static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020069{
Janos Follath5437a752016-09-26 09:15:44 +010070 if( mutex == NULL || !mutex->is_valid )
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020071 return;
Paul Bakker2466d932013-09-28 14:40:38 +020072
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020073 (void) pthread_mutex_destroy( &mutex->mutex );
Janos Follath5437a752016-09-26 09:15:44 +010074 mutex->is_valid = 0;
Paul Bakker2466d932013-09-28 14:40:38 +020075}
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020078{
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020079 if( mutex == NULL || ! mutex->is_valid )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakker2466d932013-09-28 14:40:38 +020081
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020082 if( pthread_mutex_lock( &mutex->mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker2466d932013-09-28 14:40:38 +020084
85 return( 0 );
86}
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020089{
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020090 if( mutex == NULL || ! mutex->is_valid )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakker2466d932013-09-28 14:40:38 +020092
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020093 if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker2466d932013-09-28 14:40:38 +020095
96 return( 0 );
97}
98
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020099void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
100void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
102int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200103
104/*
105 * With phtreads we can statically initialize mutexes
106 */
107#define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#endif /* MBEDTLS_THREADING_PTHREAD */
Paul Bakker2466d932013-09-28 14:40:38 +0200110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_THREADING_ALT)
112static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
Paul Bakkerc78c8422013-12-31 11:55:27 +0100113{
114 ((void) mutex );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakkerc78c8422013-12-31 11:55:27 +0100116}
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200117static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
118{
119 ((void) mutex );
120 return;
121}
Paul Bakkerc78c8422013-12-31 11:55:27 +0100122
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200123void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
124void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
126int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +0200127
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200128/*
129 * Set functions pointers and initialize global mutexes
130 */
131void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
132 void (*mutex_free)( mbedtls_threading_mutex_t * ),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
134 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
Paul Bakker2466d932013-09-28 14:40:38 +0200135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_mutex_init = mutex_init;
137 mbedtls_mutex_free = mutex_free;
138 mbedtls_mutex_lock = mutex_lock;
139 mbedtls_mutex_unlock = mutex_unlock;
Paul Bakker2466d932013-09-28 14:40:38 +0200140
Gergely Budai81906782017-08-23 14:23:58 +0200141#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200142 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
Gergely Budai81906782017-08-23 14:23:58 +0200143#endif
144#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200145 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
Gergely Budai81906782017-08-23 14:23:58 +0200146#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200147}
148
149/*
150 * Free global mutexes
151 */
152void mbedtls_threading_free_alt( void )
153{
Gergely Budai81906782017-08-23 14:23:58 +0200154#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200155 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
Gergely Budai81906782017-08-23 14:23:58 +0200156#endif
157#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200158 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
Gergely Budai81906782017-08-23 14:23:58 +0200159#endif
Paul Bakker2466d932013-09-28 14:40:38 +0200160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#endif /* MBEDTLS_THREADING_ALT */
Paul Bakker2466d932013-09-28 14:40:38 +0200162
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200163/*
164 * Define global mutexes
165 */
166#ifndef MUTEX_INIT
167#define MUTEX_INIT
168#endif
Gergely Budai81906782017-08-23 14:23:58 +0200169#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200170mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
Gergely Budai81906782017-08-23 14:23:58 +0200171#endif
172#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200173mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
Gergely Budai81906782017-08-23 14:23:58 +0200174#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_THREADING_C */