comparison mupdf-source/include/mupdf/helpers/mu-threads.h @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
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 #if !defined(DISABLE_MUTHREADS)
48 #ifdef _WIN32
49 #define MU_THREAD_IMPL_TYPE 1
50 #elif defined(HAVE_PTHREAD)
51 #define MU_THREAD_IMPL_TYPE 2
52 #else
53 #define DISABLE_MUTHREADS
54 #endif
55 #endif
56
57 /*
58 Types
59 */
60 typedef struct mu_thread mu_thread;
61 typedef struct mu_semaphore mu_semaphore;
62 typedef struct mu_mutex mu_mutex;
63
64 /*
65 Semaphores
66
67 Created with a value of 0. Triggering a semaphore
68 increments the value. Waiting on a semaphore reduces
69 the value, blocking if it would become negative.
70
71 Never increment the value of a semaphore above 1, as
72 this has undefined meaning in this implementation.
73 */
74
75 /*
76 Create a semaphore.
77
78 sem: Pointer to a mu_semaphore to populate.
79
80 Returns non-zero for error.
81 */
82 int mu_create_semaphore(mu_semaphore *sem);
83
84 /*
85 Destroy a semaphore.
86 Semaphores may safely be destroyed multiple
87 times. Any semaphore initialised to zeros is
88 safe to destroy.
89
90 Never destroy a semaphore that may be being waited
91 upon, as this has undefined meaning in this
92 implementation.
93
94 sem: Pointer to a mu_semaphore to destroy.
95 */
96 void mu_destroy_semaphore(mu_semaphore *sem);
97
98 /*
99 Increment the value of the
100 semaphore. Never blocks.
101
102 sem: The semaphore to increment.
103
104 Returns non-zero on error.
105 */
106 int mu_trigger_semaphore(mu_semaphore *sem);
107
108 /*
109 Decrement the value of the
110 semaphore, blocking if this would involve making
111 the value negative.
112
113 sem: The semaphore to decrement.
114
115 Returns non-zero on error.
116 */
117 int mu_wait_semaphore(mu_semaphore *sem);
118
119 /*
120 Threads
121 */
122
123 /*
124 The type for the function that a thread runs.
125
126 arg: User supplied data.
127 */
128 typedef void (mu_thread_fn)(void *arg);
129
130 /*
131 Create a thread to run the
132 supplied function with the supplied argument.
133
134 th: Pointer to mu_thread to populate with created
135 threads information.
136
137 fn: The function for the thread to run.
138
139 arg: The argument to pass to fn.
140 */
141 int mu_create_thread(mu_thread *th, mu_thread_fn *fn, void *arg);
142
143 /*
144 Destroy a thread. This function
145 blocks until a thread has terminated normally, and
146 destroys its storage. A mu_thread may safely be destroyed
147 multiple times, as may any mu_thread initialised with
148 zeros.
149
150 th: Pointer to mu_thread to destroy.
151 */
152 void mu_destroy_thread(mu_thread *th);
153
154 /*
155 Mutexes
156
157 This implementation does not specify whether
158 mutexes are recursive or not.
159 */
160
161 /*
162 Create a mutex.
163
164 mutex: pointer to a mu_mutex to populate.
165
166 Returns non-zero on error.
167 */
168 int mu_create_mutex(mu_mutex *mutex);
169
170 /*
171 Destroy a mutex. A mu_mutex may
172 safely be destroyed several times, as may a mu_mutex
173 initialised with zeros. Never destroy locked mu_mutex.
174
175 mutex: Pointer to mu_mutex to destroy.
176 */
177 void mu_destroy_mutex(mu_mutex *mutex);
178
179 /*
180 Lock a mutex.
181
182 mutex: Mutex to lock.
183 */
184 void mu_lock_mutex(mu_mutex *mutex);
185
186 /*
187 Unlock a mutex.
188
189 mutex: Mutex to unlock.
190 */
191 void mu_unlock_mutex(mu_mutex *mutex);
192
193 /*
194 Everything under this point is implementation specific.
195 Only people looking to extend the capabilities of this
196 helper module should need to look below here.
197 */
198
199 #ifdef DISABLE_MUTHREADS
200
201 /* Null implementation */
202 struct mu_semaphore
203 {
204 int dummy;
205 };
206
207 struct mu_thread
208 {
209 int dummy;
210 };
211
212 struct mu_mutex
213 {
214 int dummy;
215 };
216
217 #elif MU_THREAD_IMPL_TYPE == 1
218
219 #include <windows.h>
220
221 /* Windows threads */
222 struct mu_semaphore
223 {
224 HANDLE handle;
225 };
226
227 struct mu_thread
228 {
229 HANDLE handle;
230 mu_thread_fn *fn;
231 void *arg;
232 };
233
234 struct mu_mutex
235 {
236 CRITICAL_SECTION mutex;
237 };
238
239 #elif MU_THREAD_IMPL_TYPE == 2
240
241 /*
242 PThreads - without working unnamed semaphores.
243
244 Neither ios nor OSX supports unnamed semaphores.
245 Named semaphores are a pain to use, so we implement
246 our own semaphores using condition variables and
247 mutexes.
248 */
249
250 #include <pthread.h>
251
252 struct mu_semaphore
253 {
254 int count;
255 pthread_mutex_t mutex;
256 pthread_cond_t cond;
257 };
258
259 struct mu_thread
260 {
261 pthread_t thread;
262 mu_thread_fn *fn;
263 void *arg;
264 };
265
266 struct mu_mutex
267 {
268 pthread_mutex_t mutex;
269 };
270
271 /*
272 Add new threading implementations here, with
273 #elif MU_THREAD_IMPL_TYPE == 3... etc.
274 */
275
276 #else
277 #error Unknown MU_THREAD_IMPL_TYPE setting
278 #endif
279
280 #endif /* MUPDF_HELPERS_MU_THREADS_H */