1 // SPDX-License-Identifier: Apache-2.0 2 /* 3 * Threading abstraction layer 4 * 5 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 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. 18 * 19 * This file is part of mbed TLS (https://tls.mbed.org) 20 */ 21 22 #if !defined(MBEDTLS_CONFIG_FILE) 23 #include "mbedtls/config.h" 24 #else 25 #include MBEDTLS_CONFIG_FILE 26 #endif 27 28 #if defined(MBEDTLS_THREADING_C) 29 30 #include "mbedtls/threading.h" 31 32 #if defined(MBEDTLS_THREADING_PTHREAD) 33 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex ) 34 { 35 if( mutex == NULL ) 36 return; 37 38 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0; 39 } 40 41 static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex ) 42 { 43 if( mutex == NULL || !mutex->is_valid ) 44 return; 45 46 (void) pthread_mutex_destroy( &mutex->mutex ); 47 mutex->is_valid = 0; 48 } 49 50 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex ) 51 { 52 if( mutex == NULL || ! mutex->is_valid ) 53 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); 54 55 if( pthread_mutex_lock( &mutex->mutex ) != 0 ) 56 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); 57 58 return( 0 ); 59 } 60 61 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex ) 62 { 63 if( mutex == NULL || ! mutex->is_valid ) 64 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); 65 66 if( pthread_mutex_unlock( &mutex->mutex ) != 0 ) 67 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); 68 69 return( 0 ); 70 } 71 72 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread; 73 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread; 74 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread; 75 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread; 76 77 /* 78 * With phtreads we can statically initialize mutexes 79 */ 80 #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 } 81 82 #endif /* MBEDTLS_THREADING_PTHREAD */ 83 84 #if defined(MBEDTLS_THREADING_ALT) 85 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex ) 86 { 87 ((void) mutex ); 88 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); 89 } 90 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex ) 91 { 92 ((void) mutex ); 93 return; 94 } 95 96 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; 97 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; 98 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; 99 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; 100 101 /* 102 * Set functions pointers and initialize global mutexes 103 */ 104 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ), 105 void (*mutex_free)( mbedtls_threading_mutex_t * ), 106 int (*mutex_lock)( mbedtls_threading_mutex_t * ), 107 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) ) 108 { 109 mbedtls_mutex_init = mutex_init; 110 mbedtls_mutex_free = mutex_free; 111 mbedtls_mutex_lock = mutex_lock; 112 mbedtls_mutex_unlock = mutex_unlock; 113 114 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex ); 115 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex ); 116 } 117 118 /* 119 * Free global mutexes 120 */ 121 void mbedtls_threading_free_alt( void ) 122 { 123 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex ); 124 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex ); 125 } 126 #endif /* MBEDTLS_THREADING_ALT */ 127 128 /* 129 * Define global mutexes 130 */ 131 #ifndef MUTEX_INIT 132 #define MUTEX_INIT 133 #endif 134 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT; 135 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT; 136 137 #endif /* MBEDTLS_THREADING_C */ 138