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