xref: /reactos/dll/3rdparty/mbedtls/threading.c (revision cbda039f)
1 /*
2  *  Threading abstraction layer
3  *
4  *  Copyright The Mbed TLS Contributors
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 
47 /*
48  * Ensure gmtime_r is available even with -std=c99; must be defined before
49  * config.h, which pulls in glibc's features.h. Harmless on other platforms.
50  */
51 #if !defined(_POSIX_C_SOURCE)
52 #define _POSIX_C_SOURCE 200112L
53 #endif
54 
55 #if !defined(MBEDTLS_CONFIG_FILE)
56 #include "mbedtls/config.h"
57 #else
58 #include MBEDTLS_CONFIG_FILE
59 #endif
60 
61 #if defined(MBEDTLS_THREADING_C)
62 
63 #include "mbedtls/threading.h"
64 
65 #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
66 
67 #if !defined(_WIN32) && (defined(unix) || \
68     defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
69     defined(__MACH__)))
70 #include <unistd.h>
71 #endif /* !_WIN32 && (unix || __unix || __unix__ ||
72         * (__APPLE__ && __MACH__)) */
73 
74 #if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) ||     \
75        ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) &&                     \
76          _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) )
77 /*
78  * This is a convenience shorthand macro to avoid checking the long
79  * preprocessor conditions above. Ideally, we could expose this macro in
80  * platform_util.h and simply use it in platform_util.c, threading.c and
81  * threading.h. However, this macro is not part of the Mbed TLS public API, so
82  * we keep it private by only defining it in this file
83  */
84 
85 #if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
86 #define THREADING_USE_GMTIME
87 #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
88 
89 #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) ||     \
90              ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) &&                     \
91                 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
92 
93 #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
94 
95 #if defined(MBEDTLS_THREADING_PTHREAD)
threading_mutex_init_pthread(mbedtls_threading_mutex_t * mutex)96 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
97 {
98     if( mutex == NULL )
99         return;
100 
101     /* A nonzero value of is_valid indicates a successfully initialized
102      * mutex. This is a workaround for not being able to return an error
103      * code for this function. The lock/unlock functions return an error
104      * if is_valid is nonzero. The Mbed TLS unit test code uses this field
105      * to distinguish more states of the mutex; see helpers.function for
106      * details. */
107     mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
108 }
109 
threading_mutex_free_pthread(mbedtls_threading_mutex_t * mutex)110 static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
111 {
112     if( mutex == NULL || !mutex->is_valid )
113         return;
114 
115     (void) pthread_mutex_destroy( &mutex->mutex );
116     mutex->is_valid = 0;
117 }
118 
threading_mutex_lock_pthread(mbedtls_threading_mutex_t * mutex)119 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
120 {
121     if( mutex == NULL || ! mutex->is_valid )
122         return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
123 
124     if( pthread_mutex_lock( &mutex->mutex ) != 0 )
125         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
126 
127     return( 0 );
128 }
129 
threading_mutex_unlock_pthread(mbedtls_threading_mutex_t * mutex)130 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
131 {
132     if( mutex == NULL || ! mutex->is_valid )
133         return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
134 
135     if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
136         return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
137 
138     return( 0 );
139 }
140 
141 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
142 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
143 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
144 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
145 
146 /*
147  * With phtreads we can statically initialize mutexes
148  */
149 #define MUTEX_INIT  = { PTHREAD_MUTEX_INITIALIZER, 1 }
150 
151 #endif /* MBEDTLS_THREADING_PTHREAD */
152 
153 #if defined(MBEDTLS_THREADING_ALT)
threading_mutex_fail(mbedtls_threading_mutex_t * mutex)154 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
155 {
156     ((void) mutex );
157     return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
158 }
threading_mutex_dummy(mbedtls_threading_mutex_t * mutex)159 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
160 {
161     ((void) mutex );
162     return;
163 }
164 
165 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
166 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
167 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
168 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
169 
170 /*
171  * Set functions pointers and initialize global mutexes
172  */
mbedtls_threading_set_alt(void (* mutex_init)(mbedtls_threading_mutex_t *),void (* mutex_free)(mbedtls_threading_mutex_t *),int (* mutex_lock)(mbedtls_threading_mutex_t *),int (* mutex_unlock)(mbedtls_threading_mutex_t *))173 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
174                        void (*mutex_free)( mbedtls_threading_mutex_t * ),
175                        int (*mutex_lock)( mbedtls_threading_mutex_t * ),
176                        int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
177 {
178     mbedtls_mutex_init = mutex_init;
179     mbedtls_mutex_free = mutex_free;
180     mbedtls_mutex_lock = mutex_lock;
181     mbedtls_mutex_unlock = mutex_unlock;
182 
183 #if defined(MBEDTLS_FS_IO)
184     mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
185 #endif
186 #if defined(THREADING_USE_GMTIME)
187     mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
188 #endif
189 }
190 
191 /*
192  * Free global mutexes
193  */
mbedtls_threading_free_alt(void)194 void mbedtls_threading_free_alt( void )
195 {
196 #if defined(MBEDTLS_FS_IO)
197     mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
198 #endif
199 #if defined(THREADING_USE_GMTIME)
200     mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
201 #endif
202 }
203 #endif /* MBEDTLS_THREADING_ALT */
204 
205 /*
206  * Define global mutexes
207  */
208 #ifndef MUTEX_INIT
209 #define MUTEX_INIT
210 #endif
211 #if defined(MBEDTLS_FS_IO)
212 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
213 #endif
214 #if defined(THREADING_USE_GMTIME)
215 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
216 #endif
217 
218 #endif /* MBEDTLS_THREADING_C */
219