mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 17 23:23:45 2019 +0000
Revision:
0:5b88d5760320
mbed-os5 only for TYBLE16

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:5b88d5760320 1 /*
kenjiArai 0:5b88d5760320 2 * Threading abstraction layer
kenjiArai 0:5b88d5760320 3 *
kenjiArai 0:5b88d5760320 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
kenjiArai 0:5b88d5760320 5 * SPDX-License-Identifier: Apache-2.0
kenjiArai 0:5b88d5760320 6 *
kenjiArai 0:5b88d5760320 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
kenjiArai 0:5b88d5760320 8 * not use this file except in compliance with the License.
kenjiArai 0:5b88d5760320 9 * You may obtain a copy of the License at
kenjiArai 0:5b88d5760320 10 *
kenjiArai 0:5b88d5760320 11 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 0:5b88d5760320 12 *
kenjiArai 0:5b88d5760320 13 * Unless required by applicable law or agreed to in writing, software
kenjiArai 0:5b88d5760320 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
kenjiArai 0:5b88d5760320 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 0:5b88d5760320 16 * See the License for the specific language governing permissions and
kenjiArai 0:5b88d5760320 17 * limitations under the License.
kenjiArai 0:5b88d5760320 18 *
kenjiArai 0:5b88d5760320 19 * This file is part of mbed TLS (https://tls.mbed.org)
kenjiArai 0:5b88d5760320 20 */
kenjiArai 0:5b88d5760320 21
kenjiArai 0:5b88d5760320 22 /*
kenjiArai 0:5b88d5760320 23 * Ensure gmtime_r is available even with -std=c99; must be defined before
kenjiArai 0:5b88d5760320 24 * config.h, which pulls in glibc's features.h. Harmless on other platforms.
kenjiArai 0:5b88d5760320 25 */
kenjiArai 0:5b88d5760320 26 #if !defined(_POSIX_C_SOURCE)
kenjiArai 0:5b88d5760320 27 #define _POSIX_C_SOURCE 200112L
kenjiArai 0:5b88d5760320 28 #endif
kenjiArai 0:5b88d5760320 29
kenjiArai 0:5b88d5760320 30 #if !defined(MBEDTLS_CONFIG_FILE)
kenjiArai 0:5b88d5760320 31 #include "mbedtls/config.h"
kenjiArai 0:5b88d5760320 32 #else
kenjiArai 0:5b88d5760320 33 #include MBEDTLS_CONFIG_FILE
kenjiArai 0:5b88d5760320 34 #endif
kenjiArai 0:5b88d5760320 35
kenjiArai 0:5b88d5760320 36 #if defined(MBEDTLS_THREADING_C)
kenjiArai 0:5b88d5760320 37
kenjiArai 0:5b88d5760320 38 #include "mbedtls/threading.h"
kenjiArai 0:5b88d5760320 39
kenjiArai 0:5b88d5760320 40 #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
kenjiArai 0:5b88d5760320 41
kenjiArai 0:5b88d5760320 42 #if !defined(_WIN32) && (defined(unix) || \
kenjiArai 0:5b88d5760320 43 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
kenjiArai 0:5b88d5760320 44 defined(__MACH__)))
kenjiArai 0:5b88d5760320 45 #include <unistd.h>
kenjiArai 0:5b88d5760320 46 #endif /* !_WIN32 && (unix || __unix || __unix__ ||
kenjiArai 0:5b88d5760320 47 * (__APPLE__ && __MACH__)) */
kenjiArai 0:5b88d5760320 48
kenjiArai 0:5b88d5760320 49 #if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
kenjiArai 0:5b88d5760320 50 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
kenjiArai 0:5b88d5760320 51 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) )
kenjiArai 0:5b88d5760320 52 /*
kenjiArai 0:5b88d5760320 53 * This is a convenience shorthand macro to avoid checking the long
kenjiArai 0:5b88d5760320 54 * preprocessor conditions above. Ideally, we could expose this macro in
kenjiArai 0:5b88d5760320 55 * platform_util.h and simply use it in platform_util.c, threading.c and
kenjiArai 0:5b88d5760320 56 * threading.h. However, this macro is not part of the Mbed TLS public API, so
kenjiArai 0:5b88d5760320 57 * we keep it private by only defining it in this file
kenjiArai 0:5b88d5760320 58 */
kenjiArai 0:5b88d5760320 59
kenjiArai 0:5b88d5760320 60 #if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
kenjiArai 0:5b88d5760320 61 #define THREADING_USE_GMTIME
kenjiArai 0:5b88d5760320 62 #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
kenjiArai 0:5b88d5760320 63
kenjiArai 0:5b88d5760320 64 #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
kenjiArai 0:5b88d5760320 65 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
kenjiArai 0:5b88d5760320 66 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */
kenjiArai 0:5b88d5760320 67
kenjiArai 0:5b88d5760320 68 #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
kenjiArai 0:5b88d5760320 69
kenjiArai 0:5b88d5760320 70 #if defined(MBEDTLS_THREADING_PTHREAD)
kenjiArai 0:5b88d5760320 71 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
kenjiArai 0:5b88d5760320 72 {
kenjiArai 0:5b88d5760320 73 if( mutex == NULL )
kenjiArai 0:5b88d5760320 74 return;
kenjiArai 0:5b88d5760320 75
kenjiArai 0:5b88d5760320 76 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
kenjiArai 0:5b88d5760320 77 }
kenjiArai 0:5b88d5760320 78
kenjiArai 0:5b88d5760320 79 static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
kenjiArai 0:5b88d5760320 80 {
kenjiArai 0:5b88d5760320 81 if( mutex == NULL || !mutex->is_valid )
kenjiArai 0:5b88d5760320 82 return;
kenjiArai 0:5b88d5760320 83
kenjiArai 0:5b88d5760320 84 (void) pthread_mutex_destroy( &mutex->mutex );
kenjiArai 0:5b88d5760320 85 mutex->is_valid = 0;
kenjiArai 0:5b88d5760320 86 }
kenjiArai 0:5b88d5760320 87
kenjiArai 0:5b88d5760320 88 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
kenjiArai 0:5b88d5760320 89 {
kenjiArai 0:5b88d5760320 90 if( mutex == NULL || ! mutex->is_valid )
kenjiArai 0:5b88d5760320 91 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 92
kenjiArai 0:5b88d5760320 93 if( pthread_mutex_lock( &mutex->mutex ) != 0 )
kenjiArai 0:5b88d5760320 94 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
kenjiArai 0:5b88d5760320 95
kenjiArai 0:5b88d5760320 96 return( 0 );
kenjiArai 0:5b88d5760320 97 }
kenjiArai 0:5b88d5760320 98
kenjiArai 0:5b88d5760320 99 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
kenjiArai 0:5b88d5760320 100 {
kenjiArai 0:5b88d5760320 101 if( mutex == NULL || ! mutex->is_valid )
kenjiArai 0:5b88d5760320 102 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 103
kenjiArai 0:5b88d5760320 104 if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
kenjiArai 0:5b88d5760320 105 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
kenjiArai 0:5b88d5760320 106
kenjiArai 0:5b88d5760320 107 return( 0 );
kenjiArai 0:5b88d5760320 108 }
kenjiArai 0:5b88d5760320 109
kenjiArai 0:5b88d5760320 110 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
kenjiArai 0:5b88d5760320 111 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
kenjiArai 0:5b88d5760320 112 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
kenjiArai 0:5b88d5760320 113 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
kenjiArai 0:5b88d5760320 114
kenjiArai 0:5b88d5760320 115 /*
kenjiArai 0:5b88d5760320 116 * With phtreads we can statically initialize mutexes
kenjiArai 0:5b88d5760320 117 */
kenjiArai 0:5b88d5760320 118 #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
kenjiArai 0:5b88d5760320 119
kenjiArai 0:5b88d5760320 120 #endif /* MBEDTLS_THREADING_PTHREAD */
kenjiArai 0:5b88d5760320 121
kenjiArai 0:5b88d5760320 122 #if defined(MBEDTLS_THREADING_ALT)
kenjiArai 0:5b88d5760320 123 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
kenjiArai 0:5b88d5760320 124 {
kenjiArai 0:5b88d5760320 125 ((void) mutex );
kenjiArai 0:5b88d5760320 126 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
kenjiArai 0:5b88d5760320 127 }
kenjiArai 0:5b88d5760320 128 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
kenjiArai 0:5b88d5760320 129 {
kenjiArai 0:5b88d5760320 130 ((void) mutex );
kenjiArai 0:5b88d5760320 131 return;
kenjiArai 0:5b88d5760320 132 }
kenjiArai 0:5b88d5760320 133
kenjiArai 0:5b88d5760320 134 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
kenjiArai 0:5b88d5760320 135 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
kenjiArai 0:5b88d5760320 136 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
kenjiArai 0:5b88d5760320 137 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
kenjiArai 0:5b88d5760320 138
kenjiArai 0:5b88d5760320 139 /*
kenjiArai 0:5b88d5760320 140 * Set functions pointers and initialize global mutexes
kenjiArai 0:5b88d5760320 141 */
kenjiArai 0:5b88d5760320 142 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
kenjiArai 0:5b88d5760320 143 void (*mutex_free)( mbedtls_threading_mutex_t * ),
kenjiArai 0:5b88d5760320 144 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
kenjiArai 0:5b88d5760320 145 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
kenjiArai 0:5b88d5760320 146 {
kenjiArai 0:5b88d5760320 147 mbedtls_mutex_init = mutex_init;
kenjiArai 0:5b88d5760320 148 mbedtls_mutex_free = mutex_free;
kenjiArai 0:5b88d5760320 149 mbedtls_mutex_lock = mutex_lock;
kenjiArai 0:5b88d5760320 150 mbedtls_mutex_unlock = mutex_unlock;
kenjiArai 0:5b88d5760320 151
kenjiArai 0:5b88d5760320 152 #if defined(MBEDTLS_FS_IO)
kenjiArai 0:5b88d5760320 153 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
kenjiArai 0:5b88d5760320 154 #endif
kenjiArai 0:5b88d5760320 155 #if defined(THREADING_USE_GMTIME)
kenjiArai 0:5b88d5760320 156 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
kenjiArai 0:5b88d5760320 157 #endif
kenjiArai 0:5b88d5760320 158 }
kenjiArai 0:5b88d5760320 159
kenjiArai 0:5b88d5760320 160 /*
kenjiArai 0:5b88d5760320 161 * Free global mutexes
kenjiArai 0:5b88d5760320 162 */
kenjiArai 0:5b88d5760320 163 void mbedtls_threading_free_alt( void )
kenjiArai 0:5b88d5760320 164 {
kenjiArai 0:5b88d5760320 165 #if defined(MBEDTLS_FS_IO)
kenjiArai 0:5b88d5760320 166 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
kenjiArai 0:5b88d5760320 167 #endif
kenjiArai 0:5b88d5760320 168 #if defined(THREADING_USE_GMTIME)
kenjiArai 0:5b88d5760320 169 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
kenjiArai 0:5b88d5760320 170 #endif
kenjiArai 0:5b88d5760320 171 }
kenjiArai 0:5b88d5760320 172 #endif /* MBEDTLS_THREADING_ALT */
kenjiArai 0:5b88d5760320 173
kenjiArai 0:5b88d5760320 174 /*
kenjiArai 0:5b88d5760320 175 * Define global mutexes
kenjiArai 0:5b88d5760320 176 */
kenjiArai 0:5b88d5760320 177 #ifndef MUTEX_INIT
kenjiArai 0:5b88d5760320 178 #define MUTEX_INIT
kenjiArai 0:5b88d5760320 179 #endif
kenjiArai 0:5b88d5760320 180 #if defined(MBEDTLS_FS_IO)
kenjiArai 0:5b88d5760320 181 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
kenjiArai 0:5b88d5760320 182 #endif
kenjiArai 0:5b88d5760320 183 #if defined(THREADING_USE_GMTIME)
kenjiArai 0:5b88d5760320 184 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
kenjiArai 0:5b88d5760320 185 #endif
kenjiArai 0:5b88d5760320 186
kenjiArai 0:5b88d5760320 187 #endif /* MBEDTLS_THREADING_C */