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