| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Threading abstraction layer | 
|  | 3 | * | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 4 | *  Copyright (C) 2006-2014, Brainspark B.V. | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 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 |  | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #if !defined(POLARSSL_CONFIG_FILE) | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 27 | #include "polarssl/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else | 
|  | 29 | #include POLARSSL_CONFIG_FILE | 
|  | 30 | #endif | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 31 |  | 
|  | 32 | #if defined(POLARSSL_THREADING_C) | 
|  | 33 |  | 
|  | 34 | #include "polarssl/threading.h" | 
|  | 35 |  | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 36 | #if defined(POLARSSL_THREADING_PTHREAD) | 
|  | 37 | static int threading_mutex_init_pthread( threading_mutex_t *mutex ) | 
|  | 38 | { | 
|  | 39 | if( mutex == NULL ) | 
|  | 40 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); | 
|  | 41 |  | 
|  | 42 | if( pthread_mutex_init( mutex, NULL ) != 0 ) | 
|  | 43 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); | 
|  | 44 |  | 
|  | 45 | return( 0 ); | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | static int threading_mutex_free_pthread( threading_mutex_t *mutex ) | 
|  | 49 | { | 
|  | 50 | if( mutex == NULL ) | 
|  | 51 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); | 
|  | 52 |  | 
|  | 53 | if( pthread_mutex_destroy( mutex ) != 0 ) | 
|  | 54 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); | 
|  | 55 |  | 
|  | 56 | return( 0 ); | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | static int threading_mutex_lock_pthread( threading_mutex_t *mutex ) | 
|  | 60 | { | 
|  | 61 | if( mutex == NULL ) | 
|  | 62 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); | 
|  | 63 |  | 
|  | 64 | if( pthread_mutex_lock( mutex ) != 0 ) | 
|  | 65 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); | 
|  | 66 |  | 
|  | 67 | return( 0 ); | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | static int threading_mutex_unlock_pthread( threading_mutex_t *mutex ) | 
|  | 71 | { | 
|  | 72 | if( mutex == NULL ) | 
|  | 73 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); | 
|  | 74 |  | 
|  | 75 | if( pthread_mutex_unlock( mutex ) != 0 ) | 
|  | 76 | return( POLARSSL_ERR_THREADING_MUTEX_ERROR ); | 
|  | 77 |  | 
|  | 78 | return( 0 ); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_pthread; | 
|  | 82 | int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_pthread; | 
|  | 83 | int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_pthread; | 
|  | 84 | int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_pthread; | 
|  | 85 | #endif /* POLARSSL_THREADING_PTHREAD */ | 
|  | 86 |  | 
|  | 87 | #if defined(POLARSSL_THREADING_ALT) | 
| Paul Bakker | c78c842 | 2013-12-31 11:55:27 +0100 | [diff] [blame] | 88 | static int threading_mutex_fail( threading_mutex_t *mutex ) | 
|  | 89 | { | 
|  | 90 | ((void) mutex ); | 
|  | 91 | return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA ); | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_fail; | 
|  | 95 | int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_fail; | 
|  | 96 | int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_fail; | 
|  | 97 | int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_fail; | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 98 |  | 
| Paul Bakker | b7c1312 | 2013-10-11 10:51:32 +0200 | [diff] [blame] | 99 | int threading_set_alt( int (*mutex_init)( threading_mutex_t * ), | 
| Paul Bakker | 2466d93 | 2013-09-28 14:40:38 +0200 | [diff] [blame] | 100 | int (*mutex_free)( threading_mutex_t * ), | 
|  | 101 | int (*mutex_lock)( threading_mutex_t * ), | 
|  | 102 | int (*mutex_unlock)( threading_mutex_t * ) ) | 
|  | 103 | { | 
|  | 104 | polarssl_mutex_init = mutex_init; | 
|  | 105 | polarssl_mutex_free = mutex_free; | 
|  | 106 | polarssl_mutex_lock = mutex_lock; | 
|  | 107 | polarssl_mutex_unlock = mutex_unlock; | 
|  | 108 |  | 
|  | 109 | return( 0 ); | 
|  | 110 | } | 
|  | 111 | #endif /* POLARSSL_THREADING_ALT_C */ | 
|  | 112 |  | 
|  | 113 | #endif /* POLARSSL_THREADING_C */ |