2 * Threading abstraction layer
\r
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
\r
5 * SPDX-License-Identifier: Apache-2.0
\r
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
\r
8 * not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
\r
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
19 * This file is part of mbed TLS (https://tls.mbed.org)
\r
23 * Ensure gmtime_r is available even with -std=c99; must be defined before
\r
24 * config.h, which pulls in glibc's features.h. Harmless on other platforms.
\r
26 #if !defined(_POSIX_C_SOURCE)
\r
27 #define _POSIX_C_SOURCE 200112L
\r
30 #if !defined(MBEDTLS_CONFIG_FILE)
\r
31 #include "mbedtls/config.h"
\r
33 #include MBEDTLS_CONFIG_FILE
\r
36 #if defined(MBEDTLS_THREADING_C)
\r
38 #include "mbedtls/threading.h"
\r
40 #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
\r
42 #if !defined(_WIN32) && (defined(unix) || \
\r
43 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
\r
46 #endif /* !_WIN32 && (unix || __unix || __unix__ ||
\r
47 * (__APPLE__ && __MACH__)) */
\r
49 #if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
\r
50 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
\r
51 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) )
\r
53 * This is a convenience shorthand macro to avoid checking the long
\r
54 * preprocessor conditions above. Ideally, we could expose this macro in
\r
55 * platform_util.h and simply use it in platform_util.c, threading.c and
\r
56 * threading.h. However, this macro is not part of the Mbed TLS public API, so
\r
57 * we keep it private by only defining it in this file
\r
60 #if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
\r
61 #define THREADING_USE_GMTIME
\r
62 #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
\r
64 #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
\r
65 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
\r
66 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */
\r
68 #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
\r
70 #if defined(MBEDTLS_THREADING_PTHREAD)
\r
71 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
\r
76 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
\r
79 static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
\r
81 if( mutex == NULL || !mutex->is_valid )
\r
84 (void) pthread_mutex_destroy( &mutex->mutex );
\r
85 mutex->is_valid = 0;
\r
88 static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex )
\r
90 if( mutex == NULL || ! mutex->is_valid )
\r
91 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
\r
93 if( pthread_mutex_lock( &mutex->mutex ) != 0 )
\r
94 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
\r
99 static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
\r
101 if( mutex == NULL || ! mutex->is_valid )
\r
102 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
\r
104 if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
\r
105 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
\r
110 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
\r
111 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
\r
112 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
\r
113 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
\r
116 * With phtreads we can statically initialize mutexes
\r
118 #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
\r
120 #endif /* MBEDTLS_THREADING_PTHREAD */
\r
122 #if defined(MBEDTLS_THREADING_ALT)
\r
123 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
\r
126 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
\r
128 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
\r
134 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
\r
135 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
\r
136 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
\r
137 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
\r
140 * Set functions pointers and initialize global mutexes
\r
142 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
\r
143 void (*mutex_free)( mbedtls_threading_mutex_t * ),
\r
144 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
\r
145 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
\r
147 mbedtls_mutex_init = mutex_init;
\r
148 mbedtls_mutex_free = mutex_free;
\r
149 mbedtls_mutex_lock = mutex_lock;
\r
150 mbedtls_mutex_unlock = mutex_unlock;
\r
152 #if defined(MBEDTLS_FS_IO)
\r
153 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
\r
155 #if defined(THREADING_USE_GMTIME)
\r
156 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
\r
161 * Free global mutexes
\r
163 void mbedtls_threading_free_alt( void )
\r
165 #if defined(MBEDTLS_FS_IO)
\r
166 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
\r
168 #if defined(THREADING_USE_GMTIME)
\r
169 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
\r
172 #endif /* MBEDTLS_THREADING_ALT */
\r
175 * Define global mutexes
\r
180 #if defined(MBEDTLS_FS_IO)
\r
181 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
\r
183 #if defined(THREADING_USE_GMTIME)
\r
184 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
\r
187 #endif /* MBEDTLS_THREADING_C */
\r