]>
Commit | Line | Data |
---|---|---|
c7017ecc | 1 | /* |
6539a0af | 2 | * Threading primitives for CUPS. |
c7017ecc | 3 | * |
ad7daa25 | 4 | * Copyright 2009-2016 by Apple Inc. |
c7017ecc | 5 | * |
6539a0af MS |
6 | * These coded instructions, statements, and computer programs are the |
7 | * property of Apple Inc. and are protected by Federal copyright | |
8 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
9 | * which should have been included with this file. If this file is | |
10 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
c7017ecc | 11 | * |
6539a0af | 12 | * This file is subject to the Apple OS-Developed Software exception. |
c7017ecc MS |
13 | */ |
14 | ||
15 | /* | |
16 | * Include necessary headers... | |
17 | */ | |
18 | ||
19 | #include "cups-private.h" | |
20 | #include "thread-private.h" | |
21 | ||
22 | ||
23 | #if defined(HAVE_PTHREAD_H) | |
ad7daa25 MS |
24 | /* |
25 | * '_cupsCondBroadcast()' - Wake up waiting threads. | |
26 | */ | |
27 | ||
28 | void | |
29 | _cupsCondBroadcast(_cups_cond_t *cond) /* I - Condition */ | |
30 | { | |
31 | pthread_cond_broadcast(cond); | |
32 | } | |
33 | ||
34 | ||
35 | /* | |
36 | * '_cupsCondInit()' - Initialize a condition variable. | |
37 | */ | |
38 | ||
39 | void | |
40 | _cupsCondInit(_cups_cond_t *cond) /* I - Condition */ | |
41 | { | |
42 | pthread_cond_init(cond, NULL); | |
43 | } | |
44 | ||
45 | ||
46 | /* | |
47 | * '_cupsCondWait()' - Wait for a condition with optional timeout. | |
48 | */ | |
49 | ||
50 | void | |
51 | _cupsCondWait(_cups_cond_t *cond, /* I - Condition */ | |
52 | _cups_mutex_t *mutex, /* I - Mutex */ | |
53 | double timeout) /* I - Timeout in seconds (0 or negative for none) */ | |
54 | { | |
55 | if (timeout > 0.0) | |
56 | { | |
57 | struct timespec abstime; /* Timeout */ | |
58 | ||
59 | abstime.tv_sec = (long)timeout; | |
60 | abstime.tv_nsec = (long)(1000000000 * (timeout - (long)timeout)); | |
61 | ||
62 | pthread_cond_timedwait(cond, mutex, &abstime); | |
63 | } | |
64 | else | |
65 | pthread_cond_wait(cond, mutex); | |
66 | } | |
67 | ||
68 | ||
1106b00e MS |
69 | /* |
70 | * '_cupsMutexInit()' - Initialize a mutex. | |
71 | */ | |
72 | ||
73 | void | |
74 | _cupsMutexInit(_cups_mutex_t *mutex) /* I - Mutex */ | |
75 | { | |
76 | pthread_mutex_init(mutex, NULL); | |
77 | } | |
78 | ||
79 | ||
c7017ecc MS |
80 | /* |
81 | * '_cupsMutexLock()' - Lock a mutex. | |
82 | */ | |
83 | ||
84 | void | |
85 | _cupsMutexLock(_cups_mutex_t *mutex) /* I - Mutex */ | |
86 | { | |
87 | pthread_mutex_lock(mutex); | |
88 | } | |
89 | ||
90 | ||
91 | /* | |
92 | * '_cupsMutexUnlock()' - Unlock a mutex. | |
93 | */ | |
94 | ||
95 | void | |
96 | _cupsMutexUnlock(_cups_mutex_t *mutex) /* I - Mutex */ | |
97 | { | |
98 | pthread_mutex_unlock(mutex); | |
99 | } | |
100 | ||
101 | ||
1106b00e MS |
102 | /* |
103 | * '_cupsRWInit()' - Initialize a reader/writer lock. | |
104 | */ | |
105 | ||
106 | void | |
107 | _cupsRWInit(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ | |
108 | { | |
109 | pthread_rwlock_init(rwlock, NULL); | |
110 | } | |
111 | ||
112 | ||
113 | /* | |
114 | * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading. | |
115 | */ | |
116 | ||
117 | void | |
118 | _cupsRWLockRead(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ | |
119 | { | |
120 | pthread_rwlock_rdlock(rwlock); | |
121 | } | |
122 | ||
123 | ||
124 | /* | |
125 | * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing. | |
126 | */ | |
127 | ||
128 | void | |
129 | _cupsRWLockWrite(_cups_rwlock_t *rwlock)/* I - Reader/writer lock */ | |
130 | { | |
131 | pthread_rwlock_wrlock(rwlock); | |
132 | } | |
133 | ||
134 | ||
135 | /* | |
136 | * '_cupsRWUnlock()' - Release a reader/writer lock. | |
137 | */ | |
138 | ||
139 | void | |
140 | _cupsRWUnlock(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ | |
141 | { | |
142 | pthread_rwlock_unlock(rwlock); | |
143 | } | |
144 | ||
145 | ||
ad7daa25 MS |
146 | /* |
147 | * '_cupsThreadCancel()' - Cancel (kill) a thread. | |
148 | */ | |
149 | ||
150 | void | |
151 | _cupsThreadCancel(_cups_thread_t thread)/* I - Thread ID */ | |
152 | { | |
153 | pthread_cancel(thread); | |
154 | } | |
155 | ||
156 | ||
c7017ecc MS |
157 | /* |
158 | * '_cupsThreadCreate()' - Create a thread. | |
159 | */ | |
160 | ||
ad7daa25 | 161 | _cups_thread_t /* O - Thread ID */ |
c7017ecc MS |
162 | _cupsThreadCreate( |
163 | _cups_thread_func_t func, /* I - Entry point */ | |
164 | void *arg) /* I - Entry point context */ | |
165 | { | |
166 | pthread_t thread; | |
167 | ||
ad7daa25 MS |
168 | if (pthread_create(&thread, NULL, (void *(*)(void *))func, arg)) |
169 | return (0); | |
170 | else | |
171 | return (thread); | |
172 | } | |
173 | ||
174 | ||
175 | /* | |
176 | * '_cupsThreadWait()' - Wait for a thread to exit. | |
177 | */ | |
178 | ||
179 | void * /* O - Return value */ | |
180 | _cupsThreadWait(_cups_thread_t thread) /* I - Thread ID */ | |
181 | { | |
182 | void *ret; /* Return value */ | |
183 | ||
184 | ||
185 | if (pthread_join(thread, &ret)) | |
186 | return (NULL); | |
187 | else | |
188 | return (ret); | |
c7017ecc MS |
189 | } |
190 | ||
191 | ||
192 | #elif defined(WIN32) | |
193 | # include <process.h> | |
194 | ||
195 | ||
1106b00e MS |
196 | /* |
197 | * '_cupsMutexInit()' - Initialize a mutex. | |
198 | */ | |
199 | ||
200 | void | |
201 | _cupsMutexInit(_cups_mutex_t *mutex) /* I - Mutex */ | |
202 | { | |
203 | InitializeCriticalSection(&mutex->m_criticalSection); | |
204 | mutex->m_init = 1; | |
205 | } | |
206 | ||
207 | ||
c7017ecc MS |
208 | /* |
209 | * '_cupsMutexLock()' - Lock a mutex. | |
210 | */ | |
211 | ||
212 | void | |
213 | _cupsMutexLock(_cups_mutex_t *mutex) /* I - Mutex */ | |
214 | { | |
215 | if (!mutex->m_init) | |
216 | { | |
217 | _cupsGlobalLock(); | |
218 | ||
219 | if (!mutex->m_init) | |
220 | { | |
221 | InitializeCriticalSection(&mutex->m_criticalSection); | |
222 | mutex->m_init = 1; | |
223 | } | |
224 | ||
225 | _cupsGlobalUnlock(); | |
226 | } | |
227 | ||
228 | EnterCriticalSection(&mutex->m_criticalSection); | |
229 | } | |
230 | ||
231 | ||
232 | /* | |
233 | * '_cupsMutexUnlock()' - Unlock a mutex. | |
234 | */ | |
235 | ||
236 | void | |
237 | _cupsMutexUnlock(_cups_mutex_t *mutex) /* I - Mutex */ | |
238 | { | |
239 | LeaveCriticalSection(&mutex->m_criticalSection); | |
240 | } | |
241 | ||
242 | ||
1106b00e MS |
243 | /* |
244 | * '_cupsRWInit()' - Initialize a reader/writer lock. | |
245 | */ | |
246 | ||
247 | void | |
248 | _cupsRWInit(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ | |
249 | { | |
250 | _cupsMutexInit((_cups_mutex_t *)rwlock); | |
251 | } | |
252 | ||
253 | ||
254 | /* | |
255 | * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading. | |
256 | */ | |
257 | ||
258 | void | |
259 | _cupsRWLockRead(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ | |
260 | { | |
261 | _cupsMutexLock((_cups_mutex_t *)rwlock); | |
262 | } | |
263 | ||
264 | ||
265 | /* | |
266 | * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing. | |
267 | */ | |
268 | ||
269 | void | |
270 | _cupsRWLockWrite(_cups_rwlock_t *rwlock)/* I - Reader/writer lock */ | |
271 | { | |
272 | _cupsMutexLock((_cups_mutex_t *)rwlock); | |
273 | } | |
274 | ||
275 | ||
276 | /* | |
277 | * '_cupsRWUnlock()' - Release a reader/writer lock. | |
278 | */ | |
279 | ||
280 | void | |
281 | _cupsRWUnlock(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ | |
282 | { | |
283 | _cupsMutexUnlock((_cups_mutex_t *)rwlock); | |
284 | } | |
285 | ||
286 | ||
ad7daa25 MS |
287 | /* |
288 | * '_cupsThreadCancel()' - Cancel (kill) a thread. | |
289 | */ | |
290 | ||
291 | void | |
292 | _cupsThreadCancel(_cups_thread_t thread)/* I - Thread ID */ | |
293 | { | |
294 | // TODO: Implement me | |
295 | } | |
296 | ||
297 | ||
c7017ecc MS |
298 | /* |
299 | * '_cupsThreadCreate()' - Create a thread. | |
300 | */ | |
301 | ||
ad7daa25 | 302 | _cups_thread_t /* O - Thread ID */ |
c7017ecc MS |
303 | _cupsThreadCreate( |
304 | _cups_thread_func_t func, /* I - Entry point */ | |
305 | void *arg) /* I - Entry point context */ | |
306 | { | |
ad7daa25 MS |
307 | return (_beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, NULL)); |
308 | } | |
309 | ||
310 | ||
311 | /* | |
312 | * '_cupsThreadWait()' - Wait for a thread to exit. | |
313 | */ | |
314 | ||
315 | void * /* O - Return value */ | |
316 | _cupsThreadWait(_cups_thread_t thread) /* I - Thread ID */ | |
317 | { | |
318 | // TODO: Implement me | |
c7017ecc MS |
319 | } |
320 | ||
321 | ||
322 | #else | |
1106b00e MS |
323 | /* |
324 | * '_cupsMutexInit()' - Initialize a mutex. | |
325 | */ | |
326 | ||
327 | void | |
328 | _cupsMutexInit(_cups_mutex_t *mutex) /* I - Mutex */ | |
329 | { | |
330 | (void)mutex; | |
331 | } | |
332 | ||
333 | ||
c7017ecc MS |
334 | /* |
335 | * '_cupsMutexLock()' - Lock a mutex. | |
336 | */ | |
337 | ||
338 | void | |
339 | _cupsMutexLock(_cups_mutex_t *mutex) /* I - Mutex */ | |
340 | { | |
1106b00e | 341 | (void)mutex; |
c7017ecc MS |
342 | } |
343 | ||
344 | ||
345 | /* | |
346 | * '_cupsMutexUnlock()' - Unlock a mutex. | |
347 | */ | |
348 | ||
349 | void | |
350 | _cupsMutexUnlock(_cups_mutex_t *mutex) /* I - Mutex */ | |
351 | { | |
1106b00e MS |
352 | (void)mutex; |
353 | } | |
354 | ||
355 | ||
356 | /* | |
357 | * '_cupsRWInit()' - Initialize a reader/writer lock. | |
358 | */ | |
359 | ||
360 | void | |
361 | _cupsRWInit(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ | |
362 | { | |
363 | (void)rwlock; | |
364 | } | |
365 | ||
366 | ||
367 | /* | |
368 | * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading. | |
369 | */ | |
370 | ||
371 | void | |
372 | _cupsRWLockRead(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ | |
373 | { | |
374 | (void)rwlock; | |
375 | } | |
376 | ||
377 | ||
378 | /* | |
379 | * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing. | |
380 | */ | |
381 | ||
382 | void | |
383 | _cupsRWLockWrite(_cups_rwlock_t *rwlock)/* I - Reader/writer lock */ | |
384 | { | |
385 | (void)rwlock; | |
386 | } | |
387 | ||
388 | ||
389 | /* | |
390 | * '_cupsRWUnlock()' - Release a reader/writer lock. | |
391 | */ | |
392 | ||
393 | void | |
394 | _cupsRWUnlock(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */ | |
395 | { | |
396 | (void)rwlock; | |
c7017ecc | 397 | } |
f3c17241 MS |
398 | |
399 | ||
ad7daa25 MS |
400 | /* |
401 | * '_cupsThreadCancel()' - Cancel (kill) a thread. | |
402 | */ | |
403 | ||
404 | void | |
405 | _cupsThreadCancel(_cups_thread_t thread)/* I - Thread ID */ | |
406 | { | |
407 | (void)thread; | |
408 | } | |
409 | ||
410 | ||
f3c17241 MS |
411 | /* |
412 | * '_cupsThreadCreate()' - Create a thread. | |
413 | */ | |
414 | ||
ad7daa25 | 415 | _cups_thread_t /* O - Thread ID */ |
f3c17241 MS |
416 | _cupsThreadCreate( |
417 | _cups_thread_func_t func, /* I - Entry point */ | |
418 | void *arg) /* I - Entry point context */ | |
419 | { | |
420 | fputs("DEBUG: CUPS was compiled without threading support, no thread " | |
421 | "created.\n", stderr); | |
422 | ||
423 | (void)func; | |
424 | (void)arg; | |
425 | ||
426 | return (0); | |
427 | } | |
ad7daa25 MS |
428 | |
429 | ||
430 | /* | |
431 | * '_cupsThreadWait()' - Wait for a thread to exit. | |
432 | */ | |
433 | ||
434 | void * /* O - Return value */ | |
435 | _cupsThreadWait(_cups_thread_t thread) /* I - Thread ID */ | |
436 | { | |
437 | (void)thread; | |
438 | ||
439 | return (NULL); | |
440 | } | |
441 | ||
c7017ecc | 442 | #endif /* HAVE_PTHREAD_H */ |