Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Threading abstraction layer |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 7 | * |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 28 | |
| 29 | #if defined(POLARSSL_THREADING_C) |
| 30 | |
| 31 | #include "polarssl/threading.h" |
| 32 | |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 33 | #if defined(POLARSSL_THREADING_PTHREAD) |
| 34 | static int threading_mutex_init_pthread( threading_mutex_t *mutex ) |
| 35 | { |
| 36 | if( mutex == NULL ) |
| 37 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); |
| 38 | |
| 39 | if( pthread_mutex_init( mutex, NULL ) != 0 ) |
| 40 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 41 | |
| 42 | return( 0 ); |
| 43 | } |
| 44 | |
| 45 | static int threading_mutex_free_pthread( threading_mutex_t *mutex ) |
| 46 | { |
| 47 | if( mutex == NULL ) |
| 48 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); |
| 49 | |
| 50 | if( pthread_mutex_destroy( mutex ) != 0 ) |
| 51 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 52 | |
| 53 | return( 0 ); |
| 54 | } |
| 55 | |
| 56 | static int threading_mutex_lock_pthread( threading_mutex_t *mutex ) |
| 57 | { |
| 58 | if( mutex == NULL ) |
| 59 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); |
| 60 | |
| 61 | if( pthread_mutex_lock( mutex ) != 0 ) |
| 62 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 63 | |
| 64 | return( 0 ); |
| 65 | } |
| 66 | |
| 67 | static int threading_mutex_unlock_pthread( threading_mutex_t *mutex ) |
| 68 | { |
| 69 | if( mutex == NULL ) |
| 70 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); |
| 71 | |
| 72 | if( pthread_mutex_unlock( mutex ) != 0 ) |
| 73 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 74 | |
| 75 | return( 0 ); |
| 76 | } |
| 77 | |
| 78 | int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_pthread; |
| 79 | int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_pthread; |
| 80 | int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_pthread; |
| 81 | int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_pthread; |
| 82 | #endif /* POLARSSL_THREADING_PTHREAD */ |
| 83 | |
| 84 | #if defined(POLARSSL_THREADING_ALT) |
Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 85 | static int threading_mutex_fail( threading_mutex_t *mutex ) |
| 86 | { |
| 87 | ((void) mutex ); |
| 88 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); |
| 89 | } |
| 90 | |
| 91 | int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_fail; |
| 92 | int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_fail; |
| 93 | int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_fail; |
| 94 | int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_fail; |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 95 | |
Paul Bakker | b7c1312 | 2013-10-11 10:51:32 +0200 | [diff] [blame] | 96 | int threading_set_alt( int (*mutex_init)( threading_mutex_t * ), |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 97 | int (*mutex_free)( threading_mutex_t * ), |
| 98 | int (*mutex_lock)( threading_mutex_t * ), |
| 99 | int (*mutex_unlock)( threading_mutex_t * ) ) |
| 100 | { |
| 101 | polarssl_mutex_init = mutex_init; |
| 102 | polarssl_mutex_free = mutex_free; |
| 103 | polarssl_mutex_lock = mutex_lock; |
| 104 | polarssl_mutex_unlock = mutex_unlock; |
| 105 | |
| 106 | return( 0 ); |
| 107 | } |
Manuel Pégourié-Gonnard | 9b66990 | 2015-03-06 16:52:46 +0000 | [diff] [blame] | 108 | #endif /* POLARSSL_THREADING_ALT */ |
Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 109 | |
| 110 | #endif /* POLARSSL_THREADING_C */ |