1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2013-2017, Daniel Stenberg, <daniel@haxx.se>, et al.
9 * Copyright (C) 2010, 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
10 *
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.
14 *
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.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ***************************************************************************/
23 #include "curl_setup.h"
24
25 #if (defined(USE_POLARSSL) || defined(USE_MBEDTLS)) && \
26 (defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32))
27
28 #if defined(USE_THREADS_POSIX)
29 # ifdef HAVE_PTHREAD_H
30 # include <pthread.h>
31 # endif
32 #elif defined(USE_THREADS_WIN32)
33 # ifdef HAVE_PROCESS_H
34 # include <process.h>
35 # endif
36 #endif
37
38 #include "polarssl_threadlock.h"
39 #include "curl_printf.h"
40 #include "curl_memory.h"
41 /* The last #include file should be: */
42 #include "memdebug.h"
43
44 /* number of thread locks */
45 #define NUMT 2
46
47 /* This array will store all of the mutexes available to PolarSSL. */
48 static POLARSSL_MUTEX_T *mutex_buf = NULL;
49
Curl_polarsslthreadlock_thread_setup(void)50 int Curl_polarsslthreadlock_thread_setup(void)
51 {
52 int i;
53 int ret;
54
55 mutex_buf = calloc(NUMT * sizeof(POLARSSL_MUTEX_T), 1);
56 if(!mutex_buf)
57 return 0; /* error, no number of threads defined */
58
59 #ifdef HAVE_PTHREAD_H
60 for(i = 0; i < NUMT; i++) {
61 ret = pthread_mutex_init(&mutex_buf[i], NULL);
62 if(ret)
63 return 0; /* pthread_mutex_init failed */
64 }
65 #elif defined(HAVE_PROCESS_H)
66 for(i = 0; i < NUMT; i++) {
67 mutex_buf[i] = CreateMutex(0, FALSE, 0);
68 if(mutex_buf[i] == 0)
69 return 0; /* CreateMutex failed */
70 }
71 #endif /* HAVE_PTHREAD_H */
72
73 return 1; /* OK */
74 }
75
Curl_polarsslthreadlock_thread_cleanup(void)76 int Curl_polarsslthreadlock_thread_cleanup(void)
77 {
78 int i;
79 int ret;
80
81 if(!mutex_buf)
82 return 0; /* error, no threads locks defined */
83
84 #ifdef HAVE_PTHREAD_H
85 for(i = 0; i < NUMT; i++) {
86 ret = pthread_mutex_destroy(&mutex_buf[i]);
87 if(ret)
88 return 0; /* pthread_mutex_destroy failed */
89 }
90 #elif defined(HAVE_PROCESS_H)
91 for(i = 0; i < NUMT; i++) {
92 ret = CloseHandle(mutex_buf[i]);
93 if(!ret)
94 return 0; /* CloseHandle failed */
95 }
96 #endif /* HAVE_PTHREAD_H */
97 free(mutex_buf);
98 mutex_buf = NULL;
99
100 return 1; /* OK */
101 }
102
Curl_polarsslthreadlock_lock_function(int n)103 int Curl_polarsslthreadlock_lock_function(int n)
104 {
105 int ret;
106 #ifdef HAVE_PTHREAD_H
107 if(n < NUMT) {
108 ret = pthread_mutex_lock(&mutex_buf[n]);
109 if(ret) {
110 DEBUGF(fprintf(stderr,
111 "Error: polarsslthreadlock_lock_function failed\n"));
112 return 0; /* pthread_mutex_lock failed */
113 }
114 }
115 #elif defined(HAVE_PROCESS_H)
116 if(n < NUMT) {
117 ret = (WaitForSingleObject(mutex_buf[n], INFINITE) == WAIT_FAILED?1:0);
118 if(ret) {
119 DEBUGF(fprintf(stderr,
120 "Error: polarsslthreadlock_lock_function failed\n"));
121 return 0; /* pthread_mutex_lock failed */
122 }
123 }
124 #endif /* HAVE_PTHREAD_H */
125 return 1; /* OK */
126 }
127
Curl_polarsslthreadlock_unlock_function(int n)128 int Curl_polarsslthreadlock_unlock_function(int n)
129 {
130 int ret;
131 #ifdef HAVE_PTHREAD_H
132 if(n < NUMT) {
133 ret = pthread_mutex_unlock(&mutex_buf[n]);
134 if(ret) {
135 DEBUGF(fprintf(stderr,
136 "Error: polarsslthreadlock_unlock_function failed\n"));
137 return 0; /* pthread_mutex_unlock failed */
138 }
139 }
140 #elif defined(HAVE_PROCESS_H)
141 if(n < NUMT) {
142 ret = ReleaseMutex(mutex_buf[n]);
143 if(!ret) {
144 DEBUGF(fprintf(stderr,
145 "Error: polarsslthreadlock_unlock_function failed\n"));
146 return 0; /* pthread_mutex_lock failed */
147 }
148 }
149 #endif /* HAVE_PTHREAD_H */
150 return 1; /* OK */
151 }
152
153 #endif /* USE_POLARSSL || USE_MBEDTLS */
154