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