blob: 4abcd721c9ed8cc7c63a81ad54c28380ff9213d0 [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker2466d932013-09-28 14:40:38 +02007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
Paul Bakker2466d932013-09-28 14:40:38 +02009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker2466d932013-09-28 14:40:38 +020025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakker2466d932013-09-28 14:40:38 +020029
30#if defined(POLARSSL_THREADING_C)
31
32#include "polarssl/threading.h"
33
Paul Bakker2466d932013-09-28 14:40:38 +020034#if defined(POLARSSL_THREADING_PTHREAD)
35static int threading_mutex_init_pthread( threading_mutex_t *mutex )
36{
37 if( mutex == NULL )
38 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
39
40 if( pthread_mutex_init( mutex, NULL ) != 0 )
41 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
42
43 return( 0 );
44}
45
46static int threading_mutex_free_pthread( threading_mutex_t *mutex )
47{
48 if( mutex == NULL )
49 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
50
51 if( pthread_mutex_destroy( mutex ) != 0 )
52 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
53
54 return( 0 );
55}
56
57static int threading_mutex_lock_pthread( threading_mutex_t *mutex )
58{
59 if( mutex == NULL )
60 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
61
62 if( pthread_mutex_lock( mutex ) != 0 )
63 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
64
65 return( 0 );
66}
67
68static int threading_mutex_unlock_pthread( threading_mutex_t *mutex )
69{
70 if( mutex == NULL )
71 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
72
73 if( pthread_mutex_unlock( mutex ) != 0 )
74 return( POLARSSL_ERR_THREADING_MUTEX_ERROR );
75
76 return( 0 );
77}
78
79int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_init_pthread;
80int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_free_pthread;
81int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_lock_pthread;
82int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_unlock_pthread;
83#endif /* POLARSSL_THREADING_PTHREAD */
84
85#if defined(POLARSSL_THREADING_ALT)
Paul Bakkerc78c8422013-12-31 11:55:27 +010086static int threading_mutex_fail( threading_mutex_t *mutex )
87{
88 ((void) mutex );
89 return( POLARSSL_ERR_THREADING_BAD_INPUT_DATA );
90}
91
92int (*polarssl_mutex_init)( threading_mutex_t * ) = threading_mutex_fail;
93int (*polarssl_mutex_free)( threading_mutex_t * ) = threading_mutex_fail;
94int (*polarssl_mutex_lock)( threading_mutex_t * ) = threading_mutex_fail;
95int (*polarssl_mutex_unlock)( threading_mutex_t * ) = threading_mutex_fail;
Paul Bakker2466d932013-09-28 14:40:38 +020096
Paul Bakkerb7c13122013-10-11 10:51:32 +020097int threading_set_alt( int (*mutex_init)( threading_mutex_t * ),
Paul Bakker2466d932013-09-28 14:40:38 +020098 int (*mutex_free)( threading_mutex_t * ),
99 int (*mutex_lock)( threading_mutex_t * ),
100 int (*mutex_unlock)( threading_mutex_t * ) )
101{
102 polarssl_mutex_init = mutex_init;
103 polarssl_mutex_free = mutex_free;
104 polarssl_mutex_lock = mutex_lock;
105 polarssl_mutex_unlock = mutex_unlock;
106
107 return( 0 );
108}
109#endif /* POLARSSL_THREADING_ALT_C */
110
111#endif /* POLARSSL_THREADING_C */