xref: /reactos/dll/3rdparty/mbedtls/threading.c (revision d7fd62d4)
1 /*
2  *  Threading abstraction layer
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  *
7  *  This file is provided under the Apache License 2.0, or the
8  *  GNU General Public License v2.0 or later.
9  *
10  *  **********
11  *  Apache License 2.0:
12  *
13  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14  *  not use this file except in compliance with the License.
15  *  You may obtain a copy of the License at
16  *
17  *  http://www.apache.org/licenses/LICENSE-2.0
18  *
19  *  Unless required by applicable law or agreed to in writing, software
20  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the License for the specific language governing permissions and
23  *  limitations under the License.
24  *
25  *  **********
26  *
27  *  **********
28  *  GNU General Public License v2.0 or later:
29  *
30  *  This program is free software; you can redistribute it and/or modify
31  *  it under the terms of the GNU General Public License as published by
32  *  the Free Software Foundation; either version 2 of the License, or
33  *  (at your option) any later version.
34  *
35  *  This program is distributed in the hope that it will be useful,
36  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
37  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  *  GNU General Public License for more details.
39  *
40  *  You should have received a copy of the GNU General Public License along
41  *  with this program; if not, write to the Free Software Foundation, Inc.,
42  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43  *
44  *  **********
45  *
46  *  This file is part of mbed TLS (https://tls.mbed.org)
47  */
48 
49 #if !defined(MBEDTLS_CONFIG_FILE)
50 #include "mbedtls/config.h"
51 #else
52 #include MBEDTLS_CONFIG_FILE
53 #endif
54 
55 #if defined(MBEDTLS_THREADING_C)
56 
57 #include "mbedtls/threading.h"
58 
59 #if defined(MBEDTLS_THREADING_PTHREAD)
60 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
61 {
62     if( mutex == NULL )
63         return;
64 
65     mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
66 }
67 
68 static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
69 {
70     if( mutex == NULL || !mutex->is_valid )
71         return;
72 
73     (void) pthread_mutex_destroy( &mutex->mutex );
74     mutex->is_valid = 0;
75 }
76 
77 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
78 {
79     if( mutex == NULL || ! mutex->is_valid )
80         return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
81 
82     if( pthread_mutex_lock( &mutex->mutex ) != 0 )
83         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
84 
85     return( 0 );
86 }
87 
88 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
89 {
90     if( mutex == NULL || ! mutex->is_valid )
91         return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
92 
93     if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
94         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
95 
96     return( 0 );
97 }
98 
99 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
100 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
101 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
102 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
103 
104 /*
105  * With phtreads we can statically initialize mutexes
106  */
107 #define MUTEX_INIT  = { PTHREAD_MUTEX_INITIALIZER, 1 }
108 
109 #endif /* MBEDTLS_THREADING_PTHREAD */
110 
111 #if defined(MBEDTLS_THREADING_ALT)
112 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
113 {
114     ((void) mutex );
115     return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
116 }
117 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
118 {
119     ((void) mutex );
120     return;
121 }
122 
123 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
124 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
125 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
126 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
127 
128 /*
129  * Set functions pointers and initialize global mutexes
130  */
131 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
132                        void (*mutex_free)( mbedtls_threading_mutex_t * ),
133                        int (*mutex_lock)( mbedtls_threading_mutex_t * ),
134                        int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
135 {
136     mbedtls_mutex_init = mutex_init;
137     mbedtls_mutex_free = mutex_free;
138     mbedtls_mutex_lock = mutex_lock;
139     mbedtls_mutex_unlock = mutex_unlock;
140 
141 #if defined(MBEDTLS_FS_IO)
142     mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
143 #endif
144 #if defined(MBEDTLS_HAVE_TIME_DATE)
145     mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
146 #endif
147 }
148 
149 /*
150  * Free global mutexes
151  */
152 void mbedtls_threading_free_alt( void )
153 {
154 #if defined(MBEDTLS_FS_IO)
155     mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
156 #endif
157 #if defined(MBEDTLS_HAVE_TIME_DATE)
158     mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
159 #endif
160 }
161 #endif /* MBEDTLS_THREADING_ALT */
162 
163 /*
164  * Define global mutexes
165  */
166 #ifndef MUTEX_INIT
167 #define MUTEX_INIT
168 #endif
169 #if defined(MBEDTLS_FS_IO)
170 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
171 #endif
172 #if defined(MBEDTLS_HAVE_TIME_DATE)
173 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
174 #endif
175 
176 #endif /* MBEDTLS_THREADING_C */
177