1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2013 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
9 * Copyright (C) 2010, 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
11 * This software is licensed as described in the file COPYING, which
12 * you should have received as part of this distribution. The terms
13 * are also available at https://curl.se/docs/copyright.html.
15 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 * copies of the Software, and permit persons to whom the Software is
17 * furnished to do so, under the terms of the COPYING file.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 * SPDX-License-Identifier: curl
24 ***************************************************************************/
25 #include "curl_setup.h"
27 #if defined(USE_MBEDTLS) && \
28 ((defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)) || \
29 (defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)))
31 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
33 # define MBEDTLS_MUTEX_T pthread_mutex_t
34 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
36 # define MBEDTLS_MUTEX_T HANDLE
39 #include "mbedtls_threadlock.h"
40 #include "curl_printf.h"
41 #include "curl_memory.h"
42 /* The last #include file should be: */
45 /* number of thread locks */
48 /* This array will store all of the mutexes available to Mbedtls. */
49 static MBEDTLS_MUTEX_T *mutex_buf = NULL;
51 int Curl_mbedtlsthreadlock_thread_setup(void)
55 mutex_buf = calloc(NUMT * sizeof(MBEDTLS_MUTEX_T), 1);
57 return 0; /* error, no number of threads defined */
59 for(i = 0; i < NUMT; i++) {
60 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
61 if(pthread_mutex_init(&mutex_buf[i], NULL))
62 return 0; /* pthread_mutex_init failed */
63 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
64 mutex_buf[i] = CreateMutex(0, FALSE, 0);
66 return 0; /* CreateMutex failed */
67 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
73 int Curl_mbedtlsthreadlock_thread_cleanup(void)
78 return 0; /* error, no threads locks defined */
80 for(i = 0; i < NUMT; i++) {
81 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
82 if(pthread_mutex_destroy(&mutex_buf[i]))
83 return 0; /* pthread_mutex_destroy failed */
84 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
85 if(!CloseHandle(mutex_buf[i]))
86 return 0; /* CloseHandle failed */
87 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
95 int Curl_mbedtlsthreadlock_lock_function(int n)
98 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
99 if(pthread_mutex_lock(&mutex_buf[n])) {
100 DEBUGF(fprintf(stderr,
101 "Error: mbedtlsthreadlock_lock_function failed\n"));
102 return 0; /* pthread_mutex_lock failed */
104 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
105 if(WaitForSingleObject(mutex_buf[n], INFINITE) == WAIT_FAILED) {
106 DEBUGF(fprintf(stderr,
107 "Error: mbedtlsthreadlock_lock_function failed\n"));
108 return 0; /* pthread_mutex_lock failed */
110 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
115 int Curl_mbedtlsthreadlock_unlock_function(int n)
118 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
119 if(pthread_mutex_unlock(&mutex_buf[n])) {
120 DEBUGF(fprintf(stderr,
121 "Error: mbedtlsthreadlock_unlock_function failed\n"));
122 return 0; /* pthread_mutex_unlock failed */
124 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
125 if(!ReleaseMutex(mutex_buf[n])) {
126 DEBUGF(fprintf(stderr,
127 "Error: mbedtlsthreadlock_unlock_function failed\n"));
128 return 0; /* pthread_mutex_lock failed */
130 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
135 #endif /* USE_MBEDTLS */