blob: 744fe27fa9b9e8b1d5760d2c587de24937193253 [file] [log] [blame]
Paul Bakker2466d932013-09-28 14:40:38 +02001/*
2 * Threading abstraction layer
3 *
4 * Copyright (C) 2006-2013, Brainspark B.V.
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
26#include "polarssl/config.h"
27
28#if defined(POLARSSL_THREADING_C)
29
30#include "polarssl/threading.h"
31
Paul Bakker2466d932013-09-28 14:40:38 +020032#if defined(POLARSSL_THREADING_PTHREAD)
33static int threading_mutex_init_pthread( threading_mutex_t *mutex )
34{
35 if( mutex == NULL )
36 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
37
38 if( pthread_mutex_init( mutex, NULL ) != 0 )
39 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
40
41 return( 0 );
42}
43
44static int threading_mutex_free_pthread( threading_mutex_t *mutex )
45{
46 if( mutex == NULL )
47 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
48
49 if( pthread_mutex_destroy( mutex ) != 0 )
50 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
51
52 return( 0 );
53}
54
55static int threading_mutex_lock_pthread( threading_mutex_t *mutex )
56{
57 if( mutex == NULL )
58 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
59
60 if( pthread_mutex_lock( mutex ) != 0 )
61 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
62
63 return( 0 );
64}
65
66static int threading_mutex_unlock_pthread( threading_mutex_t *mutex )
67{
68 if( mutex == NULL )
69 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
70
71 if( pthread_mutex_unlock( mutex ) != 0 )
72 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
73
74 return( 0 );
75}
76
77int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_pthread;
78int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_pthread;
79int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_pthread;
80int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_pthread;
81#endif /* POLARSSL_THREADING_PTHREAD */
82
83#if defined(POLARSSL_THREADING_ALT)
Paul Bakkerc78c8422013-12-31 11:55:27 +010084static int threading_mutex_fail( threading_mutex_t *mutex )
85{
86 ((void) mutex );
87 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
88}
89
90int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_fail;
91int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_fail;
92int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_fail;
93int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +020094
Paul Bakkerb7c13122013-10-11 10:51:32 +020095int threading_set_alt( int (*mutex_init)( threading_mutex_t * ),
Paul Bakker2466d932013-09-28 14:40:38 +020096 int (*mutex_free)( threading_mutex_t * ),
97 int (*mutex_lock)( threading_mutex_t * ),
98 int (*mutex_unlock)( threading_mutex_t * ) )
99{
100 polarssl_mutex_init = mutex_init;
101 polarssl_mutex_free = mutex_free;
102 polarssl_mutex_lock = mutex_lock;
103 polarssl_mutex_unlock = mutex_unlock;
104
105 return( 0 );
106}
107#endif /* POLARSSL_THREADING_ALT_C */
108
109#endif /* POLARSSL_THREADING_C */