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