Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Threading abstraction layer |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 5 | * 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é-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 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 Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 24 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 25 | * ********** |
| 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é-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 46 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 47 | */ |
| 48 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 50 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 51 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 53 | #endif |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_THREADING_C) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 57 | #include "mbedtls/threading.h" |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_THREADING_PTHREAD) |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 60 | static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex ) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 61 | { |
Janos Follath | 6e87698 | 2016-10-28 14:59:12 +0100 | [diff] [blame] | 62 | if( mutex == NULL ) |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 63 | return; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 64 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 65 | mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 68 | static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex ) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 69 | { |
Janos Follath | 5437a75 | 2016-09-26 09:15:44 +0100 | [diff] [blame] | 70 | if( mutex == NULL || !mutex->is_valid ) |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 71 | return; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 72 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 73 | (void) pthread_mutex_destroy( &mutex->mutex ); |
Janos Follath | 5437a75 | 2016-09-26 09:15:44 +0100 | [diff] [blame] | 74 | mutex->is_valid = 0; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 77 | static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex ) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 78 | { |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 79 | if( mutex == NULL || ! mutex->is_valid ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 81 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 82 | if( pthread_mutex_lock( &mutex->mutex ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 84 | |
| 85 | return( 0 ); |
| 86 | } |
| 87 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex ) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 89 | { |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 90 | if( mutex == NULL || ! mutex->is_valid ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 91 | return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 92 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 93 | if( pthread_mutex_unlock( &mutex->mutex ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 95 | |
| 96 | return( 0 ); |
| 97 | } |
| 98 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 99 | void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread; |
| 100 | void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread; |
| 102 | int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread; |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * With phtreads we can statically initialize mutexes |
| 106 | */ |
| 107 | #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 } |
| 108 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | #endif /* MBEDTLS_THREADING_PTHREAD */ |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | #if defined(MBEDTLS_THREADING_ALT) |
| 112 | static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex ) |
Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 113 | { |
| 114 | ((void) mutex ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); |
Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 116 | } |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 117 | static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex ) |
| 118 | { |
| 119 | ((void) mutex ); |
| 120 | return; |
| 121 | } |
Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 122 | |
Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 123 | void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; |
| 124 | void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; |
| 126 | int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 127 | |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 128 | /* |
| 129 | * Set functions pointers and initialize global mutexes |
| 130 | */ |
| 131 | void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ), |
| 132 | void (*mutex_free)( mbedtls_threading_mutex_t * ), |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | int (*mutex_lock)( mbedtls_threading_mutex_t * ), |
| 134 | int (*mutex_unlock)( mbedtls_threading_mutex_t * ) ) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 135 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | mbedtls_mutex_init = mutex_init; |
| 137 | mbedtls_mutex_free = mutex_free; |
| 138 | mbedtls_mutex_lock = mutex_lock; |
| 139 | mbedtls_mutex_unlock = mutex_unlock; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 140 | |
Gergely Budai | 8190678 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 141 | #if defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 142 | mbedtls_mutex_init( &mbedtls_threading_readdir_mutex ); |
Gergely Budai | 8190678 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 143 | #endif |
| 144 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 145 | mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex ); |
Gergely Budai | 8190678 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 146 | #endif |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Free global mutexes |
| 151 | */ |
| 152 | void mbedtls_threading_free_alt( void ) |
| 153 | { |
Gergely Budai | 8190678 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 154 | #if defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 155 | mbedtls_mutex_free( &mbedtls_threading_readdir_mutex ); |
Gergely Budai | 8190678 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 156 | #endif |
| 157 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 158 | mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex ); |
Gergely Budai | 8190678 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 159 | #endif |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 160 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | #endif /* MBEDTLS_THREADING_ALT */ |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 162 | |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 163 | /* |
| 164 | * Define global mutexes |
| 165 | */ |
| 166 | #ifndef MUTEX_INIT |
| 167 | #define MUTEX_INIT |
| 168 | #endif |
Gergely Budai | 8190678 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 169 | #if defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 170 | mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT; |
Gergely Budai | 8190678 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 171 | #endif |
| 172 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 173 | mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT; |
Gergely Budai | 8190678 | 2017-08-23 14:23:58 +0200 | [diff] [blame] | 174 | #endif |
Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | #endif /* MBEDTLS_THREADING_C */ |