blob: 5d48516ba53cfe2036e47be43fff3526f9029381 [file] [log] [blame]
Paul Bakker2466d932013-09-28 14:40:38 +02001/*
2 * Threading abstraction layer
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker2466d932013-09-28 14:40:38 +02005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker2466d932013-09-28 14:40:38 +02007 *
Paul Bakker2466d932013-09-28 14:40:38 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker2466d932013-09-28 14:40:38 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker2466d932013-09-28 14:40:38 +020028
29#if defined(POLARSSL_THREADING_C)
30
31#include "polarssl/threading.h"
32
Paul Bakker2466d932013-09-28 14:40:38 +020033#if defined(POLARSSL_THREADING_PTHREAD)
34static int threading_mutex_init_pthread( threading_mutex_t *mutex )
35{
36 if( mutex == NULL )
37 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
38
39 if( pthread_mutex_init( mutex, NULL ) != 0 )
40 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
41
42 return( 0 );
43}
44
45static int threading_mutex_free_pthread( threading_mutex_t *mutex )
46{
47 if( mutex == NULL )
48 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
49
50 if( pthread_mutex_destroy( mutex ) != 0 )
51 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
52
53 return( 0 );
54}
55
56static int threading_mutex_lock_pthread( threading_mutex_t *mutex )
57{
58 if( mutex == NULL )
59 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
60
61 if( pthread_mutex_lock( mutex ) != 0 )
62 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
63
64 return( 0 );
65}
66
67static int threading_mutex_unlock_pthread( threading_mutex_t *mutex )
68{
69 if( mutex == NULL )
70 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
71
72 if( pthread_mutex_unlock( mutex ) != 0 )
73 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
74
75 return( 0 );
76}
77
78int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_pthread;
79int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_pthread;
80int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_pthread;
81int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_pthread;
82#endif /* POLARSSL_THREADING_PTHREAD */
83
84#if defined(POLARSSL_THREADING_ALT)
Paul Bakkerc78c8422013-12-31 11:55:27 +010085static int threading_mutex_fail( threading_mutex_t *mutex )
86{
87 ((void) mutex );
88 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
89}
90
91int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_fail;
92int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_fail;
93int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_fail;
94int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +020095
Paul Bakkerb7c13122013-10-11 10:51:32 +020096int threading_set_alt( int (*mutex_init)( threading_mutex_t * ),
Paul Bakker2466d932013-09-28 14:40:38 +020097 int (*mutex_free)( threading_mutex_t * ),
98 int (*mutex_lock)( threading_mutex_t * ),
99 int (*mutex_unlock)( threading_mutex_t * ) )
100{
101 polarssl_mutex_init = mutex_init;
102 polarssl_mutex_free = mutex_free;
103 polarssl_mutex_lock = mutex_lock;
104 polarssl_mutex_unlock = mutex_unlock;
105
106 return( 0 );
107}
Manuel Pégourié-Gonnard9b669902015-03-06 16:52:46 +0000108#endif /* POLARSSL_THREADING_ALT */
Paul Bakker2466d932013-09-28 14:40:38 +0200109
110#endif /* POLARSSL_THREADING_C */