]> git.zerfleddert.de Git - proxmark3-svn/blob - common/mbedtls/threading.c
Fix compilation error on OS X (#730)
[proxmark3-svn] / common / mbedtls / threading.c
1 /*
2 * Threading abstraction layer
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23
24 #if !defined(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
26 #else
27 #include MBEDTLS_CONFIG_FILE
28 #endif
29
30 #if defined(MBEDTLS_THREADING_C)
31
32 #include "mbedtls/threading.h"
33
34 #if defined(MBEDTLS_THREADING_PTHREAD)
35 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
36 {
37 if( mutex == NULL )
38 return;
39
40 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
41 }
42
43 static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
44 {
45 if( mutex == NULL || !mutex->is_valid )
46 return;
47
48 (void) pthread_mutex_destroy( &mutex->mutex );
49 mutex->is_valid = 0;
50 }
51
52 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
53 {
54 if( mutex == NULL || ! mutex->is_valid )
55 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
56
57 if( pthread_mutex_lock( &mutex->mutex ) != 0 )
58 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
59
60 return( 0 );
61 }
62
63 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
64 {
65 if( mutex == NULL || ! mutex->is_valid )
66 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
67
68 if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
69 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
70
71 return( 0 );
72 }
73
74 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
75 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
76 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
77 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
78
79 /*
80 * With phtreads we can statically initialize mutexes
81 */
82 #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
83
84 #endif /* MBEDTLS_THREADING_PTHREAD */
85
86 #if defined(MBEDTLS_THREADING_ALT)
87 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
88 {
89 ((void) mutex );
90 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
91 }
92 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
93 {
94 ((void) mutex );
95 return;
96 }
97
98 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
99 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
100 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
101 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
102
103 /*
104 * Set functions pointers and initialize global mutexes
105 */
106 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
107 void (*mutex_free)( mbedtls_threading_mutex_t * ),
108 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
109 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
110 {
111 mbedtls_mutex_init = mutex_init;
112 mbedtls_mutex_free = mutex_free;
113 mbedtls_mutex_lock = mutex_lock;
114 mbedtls_mutex_unlock = mutex_unlock;
115
116 #if defined(MBEDTLS_FS_IO)
117 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
118 #endif
119 }
120
121 /*
122 * Free global mutexes
123 */
124 void mbedtls_threading_free_alt( void )
125 {
126 #if defined(MBEDTLS_FS_IO)
127 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
128 #endif
129 }
130 #endif /* MBEDTLS_THREADING_ALT */
131
132 /*
133 * Define global mutexes
134 */
135 #ifndef MUTEX_INIT
136 #define MUTEX_INIT
137 #endif
138 #if defined(MBEDTLS_FS_IO)
139 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
140 #endif
141
142 #endif /* MBEDTLS_THREADING_C */
Impressum, Datenschutz