blob: df16fe72f0cab6adbc2d1ed369dda31858a1fb74 [file] [log] [blame]
Paul Bakker2466d932013-09-28 14:40:38 +02001/*
2 * Threading abstraction layer
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
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 * **********
Paul Bakker2466d932013-09-28 14:40:38 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker2466d932013-09-28 14:40:38 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_THREADING_C)
Paul Bakker2466d932013-09-28 14:40:38 +020054
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/threading.h"
Paul Bakker2466d932013-09-28 14:40:38 +020056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020058static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020059{
Janos Follath6e876982016-10-28 14:59:12 +010060 if( mutex == NULL )
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020061 return;
Paul Bakker2466d932013-09-28 14:40:38 +020062
Gilles Peskine57107322021-02-09 15:35:29 +010063 /* A nonzero value of is_valid indicates a successfully initialized
64 * mutex. This is a workaround for not being able to return an error
65 * code for this function. The lock/unlock functions return an error
66 * if is_valid is nonzero. The Mbed TLS unit test code uses this field
67 * to distinguish more states of the mutex; see helpers.function for
68 * details. */
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020069 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
Paul Bakker2466d932013-09-28 14:40:38 +020070}
71
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020072static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020073{
Janos Follath5437a752016-09-26 09:15:44 +010074 if( mutex == NULL || !mutex->is_valid )
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020075 return;
Paul Bakker2466d932013-09-28 14:40:38 +020076
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020077 (void) pthread_mutex_destroy( &mutex->mutex );
Janos Follath5437a752016-09-26 09:15:44 +010078 mutex->is_valid = 0;
Paul Bakker2466d932013-09-28 14:40:38 +020079}
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020082{
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020083 if( mutex == NULL || ! mutex->is_valid )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakker2466d932013-09-28 14:40:38 +020085
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020086 if( pthread_mutex_lock( &mutex->mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker2466d932013-09-28 14:40:38 +020088
89 return( 0 );
90}
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020093{
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020094 if( mutex == NULL || ! mutex->is_valid )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakker2466d932013-09-28 14:40:38 +020096
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020097 if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker2466d932013-09-28 14:40:38 +020099
100 return( 0 );
101}
102
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200103void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
104void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
106int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200107
108/*
109 * With phtreads we can statically initialize mutexes
110 */
111#define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#endif /* MBEDTLS_THREADING_PTHREAD */
Paul Bakker2466d932013-09-28 14:40:38 +0200114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_THREADING_ALT)
116static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
Paul Bakkerc78c8422013-12-31 11:55:27 +0100117{
118 ((void) mutex );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakkerc78c8422013-12-31 11:55:27 +0100120}
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200121static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
122{
123 ((void) mutex );
124 return;
125}
Paul Bakkerc78c8422013-12-31 11:55:27 +0100126
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200127void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
128void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
130int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +0200131
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200132/*
133 * Set functions pointers and initialize global mutexes
134 */
135void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
136 void (*mutex_free)( mbedtls_threading_mutex_t * ),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
138 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
Paul Bakker2466d932013-09-28 14:40:38 +0200139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_mutex_init = mutex_init;
141 mbedtls_mutex_free = mutex_free;
142 mbedtls_mutex_lock = mutex_lock;
143 mbedtls_mutex_unlock = mutex_unlock;
Paul Bakker2466d932013-09-28 14:40:38 +0200144
Gergely Budai81906782017-08-23 14:23:58 +0200145#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200146 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
Gergely Budai81906782017-08-23 14:23:58 +0200147#endif
148#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200149 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
Gergely Budai81906782017-08-23 14:23:58 +0200150#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200151}
152
153/*
154 * Free global mutexes
155 */
156void mbedtls_threading_free_alt( void )
157{
Gergely Budai81906782017-08-23 14:23:58 +0200158#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200159 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
Gergely Budai81906782017-08-23 14:23:58 +0200160#endif
161#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200162 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
Gergely Budai81906782017-08-23 14:23:58 +0200163#endif
Paul Bakker2466d932013-09-28 14:40:38 +0200164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#endif /* MBEDTLS_THREADING_ALT */
Paul Bakker2466d932013-09-28 14:40:38 +0200166
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200167/*
168 * Define global mutexes
169 */
170#ifndef MUTEX_INIT
171#define MUTEX_INIT
172#endif
Gergely Budai81906782017-08-23 14:23:58 +0200173#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200174mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
Gergely Budai81906782017-08-23 14:23:58 +0200175#endif
176#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200177mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
Gergely Budai81906782017-08-23 14:23:58 +0200178#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#endif /* MBEDTLS_THREADING_C */