2013-09-28 12:40:38 +00:00
|
|
|
/*
|
|
|
|
* Threading abstraction layer
|
|
|
|
*
|
2015-01-23 09:45:19 +00:00
|
|
|
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
2013-09-28 12:40:38 +00:00
|
|
|
*
|
2015-03-06 13:17:10 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2013-09-28 12:40:38 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-04-29 10:39:06 +00:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2013-09-28 12:40:38 +00:00
|
|
|
|
|
|
|
#if defined(POLARSSL_THREADING_C)
|
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/threading.h"
|
2013-09-28 12:40:38 +00:00
|
|
|
|
|
|
|
#if defined(POLARSSL_THREADING_PTHREAD)
|
|
|
|
static int threading_mutex_init_pthread( threading_mutex_t *mutex )
|
|
|
|
{
|
|
|
|
if( mutex == NULL )
|
|
|
|
return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
if( pthread_mutex_init( mutex, NULL ) != 0 )
|
|
|
|
return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int threading_mutex_free_pthread( threading_mutex_t *mutex )
|
|
|
|
{
|
|
|
|
if( mutex == NULL )
|
|
|
|
return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
if( pthread_mutex_destroy( mutex ) != 0 )
|
|
|
|
return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int threading_mutex_lock_pthread( threading_mutex_t *mutex )
|
|
|
|
{
|
|
|
|
if( mutex == NULL )
|
|
|
|
return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
if( pthread_mutex_lock( mutex ) != 0 )
|
|
|
|
return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int threading_mutex_unlock_pthread( threading_mutex_t *mutex )
|
|
|
|
{
|
|
|
|
if( mutex == NULL )
|
|
|
|
return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
if( pthread_mutex_unlock( mutex ) != 0 )
|
|
|
|
return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_pthread;
|
|
|
|
int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_pthread;
|
|
|
|
int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_pthread;
|
|
|
|
int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_pthread;
|
|
|
|
#endif /* POLARSSL_THREADING_PTHREAD */
|
|
|
|
|
|
|
|
#if defined(POLARSSL_THREADING_ALT)
|
2013-12-31 10:55:27 +00:00
|
|
|
static int threading_mutex_fail( threading_mutex_t *mutex )
|
|
|
|
{
|
|
|
|
((void) mutex );
|
|
|
|
return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_fail;
|
|
|
|
int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_fail;
|
|
|
|
int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_fail;
|
|
|
|
int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_fail;
|
2013-09-28 12:40:38 +00:00
|
|
|
|
2013-10-11 08:51:32 +00:00
|
|
|
int threading_set_alt( int (*mutex_init)( threading_mutex_t * ),
|
2013-09-28 12:40:38 +00:00
|
|
|
int (*mutex_free)( threading_mutex_t * ),
|
|
|
|
int (*mutex_lock)( threading_mutex_t * ),
|
|
|
|
int (*mutex_unlock)( threading_mutex_t * ) )
|
|
|
|
{
|
|
|
|
polarssl_mutex_init = mutex_init;
|
|
|
|
polarssl_mutex_free = mutex_free;
|
|
|
|
polarssl_mutex_lock = mutex_lock;
|
|
|
|
polarssl_mutex_unlock = mutex_unlock;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2015-03-06 16:52:46 +00:00
|
|
|
#endif /* POLARSSL_THREADING_ALT */
|
2013-09-28 12:40:38 +00:00
|
|
|
|
|
|
|
#endif /* POLARSSL_THREADING_C */
|