Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * Threading abstraction layer
nexpaq 0:6c56fb4bc5f0 3 *
nexpaq 0:6c56fb4bc5f0 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
nexpaq 0:6c56fb4bc5f0 5 * SPDX-License-Identifier: Apache-2.0
nexpaq 0:6c56fb4bc5f0 6 *
nexpaq 0:6c56fb4bc5f0 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
nexpaq 0:6c56fb4bc5f0 8 * not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 9 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 10 *
nexpaq 0:6c56fb4bc5f0 11 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 12 *
nexpaq 0:6c56fb4bc5f0 13 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
nexpaq 0:6c56fb4bc5f0 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 16 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 17 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 18 *
nexpaq 0:6c56fb4bc5f0 19 * This file is part of mbed TLS (https://tls.mbed.org)
nexpaq 0:6c56fb4bc5f0 20 */
nexpaq 0:6c56fb4bc5f0 21
nexpaq 0:6c56fb4bc5f0 22 #if !defined(MBEDTLS_CONFIG_FILE)
nexpaq 0:6c56fb4bc5f0 23 #include "mbedtls/config.h"
nexpaq 0:6c56fb4bc5f0 24 #else
nexpaq 0:6c56fb4bc5f0 25 #include MBEDTLS_CONFIG_FILE
nexpaq 0:6c56fb4bc5f0 26 #endif
nexpaq 0:6c56fb4bc5f0 27
nexpaq 0:6c56fb4bc5f0 28 #if defined(MBEDTLS_THREADING_C)
nexpaq 0:6c56fb4bc5f0 29
nexpaq 0:6c56fb4bc5f0 30 #include "mbedtls/threading.h"
nexpaq 0:6c56fb4bc5f0 31
nexpaq 0:6c56fb4bc5f0 32 #if defined(MBEDTLS_THREADING_PTHREAD)
nexpaq 0:6c56fb4bc5f0 33 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
nexpaq 0:6c56fb4bc5f0 34 {
nexpaq 0:6c56fb4bc5f0 35 if( mutex == NULL )
nexpaq 0:6c56fb4bc5f0 36 return;
nexpaq 0:6c56fb4bc5f0 37
nexpaq 0:6c56fb4bc5f0 38 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
nexpaq 0:6c56fb4bc5f0 39 }
nexpaq 0:6c56fb4bc5f0 40
nexpaq 0:6c56fb4bc5f0 41 static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
nexpaq 0:6c56fb4bc5f0 42 {
nexpaq 0:6c56fb4bc5f0 43 if( mutex == NULL )
nexpaq 0:6c56fb4bc5f0 44 return;
nexpaq 0:6c56fb4bc5f0 45
nexpaq 0:6c56fb4bc5f0 46 (void) pthread_mutex_destroy( &mutex->mutex );
nexpaq 0:6c56fb4bc5f0 47 }
nexpaq 0:6c56fb4bc5f0 48
nexpaq 0:6c56fb4bc5f0 49 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
nexpaq 0:6c56fb4bc5f0 50 {
nexpaq 0:6c56fb4bc5f0 51 if( mutex == NULL || ! mutex->is_valid )
nexpaq 0:6c56fb4bc5f0 52 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 53
nexpaq 0:6c56fb4bc5f0 54 if( pthread_mutex_lock( &mutex->mutex ) != 0 )
nexpaq 0:6c56fb4bc5f0 55 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
nexpaq 0:6c56fb4bc5f0 56
nexpaq 0:6c56fb4bc5f0 57 return( 0 );
nexpaq 0:6c56fb4bc5f0 58 }
nexpaq 0:6c56fb4bc5f0 59
nexpaq 0:6c56fb4bc5f0 60 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
nexpaq 0:6c56fb4bc5f0 61 {
nexpaq 0:6c56fb4bc5f0 62 if( mutex == NULL || ! mutex->is_valid )
nexpaq 0:6c56fb4bc5f0 63 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 64
nexpaq 0:6c56fb4bc5f0 65 if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
nexpaq 0:6c56fb4bc5f0 66 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
nexpaq 0:6c56fb4bc5f0 67
nexpaq 0:6c56fb4bc5f0 68 return( 0 );
nexpaq 0:6c56fb4bc5f0 69 }
nexpaq 0:6c56fb4bc5f0 70
nexpaq 0:6c56fb4bc5f0 71 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
nexpaq 0:6c56fb4bc5f0 72 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
nexpaq 0:6c56fb4bc5f0 73 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
nexpaq 0:6c56fb4bc5f0 74 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
nexpaq 0:6c56fb4bc5f0 75
nexpaq 0:6c56fb4bc5f0 76 /*
nexpaq 0:6c56fb4bc5f0 77 * With phtreads we can statically initialize mutexes
nexpaq 0:6c56fb4bc5f0 78 */
nexpaq 0:6c56fb4bc5f0 79 #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
nexpaq 0:6c56fb4bc5f0 80
nexpaq 0:6c56fb4bc5f0 81 #endif /* MBEDTLS_THREADING_PTHREAD */
nexpaq 0:6c56fb4bc5f0 82
nexpaq 0:6c56fb4bc5f0 83 #if defined(MBEDTLS_THREADING_ALT)
nexpaq 0:6c56fb4bc5f0 84 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
nexpaq 0:6c56fb4bc5f0 85 {
nexpaq 0:6c56fb4bc5f0 86 ((void) mutex );
nexpaq 0:6c56fb4bc5f0 87 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 88 }
nexpaq 0:6c56fb4bc5f0 89 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
nexpaq 0:6c56fb4bc5f0 90 {
nexpaq 0:6c56fb4bc5f0 91 ((void) mutex );
nexpaq 0:6c56fb4bc5f0 92 return;
nexpaq 0:6c56fb4bc5f0 93 }
nexpaq 0:6c56fb4bc5f0 94
nexpaq 0:6c56fb4bc5f0 95 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
nexpaq 0:6c56fb4bc5f0 96 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
nexpaq 0:6c56fb4bc5f0 97 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
nexpaq 0:6c56fb4bc5f0 98 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
nexpaq 0:6c56fb4bc5f0 99
nexpaq 0:6c56fb4bc5f0 100 /*
nexpaq 0:6c56fb4bc5f0 101 * Set functions pointers and initialize global mutexes
nexpaq 0:6c56fb4bc5f0 102 */
nexpaq 0:6c56fb4bc5f0 103 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
nexpaq 0:6c56fb4bc5f0 104 void (*mutex_free)( mbedtls_threading_mutex_t * ),
nexpaq 0:6c56fb4bc5f0 105 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
nexpaq 0:6c56fb4bc5f0 106 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
nexpaq 0:6c56fb4bc5f0 107 {
nexpaq 0:6c56fb4bc5f0 108 mbedtls_mutex_init = mutex_init;
nexpaq 0:6c56fb4bc5f0 109 mbedtls_mutex_free = mutex_free;
nexpaq 0:6c56fb4bc5f0 110 mbedtls_mutex_lock = mutex_lock;
nexpaq 0:6c56fb4bc5f0 111 mbedtls_mutex_unlock = mutex_unlock;
nexpaq 0:6c56fb4bc5f0 112
nexpaq 0:6c56fb4bc5f0 113 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
nexpaq 0:6c56fb4bc5f0 114 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
nexpaq 0:6c56fb4bc5f0 115 }
nexpaq 0:6c56fb4bc5f0 116
nexpaq 0:6c56fb4bc5f0 117 /*
nexpaq 0:6c56fb4bc5f0 118 * Free global mutexes
nexpaq 0:6c56fb4bc5f0 119 */
nexpaq 0:6c56fb4bc5f0 120 void mbedtls_threading_free_alt( void )
nexpaq 0:6c56fb4bc5f0 121 {
nexpaq 0:6c56fb4bc5f0 122 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
nexpaq 0:6c56fb4bc5f0 123 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
nexpaq 0:6c56fb4bc5f0 124 }
nexpaq 0:6c56fb4bc5f0 125 #endif /* MBEDTLS_THREADING_ALT */
nexpaq 0:6c56fb4bc5f0 126
nexpaq 0:6c56fb4bc5f0 127 /*
nexpaq 0:6c56fb4bc5f0 128 * Define global mutexes
nexpaq 0:6c56fb4bc5f0 129 */
nexpaq 0:6c56fb4bc5f0 130 #ifndef MUTEX_INIT
nexpaq 0:6c56fb4bc5f0 131 #define MUTEX_INIT
nexpaq 0:6c56fb4bc5f0 132 #endif
nexpaq 0:6c56fb4bc5f0 133 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
nexpaq 0:6c56fb4bc5f0 134 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
nexpaq 0:6c56fb4bc5f0 135
nexpaq 0:6c56fb4bc5f0 136 #endif /* MBEDTLS_THREADING_C */