mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

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