1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2013 - 2021, 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 ***************************************************************************/
23 #include "curl_setup.h"
25 #if defined(USE_MBEDTLS) && \
26 ((defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)) || \
27 (defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)))
29 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
31 # define MBEDTLS_MUTEX_T pthread_mutex_t
32 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
34 # define MBEDTLS_MUTEX_T HANDLE
37 #include "mbedtls_threadlock.h"
38 #include "curl_printf.h"
39 #include "curl_memory.h"
40 /* The last #include file should be: */
43 /* number of thread locks */
46 /* This array will store all of the mutexes available to Mbedtls. */
47 static MBEDTLS_MUTEX_T *mutex_buf = NULL;
49 int Curl_mbedtlsthreadlock_thread_setup(void)
53 mutex_buf = calloc(NUMT * sizeof(MBEDTLS_MUTEX_T), 1);
55 return 0; /* error, no number of threads defined */
57 for(i = 0; i < NUMT; i++) {
58 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
59 if(pthread_mutex_init(&mutex_buf[i], NULL))
60 return 0; /* pthread_mutex_init failed */
61 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
62 mutex_buf[i] = CreateMutex(0, FALSE, 0);
64 return 0; /* CreateMutex failed */
65 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
71 int Curl_mbedtlsthreadlock_thread_cleanup(void)
76 return 0; /* error, no threads locks defined */
78 for(i = 0; i < NUMT; i++) {
79 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
80 if(pthread_mutex_destroy(&mutex_buf[i]))
81 return 0; /* pthread_mutex_destroy failed */
82 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
83 if(!CloseHandle(mutex_buf[i]))
84 return 0; /* CloseHandle failed */
85 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
93 int Curl_mbedtlsthreadlock_lock_function(int n)
96 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
97 if(pthread_mutex_lock(&mutex_buf[n])) {
98 DEBUGF(fprintf(stderr,
99 "Error: mbedtlsthreadlock_lock_function failed\n"));
100 return 0; /* pthread_mutex_lock failed */
102 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
103 if(WaitForSingleObject(mutex_buf[n], INFINITE) == WAIT_FAILED) {
104 DEBUGF(fprintf(stderr,
105 "Error: mbedtlsthreadlock_lock_function failed\n"));
106 return 0; /* pthread_mutex_lock failed */
108 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
113 int Curl_mbedtlsthreadlock_unlock_function(int n)
116 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
117 if(pthread_mutex_unlock(&mutex_buf[n])) {
118 DEBUGF(fprintf(stderr,
119 "Error: mbedtlsthreadlock_unlock_function failed\n"));
120 return 0; /* pthread_mutex_unlock failed */
122 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
123 if(!ReleaseMutex(mutex_buf[n])) {
124 DEBUGF(fprintf(stderr,
125 "Error: mbedtlsthreadlock_unlock_function failed\n"));
126 return 0; /* pthread_mutex_lock failed */
128 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
133 #endif /* USE_MBEDTLS */