1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2013 - 2019, 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(HAVE_PTHREAD_H)) || \
27 (defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)))
28
29#if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
30# include <pthread.h>
31# define POLARSSL_MUTEX_T pthread_mutex_t
32#elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
33# include <process.h>
34# define POLARSSL_MUTEX_T HANDLE
35#endif
36
37#include "polarssl_threadlock.h"
38#include "curl_printf.h"
39#include "curl_memory.h"
40/* The last #include file should be: */
41#include "memdebug.h"
42
43/* number of thread locks */
44#define NUMT 2
45
46/* This array will store all of the mutexes available to PolarSSL. */
47static POLARSSL_MUTEX_T *mutex_buf = NULL;
48
49int Curl_polarsslthreadlock_thread_setup(void)
50{
51 int i;
52
53 mutex_buf = calloc(NUMT * sizeof(POLARSSL_MUTEX_T), 1);
54 if(!mutex_buf)
55 return 0; /* error, no number of threads defined */
56
57 for(i = 0; i < NUMT; i++) {
58 int ret;
59#if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
60 ret = pthread_mutex_init(&mutex_buf[i], NULL);
61 if(ret)
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);
65 if(mutex_buf[i] == 0)
66 return 0; /* CreateMutex failed */
67#endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
68 }
69
70 return 1; /* OK */
71}
72
73int Curl_polarsslthreadlock_thread_cleanup(void)
74{
75 int i;
76
77 if(!mutex_buf)
78 return 0; /* error, no threads locks defined */
79
80 for(i = 0; i < NUMT; i++) {
81 int ret;
82#if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
83 ret = pthread_mutex_destroy(&mutex_buf[i]);
84 if(ret)
85 return 0; /* pthread_mutex_destroy failed */
86#elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
87 ret = CloseHandle(mutex_buf[i]);
88 if(!ret)
89 return 0; /* CloseHandle failed */
90#endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
91 }
92 free(mutex_buf);
93 mutex_buf = NULL;
94
95 return 1; /* OK */
96}
97
98int Curl_polarsslthreadlock_lock_function(int n)
99{
100 if(n < NUMT) {
101 int ret;
102#if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
103 ret = pthread_mutex_lock(&mutex_buf[n]);
104 if(ret) {
105 DEBUGF(fprintf(stderr,
106 "Error: polarsslthreadlock_lock_function failed\n"));
107 return 0; /* pthread_mutex_lock failed */
108 }
109#elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
110 ret = (WaitForSingleObject(mutex_buf[n], INFINITE) == WAIT_FAILED?1:0);
111 if(ret) {
112 DEBUGF(fprintf(stderr,
113 "Error: polarsslthreadlock_lock_function failed\n"));
114 return 0; /* pthread_mutex_lock failed */
115 }
116#endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
117 }
118 return 1; /* OK */
119}
120
121int Curl_polarsslthreadlock_unlock_function(int n)
122{
123 if(n < NUMT) {
124 int ret;
125#if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
126 ret = pthread_mutex_unlock(&mutex_buf[n]);
127 if(ret) {
128 DEBUGF(fprintf(stderr,
129 "Error: polarsslthreadlock_unlock_function failed\n"));
130 return 0; /* pthread_mutex_unlock failed */
131 }
132#elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
133 ret = ReleaseMutex(mutex_buf[n]);
134 if(!ret) {
135 DEBUGF(fprintf(stderr,
136 "Error: polarsslthreadlock_unlock_function failed\n"));
137 return 0; /* pthread_mutex_lock failed */
138 }
139#endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
140 }
141 return 1; /* OK */
142}
143
144#endif /* USE_POLARSSL || USE_MBEDTLS */
145