blob: 522c70fef75b0dc14f2199dd14c1d5605629db90 [file] [log] [blame]
Paul Bakker2466d932013-09-28 14:40:38 +02001/*
2 * Threading abstraction layer
3 *
Paul Bakker66d5d072014-06-17 16:39:18 +02004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker2466d932013-09-28 14:40:38 +02005 *
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é-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker2466d932013-09-28 14:40:38 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker2466d932013-09-28 14:40:38 +020031
32#if defined(POLARSSL_THREADING_C)
33
34#include "polarssl/threading.h"
35
Paul Bakker2466d932013-09-28 14:40:38 +020036#if defined(POLARSSL_THREADING_PTHREAD)
37static 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
48static 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
59static 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
70static 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
81int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_pthread;
82int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_pthread;
83int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_pthread;
84int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_pthread;
85#endif /* POLARSSL_THREADING_PTHREAD */
86
87#if defined(POLARSSL_THREADING_ALT)
Paul Bakkerc78c8422013-12-31 11:55:27 +010088static int threading_mutex_fail( threading_mutex_t *mutex )
89{
90 ((void) mutex );
91 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
92}
93
94int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_fail;
95int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_fail;
96int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_fail;
97int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +020098
Paul Bakkerb7c13122013-10-11 10:51:32 +020099int threading_set_alt( int (*mutex_init)( threading_mutex_t * ),
Paul Bakker2466d932013-09-28 14:40:38 +0200100 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 */