1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2013-2016, 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.haxx.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_POLARSSL) || defined(USE_MBEDTLS)) && \
26 (defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32))
28 #if defined(USE_THREADS_POSIX)
29 # ifdef HAVE_PTHREAD_H
32 #elif defined(USE_THREADS_WIN32)
33 # ifdef HAVE_PROCESS_H
38 #include "polarssl_threadlock.h"
39 #include "curl_printf.h"
40 #include "curl_memory.h"
41 /* The last #include file should be: */
44 /* number of thread locks */
47 /* This array will store all of the mutexes available to PolarSSL. */
48 static POLARSSL_MUTEX_T *mutex_buf = NULL;
50 int Curl_polarsslthreadlock_thread_setup(void)
55 mutex_buf = calloc(NUMT * sizeof(POLARSSL_MUTEX_T), 1);
57 return 0; /* error, no number of threads defined */
60 for(i = 0; i < NUMT; i++) {
61 ret = pthread_mutex_init(&mutex_buf[i], NULL);
63 return 0; /* pthread_mutex_init failed */
65 #elif defined(HAVE_PROCESS_H)
66 for(i = 0; i < NUMT; i++) {
67 mutex_buf[i] = CreateMutex(0, FALSE, 0);
69 return 0; /* CreateMutex failed */
71 #endif /* HAVE_PTHREAD_H */
76 int Curl_polarsslthreadlock_thread_cleanup(void)
82 return 0; /* error, no threads locks defined */
85 for(i = 0; i < NUMT; i++) {
86 ret = pthread_mutex_destroy(&mutex_buf[i]);
88 return 0; /* pthread_mutex_destroy failed */
90 #elif defined(HAVE_PROCESS_H)
91 for(i = 0; i < NUMT; i++) {
92 ret = CloseHandle(mutex_buf[i]);
94 return 0; /* CloseHandle failed */
96 #endif /* HAVE_PTHREAD_H */
103 int Curl_polarsslthreadlock_lock_function(int n)
106 #ifdef HAVE_PTHREAD_H
108 ret = pthread_mutex_lock(&mutex_buf[n]);
110 DEBUGF(fprintf(stderr,
111 "Error: polarsslthreadlock_lock_function failed\n"));
112 return 0; /* pthread_mutex_lock failed */
115 #elif defined(HAVE_PROCESS_H)
117 ret = (WaitForSingleObject(mutex_buf[n], INFINITE)==WAIT_FAILED?1:0);
119 DEBUGF(fprintf(stderr,
120 "Error: polarsslthreadlock_lock_function failed\n"));
121 return 0; /* pthread_mutex_lock failed */
124 #endif /* HAVE_PTHREAD_H */
128 int Curl_polarsslthreadlock_unlock_function(int n)
131 #ifdef HAVE_PTHREAD_H
133 ret = pthread_mutex_unlock(&mutex_buf[n]);
135 DEBUGF(fprintf(stderr,
136 "Error: polarsslthreadlock_unlock_function failed\n"));
137 return 0; /* pthread_mutex_unlock failed */
140 #elif defined(HAVE_PROCESS_H)
142 ret = ReleaseMutex(mutex_buf[n]);
144 DEBUGF(fprintf(stderr,
145 "Error: polarsslthreadlock_unlock_function failed\n"));
146 return 0; /* pthread_mutex_lock failed */
149 #endif /* HAVE_PTHREAD_H */
153 #endif /* USE_POLARSSL || USE_MBEDTLS */