Debian Codegraph @

mupdf_mod/mupdf/helpers/mu-threads.h

raw
1 // Copyright (C) 2004-2021 Artifex Software, Inc.
2 //
3 // This file is part of MuPDF.
4 //
5 // MuPDF is free software: you can redistribute it and/or modify it under the
6 // terms of the GNU Affero General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or (at your option)
8 // any later version.
9 //
10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Affero General Public License
16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17 //
18 // Alternative licensing terms are available from the licensor.
19 // For commercial licensing, see <https://www.artifex.com/> or contact
20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21 // CA 94129, USA, for further information.
22
23 #ifndef MUPDF_HELPERS_MU_THREADS_H
24 #define MUPDF_HELPERS_MU_THREADS_H
25
26 /*
27 Simple threading helper library.
28 Includes implementations for Windows, pthreads,
29 and "no threads".
30
31 The "no threads" implementation simply provides types
32 and stub functions so that things will build, but abort
33 if we try to call them. This simplifies the job for
34 calling functions.
35
36 To build this library on a platform with no threading,
37 define DISABLE_MUTHREADS (or extend the ifdeffery below
38 so that it does so).
39
40 To build this library on a platform that uses a
41 threading model other than windows threads or pthreads,
42 extend the #ifdeffery below to set MUTHREAD_IMPL_TYPE
43 to an unused value, and modify mu-threads.c
44 appropriately.
45 */
46
47 /* willus mod */
48 #ifndef HAVE_PTHREAD
49 #define HAVE_PTHREAD
50 #endif
51
52 #if !defined(DISABLE_MUTHREADS)
53 #ifdef _WIN32
54 #define MU_THREAD_IMPL_TYPE 1
55 #elif defined(HAVE_PTHREAD)
56 #define MU_THREAD_IMPL_TYPE 2
57 #else
58 #define DISABLE_MUTHREADS
59 #endif
60 #endif
61
62 /*
63 Types
64 */
65 typedef struct mu_thread mu_thread;
66 typedef struct mu_semaphore mu_semaphore;
67 typedef struct mu_mutex mu_mutex;
68
69 /*
70 Semaphores
71
72 Created with a value of 0. Triggering a semaphore
73 increments the value. Waiting on a semaphore reduces
74 the value, blocking if it would become negative.
75
76 Never increment the value of a semaphore above 1, as
77 this has undefined meaning in this implementation.
78 */
79
80 /*
81 Create a semaphore.
82
83 sem: Pointer to a mu_semaphore to populate.
84
85 Returns non-zero for error.
86 */
87 int mu_create_semaphore(mu_semaphore *sem);
88
89 /*
90 Destroy a semaphore.
91 Semaphores may safely be destroyed multiple
92 times. Any semaphore initialised to zeros is
93 safe to destroy.
94
95 Never destroy a semaphore that may be being waited
96 upon, as this has undefined meaning in this
97 implementation.
98
99 sem: Pointer to a mu_semaphore to destroy.
100 */
101 void mu_destroy_semaphore(mu_semaphore *sem);
102
103 /*
104 Increment the value of the
105 semaphore. Never blocks.
106
107 sem: The semaphore to increment.
108
109 Returns non-zero on error.
110 */
111 int mu_trigger_semaphore(mu_semaphore *sem);
112
113 /*
114 Decrement the value of the
115 semaphore, blocking if this would involve making
116 the value negative.
117
118 sem: The semaphore to decrement.
119
120 Returns non-zero on error.
121 */
122 int mu_wait_semaphore(mu_semaphore *sem);
123
124 /*
125 Threads
126 */
127
128 /*
129 The type for the function that a thread runs.
130
131 arg: User supplied data.
132 */
133 typedef void (mu_thread_fn)(void *arg);
134
135 /*
136 Create a thread to run the
137 supplied function with the supplied argument.
138
139 th: Pointer to mu_thread to populate with created
140 threads information.
141
142 fn: The function for the thread to run.
143
144 arg: The argument to pass to fn.
145 */
146 int mu_create_thread(mu_thread *th, mu_thread_fn *fn, void *arg);
147
148 /*
149 Destroy a thread. This function
150 blocks until a thread has terminated normally, and
151 destroys its storage. A mu_thread may safely be destroyed
152 multiple times, as may any mu_thread initialised with
153 zeros.
154
155 th: Pointer to mu_thread to destroy.
156 */
157 void mu_destroy_thread(mu_thread *th);
158
159 /*
160 Mutexes
161
162 This implementation does not specify whether
163 mutexes are recursive or not.
164 */
165
166 /*
167 Create a mutex.
168
169 mutex: pointer to a mu_mutex to populate.
170
171 Returns non-zero on error.
172 */
173 int mu_create_mutex(mu_mutex *mutex);
174
175 /*
176 Destroy a mutex. A mu_mutex may
177 safely be destroyed several times, as may a mu_mutex
178 initialised with zeros. Never destroy locked mu_mutex.
179
180 mutex: Pointer to mu_mutex to destroy.
181 */
182 void mu_destroy_mutex(mu_mutex *mutex);
183
184 /*
185 Lock a mutex.
186
187 mutex: Mutex to lock.
188 */
189 void mu_lock_mutex(mu_mutex *mutex);
190
191 /*
192 Unlock a mutex.
193
194 mutex: Mutex to unlock.
195 */
196 void mu_unlock_mutex(mu_mutex *mutex);
197
198 /*
199 Everything under this point is implementation specific.
200 Only people looking to extend the capabilities of this
201 helper module should need to look below here.
202 */
203
204 #ifdef DISABLE_MUTHREADS
205
206 /* Null implementation */
207 struct mu_semaphore
208 {
209 int dummy;
210 };
211
212 struct mu_thread
213 {
214 int dummy;
215 };
216
217 struct mu_mutex
218 {
219 int dummy;
220 };
221
222 #elif MU_THREAD_IMPL_TYPE == 1
223
224 #include <windows.h>
225
226 /* Windows threads */
227 struct mu_semaphore
228 {
229 HANDLE handle;
230 };
231
232 struct mu_thread
233 {
234 HANDLE handle;
235 mu_thread_fn *fn;
236 void *arg;
237 };
238
239 struct mu_mutex
240 {
241 CRITICAL_SECTION mutex;
242 };
243
244 #elif MU_THREAD_IMPL_TYPE == 2
245
246 /*
247 PThreads - without working unnamed semaphores.
248
249 Neither ios nor OSX supports unnamed semaphores.
250 Named semaphores are a pain to use, so we implement
251 our own semaphores using condition variables and
252 mutexes.
253 */
254
255 #include <pthread.h>
256
257 struct mu_semaphore
258 {
259 int count;
260 pthread_mutex_t mutex;
261 pthread_cond_t cond;
262 };
263
264 struct mu_thread
265 {
266 pthread_t thread;
267 mu_thread_fn *fn;
268 void *arg;
269 };
270
271 struct mu_mutex
272 {
273 pthread_mutex_t mutex;
274 };
275
276 /*
277 Add new threading implementations here, with
278 #elif MU_THREAD_IMPL_TYPE == 3... etc.
279 */
280
281 #else
282 #error Unknown MU_THREAD_IMPL_TYPE setting
283 #endif
284
285 #endif /* MUPDF_HELPERS_MU_THREADS_H */