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