Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Utilities / cmcurl / lib / vtls / mbedtls_threadlock.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2013 - 2022, 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  * SPDX-License-Identifier: curl
23  *
24  ***************************************************************************/
25 #include "curl_setup.h"
26
27 #if defined(USE_MBEDTLS) &&                                     \
28   ((defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)) ||   \
29    (defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)))
30
31 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
32 #  include <pthread.h>
33 #  define MBEDTLS_MUTEX_T pthread_mutex_t
34 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
35 #  include <process.h>
36 #  define MBEDTLS_MUTEX_T HANDLE
37 #endif
38
39 #include "mbedtls_threadlock.h"
40 #include "curl_printf.h"
41 #include "curl_memory.h"
42 /* The last #include file should be: */
43 #include "memdebug.h"
44
45 /* number of thread locks */
46 #define NUMT                    2
47
48 /* This array will store all of the mutexes available to Mbedtls. */
49 static MBEDTLS_MUTEX_T *mutex_buf = NULL;
50
51 int Curl_mbedtlsthreadlock_thread_setup(void)
52 {
53   int i;
54
55   mutex_buf = calloc(NUMT * sizeof(MBEDTLS_MUTEX_T), 1);
56   if(!mutex_buf)
57     return 0;     /* error, no number of threads defined */
58
59   for(i = 0;  i < NUMT;  i++) {
60 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
61     if(pthread_mutex_init(&mutex_buf[i], NULL))
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
73 int Curl_mbedtlsthreadlock_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 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
82     if(pthread_mutex_destroy(&mutex_buf[i]))
83       return 0; /* pthread_mutex_destroy failed */
84 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
85     if(!CloseHandle(mutex_buf[i]))
86       return 0; /* CloseHandle failed */
87 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
88   }
89   free(mutex_buf);
90   mutex_buf = NULL;
91
92   return 1; /* OK */
93 }
94
95 int Curl_mbedtlsthreadlock_lock_function(int n)
96 {
97   if(n < NUMT) {
98 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
99     if(pthread_mutex_lock(&mutex_buf[n])) {
100       DEBUGF(fprintf(stderr,
101                      "Error: mbedtlsthreadlock_lock_function failed\n"));
102       return 0; /* pthread_mutex_lock failed */
103     }
104 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
105     if(WaitForSingleObject(mutex_buf[n], INFINITE) == WAIT_FAILED) {
106       DEBUGF(fprintf(stderr,
107                      "Error: mbedtlsthreadlock_lock_function failed\n"));
108       return 0; /* pthread_mutex_lock failed */
109     }
110 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
111   }
112   return 1; /* OK */
113 }
114
115 int Curl_mbedtlsthreadlock_unlock_function(int n)
116 {
117   if(n < NUMT) {
118 #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
119     if(pthread_mutex_unlock(&mutex_buf[n])) {
120       DEBUGF(fprintf(stderr,
121                      "Error: mbedtlsthreadlock_unlock_function failed\n"));
122       return 0; /* pthread_mutex_unlock failed */
123     }
124 #elif defined(USE_THREADS_WIN32) && defined(HAVE_PROCESS_H)
125     if(!ReleaseMutex(mutex_buf[n])) {
126       DEBUGF(fprintf(stderr,
127                      "Error: mbedtlsthreadlock_unlock_function failed\n"));
128       return 0; /* pthread_mutex_lock failed */
129     }
130 #endif /* USE_THREADS_POSIX && HAVE_PTHREAD_H */
131   }
132   return 1; /* OK */
133 }
134
135 #endif /* USE_MBEDTLS */