1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2010, 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
9 * Copyright (C) 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://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) && \
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"
40 #define _MPRINTF_REPLACE /* use our functions only */
41 #include <curl/mprintf.h>
43 #include "curl_memory.h"
44 /* The last #include file should be: */
47 /* number of thread locks */
50 /* This array will store all of the mutexes available to PolarSSL. */
51 static POLARSSL_MUTEX_T *mutex_buf = NULL;
53 int polarsslthreadlock_thread_setup(void)
58 mutex_buf = malloc(NUMT * sizeof(POLARSSL_MUTEX_T));
60 return 0; /* error, no number of threads defined */
63 for(i = 0; i < NUMT; i++) {
64 ret = pthread_mutex_init(&mutex_buf[i], NULL);
66 return 0; /* pthread_mutex_init failed */
68 #elif defined(HAVE_PROCESS_H)
69 for(i = 0; i < NUMT; i++) {
70 mutex_buf[i] = CreateMutex(0, FALSE, 0);
72 return 0; /* CreateMutex failed */
74 #endif /* HAVE_PTHREAD_H */
79 int polarsslthreadlock_thread_cleanup(void)
85 return 0; /* error, no threads locks defined */
88 for(i = 0; i < NUMT; i++) {
89 ret = pthread_mutex_destroy(&mutex_buf[i]);
91 return 0; /* pthread_mutex_destroy failed */
93 #elif defined(HAVE_PROCESS_H)
94 for(i = 0; i < NUMT; i++) {
95 ret = CloseHandle(mutex_buf[i]);
97 return 0; /* CloseHandle failed */
99 #endif /* HAVE_PTHREAD_H */
106 int polarsslthreadlock_lock_function(int n)
109 #ifdef HAVE_PTHREAD_H
111 ret = pthread_mutex_lock(&mutex_buf[n]);
113 DEBUGF(fprintf(stderr,
114 "Error: polarsslthreadlock_lock_function failed\n"));
115 return 0; /* pthread_mutex_lock failed */
118 #elif defined(HAVE_PROCESS_H)
120 ret = (WaitForSingleObject(mutex_buf[n], INFINITE)==WAIT_FAILED?1:0);
122 DEBUGF(fprintf(stderr,
123 "Error: polarsslthreadlock_lock_function failed\n"));
124 return 0; /* pthread_mutex_lock failed */
127 #endif /* HAVE_PTHREAD_H */
131 int polarsslthreadlock_unlock_function(int n)
134 #ifdef HAVE_PTHREAD_H
136 ret = pthread_mutex_unlock(&mutex_buf[n]);
138 DEBUGF(fprintf(stderr,
139 "Error: polarsslthreadlock_unlock_function failed\n"));
140 return 0; /* pthread_mutex_unlock failed */
143 #elif defined(HAVE_PROCESS_H)
145 ret = ReleaseMutex(mutex_buf[n]);
147 DEBUGF(fprintf(stderr,
148 "Error: polarsslthreadlock_unlock_function failed\n"));
149 return 0; /* pthread_mutex_lock failed */
152 #endif /* HAVE_PTHREAD_H */
156 #endif /* USE_POLARSSL */