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 | |
| 32 | #if defined(POLARSSL_THREADING_DUMMY) |
| 33 | static int threading_mutex_init_dummy( threading_mutex_t *mutex ) |
| 34 | { |
| 35 | ((void) mutex ); |
| 36 | return( 0 ); |
| 37 | } |
| 38 | |
| 39 | static int threading_mutex_free_dummy( threading_mutex_t *mutex ) |
| 40 | { |
| 41 | ((void) mutex ); |
| 42 | return( 0 ); |
| 43 | } |
| 44 | |
| 45 | static int threading_mutex_lock_dummy( threading_mutex_t *mutex ) |
| 46 | { |
| 47 | ((void) mutex ); |
| 48 | return( 0 ); |
| 49 | } |
| 50 | |
| 51 | static int threading_mutex_unlock_dummy( threading_mutex_t *mutex ) |
| 52 | { |
| 53 | ((void) mutex ); |
| 54 | return( 0 ); |
| 55 | } |
| 56 | |
| 57 | int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_dummy; |
| 58 | int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_dummy; |
| 59 | int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_dummy; |
| 60 | int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_dummy; |
| 61 | #endif /* POLARSSL_THREADING_DUMMY */ |
| 62 | |
| 63 | #if defined(POLARSSL_THREADING_PTHREAD) |
| 64 | static int threading_mutex_init_pthread( threading_mutex_t *mutex ) |
| 65 | { |
| 66 | if( mutex == NULL ) |
| 67 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); |
| 68 | |
| 69 | if( pthread_mutex_init( mutex, NULL ) != 0 ) |
| 70 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 71 | |
| 72 | return( 0 ); |
| 73 | } |
| 74 | |
| 75 | static int threading_mutex_free_pthread( threading_mutex_t *mutex ) |
| 76 | { |
| 77 | if( mutex == NULL ) |
| 78 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); |
| 79 | |
| 80 | if( pthread_mutex_destroy( mutex ) != 0 ) |
| 81 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 82 | |
| 83 | return( 0 ); |
| 84 | } |
| 85 | |
| 86 | static int threading_mutex_lock_pthread( threading_mutex_t *mutex ) |
| 87 | { |
| 88 | if( mutex == NULL ) |
| 89 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); |
| 90 | |
| 91 | if( pthread_mutex_lock( mutex ) != 0 ) |
| 92 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 93 | |
| 94 | return( 0 ); |
| 95 | } |
| 96 | |
| 97 | static int threading_mutex_unlock_pthread( threading_mutex_t *mutex ) |
| 98 | { |
| 99 | if( mutex == NULL ) |
| 100 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); |
| 101 | |
| 102 | if( pthread_mutex_unlock( mutex ) != 0 ) |
| 103 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); |
| 104 | |
| 105 | return( 0 ); |
| 106 | } |
| 107 | |
| 108 | int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_pthread; |
| 109 | int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_pthread; |
| 110 | int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_pthread; |
| 111 | int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_pthread; |
| 112 | #endif /* POLARSSL_THREADING_PTHREAD */ |
| 113 | |
| 114 | #if defined(POLARSSL_THREADING_ALT) |
| 115 | int (*polarssl_mutex_init)( threading_mutex_t * ) = NULL; |
| 116 | int (*polarssl_mutex_free)( threading_mutex_t * ) = NULL; |
| 117 | int (*polarssl_mutex_lock)( threading_mutex_t * ) = NULL; |
| 118 | int (*polarssl_mutex_unlock)( threading_mutex_t * ) = NULL; |
| 119 | |
| 120 | int threading_set_own( int (*mutex_init)( threading_mutex_t * ), |
| 121 | int (*mutex_free)( threading_mutex_t * ), |
| 122 | int (*mutex_lock)( threading_mutex_t * ), |
| 123 | int (*mutex_unlock)( threading_mutex_t * ) ) |
| 124 | { |
| 125 | polarssl_mutex_init = mutex_init; |
| 126 | polarssl_mutex_free = mutex_free; |
| 127 | polarssl_mutex_lock = mutex_lock; |
| 128 | polarssl_mutex_unlock = mutex_unlock; |
| 129 | |
| 130 | return( 0 ); |
| 131 | } |
| 132 | #endif /* POLARSSL_THREADING_ALT_C */ |
| 133 | |
| 134 | #endif /* POLARSSL_THREADING_C */ |