751755c239f5e3102fc437ba40c74dfe636afc49
[platform/upstream/cmake.git] / Utilities / cmcurl / lib / vtls / mbedtls_threadlock.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2013 - 2021, 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.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_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 MBEDTLS_MUTEX_T pthread_mutex_t
32 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
33 #  include <process.h>
34 #  define MBEDTLS_MUTEX_T HANDLE
35 #endif
36
37 #include "mbedtls_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 Mbedtls. */
47 static MBEDTLS_MUTEX_T *mutex_buf = NULL;
48
49 int Curl_mbedtlsthreadlock_thread_setup(void)
50 {
51   int i;
52
53   mutex_buf = calloc(NUMT * sizeof(MBEDTLS_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 #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);
63     if(mutex_buf[i] == 0)
64       return 0;  /* CreateMutex failed */
65 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
66   }
67
68   return 1; /* OK */
69 }
70
71 int Curl_mbedtlsthreadlock_thread_cleanup(void)
72 {
73   int i;
74
75   if(!mutex_buf)
76     return 0; /* error, no threads locks defined */
77
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 */
86   }
87   free(mutex_buf);
88   mutex_buf = NULL;
89
90   return 1; /* OK */
91 }
92
93 int Curl_mbedtlsthreadlock_lock_function(int n)
94 {
95   if(n < NUMT) {
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 */
101     }
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 */
107     }
108 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
109   }
110   return 1; /* OK */
111 }
112
113 int Curl_mbedtlsthreadlock_unlock_function(int n)
114 {
115   if(n < NUMT) {
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 */
121     }
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 */
127     }
128 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
129   }
130   return 1; /* OK */
131 }
132
133 #endif /* USE_MBEDTLS */