| 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 | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  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 Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 18 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 20 | */ | 
|  | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 27 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_THREADING_C) | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 29 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/threading.h" | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 31 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_THREADING_PTHREAD) | 
| Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 33 | static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex ) | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 34 | { | 
|  | 35 | if( mutex == NULL ) | 
| Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 36 | return; | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 37 |  | 
| Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 38 | mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0; | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 39 | } | 
|  | 40 |  | 
| Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 41 | static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex ) | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 42 | { | 
|  | 43 | if( mutex == NULL ) | 
| Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 44 | return; | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 45 |  | 
| Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 46 | (void) pthread_mutex_destroy( &mutex->mutex ); | 
| 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 | static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex ) | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 50 | { | 
| Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 51 | if( mutex == NULL || ! mutex->is_valid ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 53 |  | 
| Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 54 | if( pthread_mutex_lock( &mutex->mutex ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 56 |  | 
|  | 57 | return( 0 ); | 
|  | 58 | } | 
|  | 59 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex ) | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 61 | { | 
| Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 62 | if( mutex == NULL || ! mutex->is_valid ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); | 
| 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 | if( pthread_mutex_unlock( &mutex->mutex ) != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 67 |  | 
|  | 68 | return( 0 ); | 
|  | 69 | } | 
|  | 70 |  | 
| Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 71 | void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread; | 
|  | 72 | 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] | 73 | int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread; | 
|  | 74 | 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] | 75 |  | 
|  | 76 | /* | 
|  | 77 | * With phtreads we can statically initialize mutexes | 
|  | 78 | */ | 
|  | 79 | #define MUTEX_INIT  = { PTHREAD_MUTEX_INITIALIZER, 1 } | 
|  | 80 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | #endif /* MBEDTLS_THREADING_PTHREAD */ | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 82 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | #if defined(MBEDTLS_THREADING_ALT) | 
|  | 84 | static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex ) | 
| Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 85 | { | 
|  | 86 | ((void) mutex ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); | 
| Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 88 | } | 
| Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 89 | static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex ) | 
|  | 90 | { | 
|  | 91 | ((void) mutex ); | 
|  | 92 | return; | 
|  | 93 | } | 
| Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 94 |  | 
| Manuel Pégourié-Gonnard | 1e2eae0 | 2015-04-29 01:26:03 +0200 | [diff] [blame] | 95 | void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; | 
|  | 96 | 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] | 97 | int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; | 
|  | 98 | int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 99 |  | 
| Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 100 | /* | 
|  | 101 | * Set functions pointers and initialize global mutexes | 
|  | 102 | */ | 
|  | 103 | void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ), | 
|  | 104 | void (*mutex_free)( mbedtls_threading_mutex_t * ), | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | int (*mutex_lock)( mbedtls_threading_mutex_t * ), | 
|  | 106 | int (*mutex_unlock)( mbedtls_threading_mutex_t * ) ) | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 107 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | mbedtls_mutex_init = mutex_init; | 
|  | 109 | mbedtls_mutex_free = mutex_free; | 
|  | 110 | mbedtls_mutex_lock = mutex_lock; | 
|  | 111 | mbedtls_mutex_unlock = mutex_unlock; | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 112 |  | 
| Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 113 | mbedtls_mutex_init( &mbedtls_threading_readdir_mutex ); | 
| Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 114 | mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex ); | 
| Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
|  | 117 | /* | 
|  | 118 | * Free global mutexes | 
|  | 119 | */ | 
|  | 120 | void mbedtls_threading_free_alt( void ) | 
|  | 121 | { | 
|  | 122 | mbedtls_mutex_free( &mbedtls_threading_readdir_mutex ); | 
| Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 123 | mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex ); | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 124 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | #endif /* MBEDTLS_THREADING_ALT */ | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 126 |  | 
| Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 127 | /* | 
|  | 128 | * Define global mutexes | 
|  | 129 | */ | 
|  | 130 | #ifndef MUTEX_INIT | 
|  | 131 | #define MUTEX_INIT | 
|  | 132 | #endif | 
|  | 133 | mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT; | 
| Manuel Pégourié-Gonnard | 864108d | 2015-05-29 10:11:03 +0200 | [diff] [blame] | 134 | mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT; | 
| Manuel Pégourié-Gonnard | 944cfe8 | 2015-05-27 20:07:18 +0200 | [diff] [blame] | 135 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | #endif /* MBEDTLS_THREADING_C */ |