]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/jni/thirdparty/curl/lib/polarssl_threadlock.c
Add MuPDF native source codes
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / jni / thirdparty / curl / lib / polarssl_threadlock.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2010, 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
9  * Copyright (C) 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://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) && \
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
40 #define _MPRINTF_REPLACE /* use our functions only */
41 #include <curl/mprintf.h>
42
43 #include "curl_memory.h"
44 /* The last #include file should be: */
45 #include "memdebug.h"
46
47 /* number of thread locks */
48 #define NUMT                    2
49
50 /* This array will store all of the mutexes available to PolarSSL. */
51 static POLARSSL_MUTEX_T *mutex_buf = NULL;
52
53 int polarsslthreadlock_thread_setup(void)
54 {
55   int i;
56   int ret;
57
58   mutex_buf = malloc(NUMT * sizeof(POLARSSL_MUTEX_T));
59   if(!mutex_buf)
60     return 0;     /* error, no number of threads defined */
61
62 #ifdef HAVE_PTHREAD_H
63   for(i = 0;  i < NUMT;  i++) {
64     ret = pthread_mutex_init(&mutex_buf[i], NULL);
65     if(ret)
66       return 0; /* pthread_mutex_init failed */
67   }
68 #elif defined(HAVE_PROCESS_H)
69   for(i = 0;  i < NUMT;  i++) {
70     mutex_buf[i] = CreateMutex(0, FALSE, 0);
71     if(mutex_buf[i] == 0)
72       return 0;  /* CreateMutex failed */
73   }
74 #endif /* HAVE_PTHREAD_H */
75
76   return 1; /* OK */
77 }
78
79 int polarsslthreadlock_thread_cleanup(void)
80 {
81   int i;
82   int ret;
83
84   if(!mutex_buf)
85     return 0; /* error, no threads locks defined */
86
87 #ifdef HAVE_PTHREAD_H
88   for(i = 0; i < NUMT; i++) {
89     ret = pthread_mutex_destroy(&mutex_buf[i]);
90     if(ret)
91       return 0; /* pthread_mutex_destroy failed */
92   }
93 #elif defined(HAVE_PROCESS_H)
94   for(i = 0; i < NUMT; i++) {
95     ret = CloseHandle(mutex_buf[i]);
96     if(!ret)
97       return 0; /* CloseHandle failed */
98   }
99 #endif /* HAVE_PTHREAD_H */
100   free(mutex_buf);
101   mutex_buf = NULL;
102
103   return 1; /* OK */
104 }
105
106 int polarsslthreadlock_lock_function(int n)
107 {
108   int ret;
109 #ifdef HAVE_PTHREAD_H
110   if(n < NUMT) {
111     ret = pthread_mutex_lock(&mutex_buf[n]);
112     if(ret) {
113       DEBUGF(fprintf(stderr,
114                      "Error: polarsslthreadlock_lock_function failed\n"));
115       return 0; /* pthread_mutex_lock failed */
116     }
117   }
118 #elif defined(HAVE_PROCESS_H)
119   if(n < NUMT) {
120     ret = (WaitForSingleObject(mutex_buf[n], INFINITE)==WAIT_FAILED?1:0);
121     if(ret) {
122       DEBUGF(fprintf(stderr,
123                      "Error: polarsslthreadlock_lock_function failed\n"));
124       return 0; /* pthread_mutex_lock failed */
125     }
126   }
127 #endif /* HAVE_PTHREAD_H */
128   return 1; /* OK */
129 }
130
131 int polarsslthreadlock_unlock_function(int n)
132 {
133   int ret;
134 #ifdef HAVE_PTHREAD_H
135   if(n < NUMT) {
136     ret = pthread_mutex_unlock(&mutex_buf[n]);
137     if(ret) {
138       DEBUGF(fprintf(stderr,
139                      "Error: polarsslthreadlock_unlock_function failed\n"));
140       return 0; /* pthread_mutex_unlock failed */
141     }
142   }
143 #elif defined(HAVE_PROCESS_H)
144   if(n < NUMT) {
145     ret = ReleaseMutex(mutex_buf[n]);
146     if(!ret) {
147       DEBUGF(fprintf(stderr,
148                      "Error: polarsslthreadlock_unlock_function failed\n"));
149       return 0; /* pthread_mutex_lock failed */
150     }
151   }
152 #endif /* HAVE_PTHREAD_H */
153   return 1; /* OK */
154 }
155
156 #endif /* USE_POLARSSL */