blob: 838cd74d93df1f8333a720a99ebf88aee8150f3e [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
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020063 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
Paul Bakker2466d932013-09-28 14:40:38 +020064}
65
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020066static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020067{
Janos Follath5437a752016-09-26 09:15:44 +010068 if( mutex == NULL || !mutex->is_valid )
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020069 return;
Paul Bakker2466d932013-09-28 14:40:38 +020070
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020071 (void) pthread_mutex_destroy( &mutex->mutex );
Janos Follath5437a752016-09-26 09:15:44 +010072 mutex->is_valid = 0;
Paul Bakker2466d932013-09-28 14:40:38 +020073}
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075static int threading_mutex_lock_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_lock( &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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020086static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
Paul Bakker2466d932013-09-28 14:40:38 +020087{
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020088 if( mutex == NULL || ! mutex->is_valid )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakker2466d932013-09-28 14:40:38 +020090
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020091 if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Paul Bakker2466d932013-09-28 14:40:38 +020093
94 return( 0 );
95}
96
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +020097void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
98void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
100int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200101
102/*
103 * With phtreads we can statically initialize mutexes
104 */
105#define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#endif /* MBEDTLS_THREADING_PTHREAD */
Paul Bakker2466d932013-09-28 14:40:38 +0200108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if defined(MBEDTLS_THREADING_ALT)
110static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
Paul Bakkerc78c8422013-12-31 11:55:27 +0100111{
112 ((void) mutex );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
Paul Bakkerc78c8422013-12-31 11:55:27 +0100114}
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200115static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
116{
117 ((void) mutex );
118 return;
119}
Paul Bakkerc78c8422013-12-31 11:55:27 +0100120
Manuel Pégourié-Gonnard1e2eae02015-04-29 01:26:03 +0200121void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
122void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
124int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +0200125
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200126/*
127 * Set functions pointers and initialize global mutexes
128 */
129void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
130 void (*mutex_free)( mbedtls_threading_mutex_t * ),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
132 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
Paul Bakker2466d932013-09-28 14:40:38 +0200133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_mutex_init = mutex_init;
135 mbedtls_mutex_free = mutex_free;
136 mbedtls_mutex_lock = mutex_lock;
137 mbedtls_mutex_unlock = mutex_unlock;
Paul Bakker2466d932013-09-28 14:40:38 +0200138
Gergely Budai81906782017-08-23 14:23:58 +0200139#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200140 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
Gergely Budai81906782017-08-23 14:23:58 +0200141#endif
142#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200143 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
Gergely Budai81906782017-08-23 14:23:58 +0200144#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200145}
146
147/*
148 * Free global mutexes
149 */
150void mbedtls_threading_free_alt( void )
151{
Gergely Budai81906782017-08-23 14:23:58 +0200152#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200153 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
Gergely Budai81906782017-08-23 14:23:58 +0200154#endif
155#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200156 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
Gergely Budai81906782017-08-23 14:23:58 +0200157#endif
Paul Bakker2466d932013-09-28 14:40:38 +0200158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#endif /* MBEDTLS_THREADING_ALT */
Paul Bakker2466d932013-09-28 14:40:38 +0200160
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200161/*
162 * Define global mutexes
163 */
164#ifndef MUTEX_INIT
165#define MUTEX_INIT
166#endif
Gergely Budai81906782017-08-23 14:23:58 +0200167#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200168mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
Gergely Budai81906782017-08-23 14:23:58 +0200169#endif
170#if defined(MBEDTLS_HAVE_TIME_DATE)
Manuel Pégourié-Gonnard864108d2015-05-29 10:11:03 +0200171mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
Gergely Budai81906782017-08-23 14:23:58 +0200172#endif
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +0200173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#endif /* MBEDTLS_THREADING_C */