1 /*
2 * Threading abstraction layer
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
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.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22 /*
23 * Ensure gmtime_r is available even with -std=c99; must be defined before
24 * config.h, which pulls in glibc's features.h. Harmless on other platforms.
25 */
26 #if !defined(_POSIX_C_SOURCE)
27 #define _POSIX_C_SOURCE 200112L
28 #endif
29
30 #if !defined(MBEDTLS_CONFIG_FILE)
31 #include "mbedtls/config.h"
32 #else
33 #include MBEDTLS_CONFIG_FILE
34 #endif
35
36 #if defined(MBEDTLS_THREADING_C)
37
38 #include "mbedtls/threading.h"
39
40 #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
41
42 #if !defined(_WIN32) && (defined(unix) || \
43 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
44 defined(__MACH__)))
45 #include <unistd.h>
46 #endif /* !_WIN32 && (unix || __unix || __unix__ ||
47 * (__APPLE__ && __MACH__)) */
48
49 #if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
50 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
51 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) )
52 /*
53 * This is a convenience shorthand macro to avoid checking the long
54 * preprocessor conditions above. Ideally, we could expose this macro in
55 * platform_util.h and simply use it in platform_util.c, threading.c and
56 * threading.h. However, this macro is not part of the Mbed TLS public API, so
57 * we keep it private by only defining it in this file
58 */
59
60 #if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
61 #define THREADING_USE_GMTIME
62 #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
63
64 #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \
65 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \
66 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */
67
68 #endif /* MBEDTLS_HAVE_TIME_DATE && !MBEDTLS_PLATFORM_GMTIME_R_ALT */
69
70 #if defined(MBEDTLS_THREADING_PTHREAD)
threading_mutex_init_pthread(mbedtls_threading_mutex_t * mutex)71 static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex )
72 {
73 if( mutex == NULL )
74 return;
75
76 mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0;
77 }
78
threading_mutex_free_pthread(mbedtls_threading_mutex_t * mutex)79 static 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
threading_mutex_lock_pthread(mbedtls_threading_mutex_t * mutex)88 static 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
threading_mutex_unlock_pthread(mbedtls_threading_mutex_t * mutex)99 static 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
110 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread;
111 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread;
112 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread;
113 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread;
114
115 /*
116 * With phtreads we can statically initialize mutexes
117 */
118 #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 }
119
120 #endif /* MBEDTLS_THREADING_PTHREAD */
121
122 #if defined(MBEDTLS_THREADING_ALT)
123 /* Modify for AliOS Things begin. 2019-01-09 */
124 #if 0
125 static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex )
126 {
127 ((void) mutex );
128 return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
129 }
130 static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex )
131 {
132 ((void) mutex );
133 return;
134 }
135
136 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
137 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy;
138 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
139 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail;
140 #else
141 void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init;
142 void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free;
143 int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock;
144 int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock;
145 #endif
146 /* Modify for AliOS Things end. 2019-01-09 */
147
148 /*
149 * Set functions pointers and initialize global mutexes
150 */
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 *))151 void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ),
152 void (*mutex_free)( mbedtls_threading_mutex_t * ),
153 int (*mutex_lock)( mbedtls_threading_mutex_t * ),
154 int (*mutex_unlock)( mbedtls_threading_mutex_t * ) )
155 {
156 mbedtls_mutex_init = mutex_init;
157 mbedtls_mutex_free = mutex_free;
158 mbedtls_mutex_lock = mutex_lock;
159 mbedtls_mutex_unlock = mutex_unlock;
160
161 #if defined(MBEDTLS_FS_IO)
162 mbedtls_mutex_init( &mbedtls_threading_readdir_mutex );
163 #endif
164 #if defined(THREADING_USE_GMTIME)
165 mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex );
166 #endif
167 }
168
169 /*
170 * Free global mutexes
171 */
mbedtls_threading_free_alt(void)172 void mbedtls_threading_free_alt( void )
173 {
174 #if defined(MBEDTLS_FS_IO)
175 mbedtls_mutex_free( &mbedtls_threading_readdir_mutex );
176 #endif
177 #if defined(THREADING_USE_GMTIME)
178 mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex );
179 #endif
180 }
181 #endif /* MBEDTLS_THREADING_ALT */
182
183 /*
184 * Define global mutexes
185 */
186 #ifndef MUTEX_INIT
187 #define MUTEX_INIT
188 #endif
189 #if defined(MBEDTLS_FS_IO)
190 mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT;
191 #endif
192 #if defined(THREADING_USE_GMTIME)
193 mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
194 #endif
195
196 #endif /* MBEDTLS_THREADING_C */
197