mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers threading.c Source File

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 )
00044         return;
00045 
00046     (void) pthread_mutex_destroy( &mutex->mutex );
00047 }
00048 
00049 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
00050 {
00051     if( mutex == NULL || ! mutex->is_valid )
00052         return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
00053 
00054     if( pthread_mutex_lock( &mutex->mutex ) != 0 )
00055         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
00056 
00057     return( 0 );
00058 }
00059 
00060 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
00061 {
00062     if( mutex == NULL || ! mutex->is_valid )
00063         return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
00064 
00065     if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
00066         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
00067 
00068     return( 0 );
00069 }
00070 
00071 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
00072 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
00073 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
00074 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
00075 
00076 /*
00077  * With phtreads we can statically initialize mutexes
00078  */
00079 #define MUTEX_INIT  = { PTHREAD_MUTEX_INITIALIZER, 1 }
00080 
00081 #endif /* MBEDTLS_THREADING_PTHREAD */
00082 
00083 #if defined(MBEDTLS_THREADING_ALT)
00084 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
00085 {
00086     ((void) mutex );
00087     return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
00088 }
00089 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
00090 {
00091     ((void) mutex );
00092     return;
00093 }
00094 
00095 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
00096 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
00097 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
00098 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
00099 
00100 /*
00101  * Set functions pointers and initialize global mutexes
00102  */
00103 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
00104                        void (*mutex_free)( mbedtls_threading_mutex_t * ),
00105                        int (*mutex_lock)( mbedtls_threading_mutex_t * ),
00106                        int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
00107 {
00108     mbedtls_mutex_init = mutex_init;
00109     mbedtls_mutex_free = mutex_free;
00110     mbedtls_mutex_lock = mutex_lock;
00111     mbedtls_mutex_unlock = mutex_unlock;
00112 
00113     mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
00114     mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
00115 }
00116 
00117 /*
00118  * Free global mutexes
00119  */
00120 void mbedtls_threading_free_alt( void )
00121 {
00122     mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
00123     mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
00124 }
00125 #endif /* MBEDTLS_THREADING_ALT */
00126 
00127 /*
00128  * Define global mutexes
00129  */
00130 #ifndef MUTEX_INIT
00131 #define MUTEX_INIT
00132 #endif
00133 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
00134 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
00135 
00136 #endif /* MBEDTLS_THREADING_C */