Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
threading.c
00001 /* 00002 * Threading abstraction layer 00003 * 00004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 00005 * SPDX-License-Identifier: Apache-2.0 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00008 * not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00015 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 * 00019 * This file is part of mbed TLS (https://tls.mbed.org) 00020 */ 00021 00022 #if !defined(MBEDTLS_CONFIG_FILE) 00023 #include "mbedtls/config.h" 00024 #else 00025 #include MBEDTLS_CONFIG_FILE 00026 #endif 00027 00028 #if defined(MBEDTLS_THREADING_C) 00029 00030 #include "mbedtls/threading.h" 00031 00032 #if defined(MBEDTLS_THREADING_PTHREAD) 00033 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex ) 00034 { 00035 if( mutex == NULL ) 00036 return; 00037 00038 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0; 00039 } 00040 00041 static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex ) 00042 { 00043 if( mutex == NULL || !mutex->is_valid ) 00044 return; 00045 00046 (void) pthread_mutex_destroy( &mutex->mutex ); 00047 mutex->is_valid = 0; 00048 } 00049 00050 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex ) 00051 { 00052 if( mutex == NULL || ! mutex->is_valid ) 00053 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); 00054 00055 if( pthread_mutex_lock( &mutex->mutex ) != 0 ) 00056 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); 00057 00058 return( 0 ); 00059 } 00060 00061 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex ) 00062 { 00063 if( mutex == NULL || ! mutex->is_valid ) 00064 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); 00065 00066 if( pthread_mutex_unlock( &mutex->mutex ) != 0 ) 00067 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); 00068 00069 return( 0 ); 00070 } 00071 00072 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread; 00073 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread; 00074 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread; 00075 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread; 00076 00077 /* 00078 * With phtreads we can statically initialize mutexes 00079 */ 00080 #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 } 00081 00082 #endif /* MBEDTLS_THREADING_PTHREAD */ 00083 00084 #if defined(MBEDTLS_THREADING_ALT) 00085 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex ) 00086 { 00087 ((void) mutex ); 00088 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); 00089 } 00090 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex ) 00091 { 00092 ((void) mutex ); 00093 return; 00094 } 00095 00096 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; 00097 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; 00098 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; 00099 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; 00100 00101 /* 00102 * Set functions pointers and initialize global mutexes 00103 */ 00104 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ), 00105 void (*mutex_free)( mbedtls_threading_mutex_t * ), 00106 int (*mutex_lock)( mbedtls_threading_mutex_t * ), 00107 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) ) 00108 { 00109 mbedtls_mutex_init = mutex_init; 00110 mbedtls_mutex_free = mutex_free; 00111 mbedtls_mutex_lock = mutex_lock; 00112 mbedtls_mutex_unlock = mutex_unlock; 00113 00114 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex ); 00115 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex ); 00116 } 00117 00118 /* 00119 * Free global mutexes 00120 */ 00121 void mbedtls_threading_free_alt( void ) 00122 { 00123 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex ); 00124 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex ); 00125 } 00126 #endif /* MBEDTLS_THREADING_ALT */ 00127 00128 /* 00129 * Define global mutexes 00130 */ 00131 #ifndef MUTEX_INIT 00132 #define MUTEX_INIT 00133 #endif 00134 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT; 00135 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT; 00136 00137 #endif /* MBEDTLS_THREADING_C */
Generated on Tue Jul 12 2022 12:22:26 by
