]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/thread.c
2 * Threading primitives for CUPS.
4 * Copyright 2009-2017 by Apple Inc.
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
10 * Include necessary headers...
13 #include "cups-private.h"
14 #include "thread-private.h"
17 #if defined(HAVE_PTHREAD_H)
19 * '_cupsCondBroadcast()' - Wake up waiting threads.
23 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
25 pthread_cond_broadcast(cond
);
30 * '_cupsCondInit()' - Initialize a condition variable.
34 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
36 pthread_cond_init(cond
, NULL
);
41 * '_cupsCondWait()' - Wait for a condition with optional timeout.
45 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
46 _cups_mutex_t
*mutex
, /* I - Mutex */
47 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
51 struct timespec abstime
; /* Timeout */
53 abstime
.tv_sec
= (long)timeout
;
54 abstime
.tv_nsec
= (long)(1000000000 * (timeout
- (long)timeout
));
56 pthread_cond_timedwait(cond
, mutex
, &abstime
);
59 pthread_cond_wait(cond
, mutex
);
64 * '_cupsMutexInit()' - Initialize a mutex.
68 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
70 pthread_mutex_init(mutex
, NULL
);
75 * '_cupsMutexLock()' - Lock a mutex.
79 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
81 pthread_mutex_lock(mutex
);
86 * '_cupsMutexUnlock()' - Unlock a mutex.
90 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
92 pthread_mutex_unlock(mutex
);
97 * '_cupsRWInit()' - Initialize a reader/writer lock.
101 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
103 pthread_rwlock_init(rwlock
, NULL
);
108 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
112 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
114 pthread_rwlock_rdlock(rwlock
);
119 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
123 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
125 pthread_rwlock_wrlock(rwlock
);
130 * '_cupsRWUnlock()' - Release a reader/writer lock.
134 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
136 pthread_rwlock_unlock(rwlock
);
141 * '_cupsThreadCancel()' - Cancel (kill) a thread.
145 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
147 pthread_cancel(thread
);
152 * '_cupsThreadCreate()' - Create a thread.
155 _cups_thread_t
/* O - Thread ID */
157 _cups_thread_func_t func
, /* I - Entry point */
158 void *arg
) /* I - Entry point context */
162 if (pthread_create(&thread
, NULL
, (void *(*)(void *))func
, arg
))
170 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
174 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
176 pthread_detach(thread
);
181 * '_cupsThreadWait()' - Wait for a thread to exit.
184 void * /* O - Return value */
185 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
187 void *ret
; /* Return value */
190 if (pthread_join(thread
, &ret
))
198 # include <process.h>
202 * '_cupsCondBroadcast()' - Wake up waiting threads.
206 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
208 // TODO: Implement me
213 * '_cupsCondInit()' - Initialize a condition variable.
217 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
219 // TODO: Implement me
224 * '_cupsCondWait()' - Wait for a condition with optional timeout.
228 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
229 _cups_mutex_t
*mutex
, /* I - Mutex */
230 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
232 // TODO: Implement me
237 * '_cupsMutexInit()' - Initialize a mutex.
241 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
243 InitializeCriticalSection(&mutex
->m_criticalSection
);
249 * '_cupsMutexLock()' - Lock a mutex.
253 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
261 InitializeCriticalSection(&mutex
->m_criticalSection
);
268 EnterCriticalSection(&mutex
->m_criticalSection
);
273 * '_cupsMutexUnlock()' - Unlock a mutex.
277 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
279 LeaveCriticalSection(&mutex
->m_criticalSection
);
284 * '_cupsRWInit()' - Initialize a reader/writer lock.
288 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
290 _cupsMutexInit((_cups_mutex_t
*)rwlock
);
295 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
299 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
301 _cupsMutexLock((_cups_mutex_t
*)rwlock
);
306 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
310 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
312 _cupsMutexLock((_cups_mutex_t
*)rwlock
);
317 * '_cupsRWUnlock()' - Release a reader/writer lock.
321 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
323 _cupsMutexUnlock((_cups_mutex_t
*)rwlock
);
328 * '_cupsThreadCancel()' - Cancel (kill) a thread.
332 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
334 // TODO: Implement me
339 * '_cupsThreadCreate()' - Create a thread.
342 _cups_thread_t
/* O - Thread ID */
344 _cups_thread_func_t func
, /* I - Entry point */
345 void *arg
) /* I - Entry point context */
347 return (_beginthreadex(NULL
, 0, (LPTHREAD_START_ROUTINE
)func
, arg
, 0, NULL
));
352 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
356 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
358 // TODO: Implement me
364 * '_cupsThreadWait()' - Wait for a thread to exit.
367 void * /* O - Return value */
368 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
370 // TODO: Implement me
377 #else /* No threading */
379 * '_cupsCondBroadcast()' - Wake up waiting threads.
383 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
385 // TODO: Implement me
390 * '_cupsCondInit()' - Initialize a condition variable.
394 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
396 // TODO: Implement me
401 * '_cupsCondWait()' - Wait for a condition with optional timeout.
405 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
406 _cups_mutex_t
*mutex
, /* I - Mutex */
407 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
409 // TODO: Implement me
414 * '_cupsMutexInit()' - Initialize a mutex.
418 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
425 * '_cupsMutexLock()' - Lock a mutex.
429 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
436 * '_cupsMutexUnlock()' - Unlock a mutex.
440 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
447 * '_cupsRWInit()' - Initialize a reader/writer lock.
451 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
458 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
462 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
469 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
473 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
480 * '_cupsRWUnlock()' - Release a reader/writer lock.
484 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
491 * '_cupsThreadCancel()' - Cancel (kill) a thread.
495 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
502 * '_cupsThreadCreate()' - Create a thread.
505 _cups_thread_t
/* O - Thread ID */
507 _cups_thread_func_t func
, /* I - Entry point */
508 void *arg
) /* I - Entry point context */
510 fputs("DEBUG: CUPS was compiled without threading support, no thread created.\n", stderr
);
520 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
524 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
531 * '_cupsThreadWait()' - Wait for a thread to exit.
534 void * /* O - Return value */
535 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
542 #endif /* HAVE_PTHREAD_H */