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 /* 00023 * Ensure gmtime_r is available even with -std=c99; must be defined before 00024 * config.h, which pulls in glibc's features.h. Harmless on other platforms. 00025 */ 00026 #if !defined(_POSIX_C_SOURCE) 00027 #define _POSIX_C_SOURCE 200112L 00028 #endif 00029 00030 #if !defined(MBEDTLS_CONFIG_FILE) 00031 #include "mbedtls/config.h" 00032 #else 00033 #include MBEDTLS_CONFIG_FILE 00034 #endif 00035 00036 #if defined(MBEDTLS_THREADING_C) 00037 00038 #include "mbedtls/threading.h" 00039 00040 #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT) 00041 00042 #if !defined(_WIN32) && (defined(unix) || \ 00043 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \ 00044 defined(__MACH__))) 00045 #include <unistd.h> 00046 #endif /* !_WIN32 && (unix || __unix || __unix__ || 00047 * (__APPLE__ && __MACH__)) */ 00048 00049 #if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \ 00050 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \ 00051 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) 00052 /* 00053 * This is a convenience shorthand macro to avoid checking the long 00054 * preprocessor conditions above. Ideally, we could expose this macro in 00055 * platform_util.h and simply use it in platform_util.c, threading.c and 00056 * threading.h. However, this macro is not part of the Mbed TLS public API, so 00057 * we keep it private by only defining it in this file 00058 */ 00059 00060 #if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) 00061 #define THREADING_USE_GMTIME 00062 #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */ 00063 00064 #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \ 00065 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \ 00066 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */ 00067 00068 #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */ 00069 00070 #if defined(MBEDTLS_THREADING_PTHREAD) 00071 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex ) 00072 { 00073 if( mutex == NULL ) 00074 return; 00075 00076 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0; 00077 } 00078 00079 static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex ) 00080 { 00081 if( mutex == NULL || !mutex->is_valid ) 00082 return; 00083 00084 (void) pthread_mutex_destroy( &mutex->mutex ); 00085 mutex->is_valid = 0; 00086 } 00087 00088 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex ) 00089 { 00090 if( mutex == NULL || ! mutex->is_valid ) 00091 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); 00092 00093 if( pthread_mutex_lock( &mutex->mutex ) != 0 ) 00094 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); 00095 00096 return( 0 ); 00097 } 00098 00099 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex ) 00100 { 00101 if( mutex == NULL || ! mutex->is_valid ) 00102 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); 00103 00104 if( pthread_mutex_unlock( &mutex->mutex ) != 0 ) 00105 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); 00106 00107 return( 0 ); 00108 } 00109 00110 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread; 00111 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread; 00112 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread; 00113 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread; 00114 00115 /* 00116 * With phtreads we can statically initialize mutexes 00117 */ 00118 #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 } 00119 00120 #endif /* MBEDTLS_THREADING_PTHREAD */ 00121 00122 #if defined(MBEDTLS_THREADING_ALT) 00123 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex ) 00124 { 00125 ((void) mutex ); 00126 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); 00127 } 00128 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex ) 00129 { 00130 ((void) mutex ); 00131 return; 00132 } 00133 00134 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; 00135 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; 00136 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; 00137 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; 00138 00139 /* 00140 * Set functions pointers and initialize global mutexes 00141 */ 00142 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ), 00143 void (*mutex_free)( mbedtls_threading_mutex_t * ), 00144 int (*mutex_lock)( mbedtls_threading_mutex_t * ), 00145 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) ) 00146 { 00147 mbedtls_mutex_init = mutex_init; 00148 mbedtls_mutex_free = mutex_free; 00149 mbedtls_mutex_lock = mutex_lock; 00150 mbedtls_mutex_unlock = mutex_unlock; 00151 00152 #if defined(MBEDTLS_FS_IO) 00153 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex ); 00154 #endif 00155 #if defined(THREADING_USE_GMTIME) 00156 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex ); 00157 #endif 00158 } 00159 00160 /* 00161 * Free global mutexes 00162 */ 00163 void mbedtls_threading_free_alt( void ) 00164 { 00165 #if defined(MBEDTLS_FS_IO) 00166 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex ); 00167 #endif 00168 #if defined(THREADING_USE_GMTIME) 00169 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex ); 00170 #endif 00171 } 00172 #endif /* MBEDTLS_THREADING_ALT */ 00173 00174 /* 00175 * Define global mutexes 00176 */ 00177 #ifndef MUTEX_INIT 00178 #define MUTEX_INIT 00179 #endif 00180 #if defined(MBEDTLS_FS_IO) 00181 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT; 00182 #endif 00183 #if defined(THREADING_USE_GMTIME) 00184 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT; 00185 #endif 00186 00187 #endif /* MBEDTLS_THREADING_C */
Generated on Tue Jul 12 2022 13:53:42 by
