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