blob: 5e0aaa4f21a88c88beb5bde1a48760c448c52cee [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * Threading abstraction layer
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02006 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Jens Wiklander817466c2018-05-22 13:49:31 +020018 */
19
Jens Wiklander3d3b0592019-03-20 15:30:29 +010020/*
21 * Ensure gmtime_r is available even with -std=c99; must be defined before
22 * config.h, which pulls in glibc's features.h. Harmless on other platforms.
23 */
24#if !defined(_POSIX_C_SOURCE)
25#define _POSIX_C_SOURCE 200112L
26#endif
27
Jerome Forissier79013242021-07-28 10:24:04 +020028#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020029
30#if defined(MBEDTLS_THREADING_C)
31
32#include "mbedtls/threading.h"
33
Jens Wiklander3d3b0592019-03-20 15:30:29 +010034#if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
35
36#if !defined(_WIN32) && (defined(unix) || \
37 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
38 defined(__MACH__)))
39#include <unistd.h>
40#endif /* !_WIN32 && (unix || __unix || __unix__ ||
41 * (__APPLE__ && __MACH__)) */
42
43#if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
44 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
Jerome Forissier79013242021-07-28 10:24:04 +020045 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) )
Jens Wiklander3d3b0592019-03-20 15:30:29 +010046/*
47 * This is a convenience shorthand macro to avoid checking the long
48 * preprocessor conditions above. Ideally, we could expose this macro in
49 * platform_util.h and simply use it in platform_util.c, threading.c and
50 * threading.h. However, this macro is not part of the Mbed TLS public API, so
51 * we keep it private by only defining it in this file
52 */
53
54#if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
55#define THREADING_USE_GMTIME
56#endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
57
58#endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
59 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
Jerome Forissier79013242021-07-28 10:24:04 +020060 _POSIX_THREAD_SAFE_FUNCTIONS >= 200112L ) ) */
Jens Wiklander3d3b0592019-03-20 15:30:29 +010061
62#endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
63
Jens Wiklander817466c2018-05-22 13:49:31 +020064#if defined(MBEDTLS_THREADING_PTHREAD)
65static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
66{
67 if( mutex == NULL )
68 return;
69
Jerome Forissier79013242021-07-28 10:24:04 +020070 /* A nonzero value of is_valid indicates a successfully initialized
71 * mutex. This is a workaround for not being able to return an error
72 * code for this function. The lock/unlock functions return an error
73 * if is_valid is nonzero. The Mbed TLS unit test code uses this field
74 * to distinguish more states of the mutex; see
75 * tests/src/threading_helpers for details. */
Jens Wiklander817466c2018-05-22 13:49:31 +020076 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
77}
78
79static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex )
80{
81 if( mutex == NULL || !mutex->is_valid )
82 return;
83
84 (void) pthread_mutex_destroy( &mutex->mutex );
85 mutex->is_valid = 0;
86}
87
88static int threading_mutex_lock_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_lock( &mutex->mutex ) != 0 )
94 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
95
96 return( 0 );
97}
98
99static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex )
100{
101 if( mutex == NULL || ! mutex->is_valid )
102 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
103
104 if( pthread_mutex_unlock( &mutex->mutex ) != 0 )
105 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
106
107 return( 0 );
108}
109
110void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
111void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
112int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
113int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
114
115/*
Jerome Forissier039e02d2022-08-09 17:10:15 +0200116 * With pthreads we can statically initialize mutexes
Jens Wiklander817466c2018-05-22 13:49:31 +0200117 */
118#define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
119
120#endif /* MBEDTLS_THREADING_PTHREAD */
121
122#if defined(MBEDTLS_THREADING_ALT)
123static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
124{
125 ((void) mutex );
126 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
127}
128static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
129{
130 ((void) mutex );
131 return;
132}
133
134void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
135void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
136int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
137int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
138
139/*
140 * Set functions pointers and initialize global mutexes
141 */
142void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
143 void (*mutex_free)( mbedtls_threading_mutex_t * ),
144 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
145 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
146{
147 mbedtls_mutex_init = mutex_init;
148 mbedtls_mutex_free = mutex_free;
149 mbedtls_mutex_lock = mutex_lock;
150 mbedtls_mutex_unlock = mutex_unlock;
151
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100152#if defined(MBEDTLS_FS_IO)
Jens Wiklander817466c2018-05-22 13:49:31 +0200153 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100154#endif
155#if defined(THREADING_USE_GMTIME)
Jens Wiklander817466c2018-05-22 13:49:31 +0200156 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100157#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200158}
159
160/*
161 * Free global mutexes
162 */
163void mbedtls_threading_free_alt( void )
164{
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100165#if defined(MBEDTLS_FS_IO)
Jens Wiklander817466c2018-05-22 13:49:31 +0200166 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100167#endif
168#if defined(THREADING_USE_GMTIME)
Jens Wiklander817466c2018-05-22 13:49:31 +0200169 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100170#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200171}
172#endif /* MBEDTLS_THREADING_ALT */
173
174/*
175 * Define global mutexes
176 */
177#ifndef MUTEX_INIT
178#define MUTEX_INIT
179#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100180#if defined(MBEDTLS_FS_IO)
Jens Wiklander817466c2018-05-22 13:49:31 +0200181mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100182#endif
183#if defined(THREADING_USE_GMTIME)
Jens Wiklander817466c2018-05-22 13:49:31 +0200184mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100185#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200186
187#endif /* MBEDTLS_THREADING_C */