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