]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/thread.c
2 * Threading primitives for CUPS.
4 * Copyright © 2009-2018 by Apple Inc.
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more
11 * Include necessary headers...
14 #include "cups-private.h"
15 #include "thread-private.h"
18 #if defined(HAVE_PTHREAD_H)
20 * '_cupsCondBroadcast()' - Wake up waiting threads.
24 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
26 pthread_cond_broadcast(cond
);
31 * '_cupsCondInit()' - Initialize a condition variable.
35 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
37 pthread_cond_init(cond
, NULL
);
42 * '_cupsCondWait()' - Wait for a condition with optional timeout.
46 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
47 _cups_mutex_t
*mutex
, /* I - Mutex */
48 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
52 struct timespec abstime
; /* Timeout */
54 clock_gettime(CLOCK_REALTIME
, &abstime
);
56 abstime
.tv_sec
+= (long)timeout
;
57 abstime
.tv_nsec
+= (long)(1000000000 * (timeout
- (long)timeout
));
59 while (abstime
.tv_nsec
>= 1000000000)
61 abstime
.tv_nsec
-= 1000000000;
65 pthread_cond_timedwait(cond
, mutex
, &abstime
);
68 pthread_cond_wait(cond
, mutex
);
73 * '_cupsMutexInit()' - Initialize a mutex.
77 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
79 pthread_mutex_init(mutex
, NULL
);
84 * '_cupsMutexLock()' - Lock a mutex.
88 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
90 pthread_mutex_lock(mutex
);
95 * '_cupsMutexUnlock()' - Unlock a mutex.
99 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
101 pthread_mutex_unlock(mutex
);
106 * '_cupsRWInit()' - Initialize a reader/writer lock.
110 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
112 pthread_rwlock_init(rwlock
, NULL
);
117 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
121 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
123 pthread_rwlock_rdlock(rwlock
);
128 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
132 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
134 pthread_rwlock_wrlock(rwlock
);
139 * '_cupsRWUnlock()' - Release a reader/writer lock.
143 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
145 pthread_rwlock_unlock(rwlock
);
150 * '_cupsThreadCancel()' - Cancel (kill) a thread.
154 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
156 pthread_cancel(thread
);
161 * '_cupsThreadCreate()' - Create a thread.
164 _cups_thread_t
/* O - Thread ID */
166 _cups_thread_func_t func
, /* I - Entry point */
167 void *arg
) /* I - Entry point context */
171 if (pthread_create(&thread
, NULL
, (void *(*)(void *))func
, arg
))
179 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
183 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
185 pthread_detach(thread
);
190 * '_cupsThreadWait()' - Wait for a thread to exit.
193 void * /* O - Return value */
194 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
196 void *ret
; /* Return value */
199 if (pthread_join(thread
, &ret
))
207 # include <process.h>
211 * '_cupsCondBroadcast()' - Wake up waiting threads.
215 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
217 // TODO: Implement me
222 * '_cupsCondInit()' - Initialize a condition variable.
226 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
228 // TODO: Implement me
233 * '_cupsCondWait()' - Wait for a condition with optional timeout.
237 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
238 _cups_mutex_t
*mutex
, /* I - Mutex */
239 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
241 // TODO: Implement me
246 * '_cupsMutexInit()' - Initialize a mutex.
250 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
252 InitializeCriticalSection(&mutex
->m_criticalSection
);
258 * '_cupsMutexLock()' - Lock a mutex.
262 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
270 InitializeCriticalSection(&mutex
->m_criticalSection
);
277 EnterCriticalSection(&mutex
->m_criticalSection
);
282 * '_cupsMutexUnlock()' - Unlock a mutex.
286 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
288 LeaveCriticalSection(&mutex
->m_criticalSection
);
293 * '_cupsRWInit()' - Initialize a reader/writer lock.
297 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
299 _cupsMutexInit((_cups_mutex_t
*)rwlock
);
304 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
308 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
310 _cupsMutexLock((_cups_mutex_t
*)rwlock
);
315 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
319 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
321 _cupsMutexLock((_cups_mutex_t
*)rwlock
);
326 * '_cupsRWUnlock()' - Release a reader/writer lock.
330 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
332 _cupsMutexUnlock((_cups_mutex_t
*)rwlock
);
337 * '_cupsThreadCancel()' - Cancel (kill) a thread.
341 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
343 // TODO: Implement me
348 * '_cupsThreadCreate()' - Create a thread.
351 _cups_thread_t
/* O - Thread ID */
353 _cups_thread_func_t func
, /* I - Entry point */
354 void *arg
) /* I - Entry point context */
356 return (_beginthreadex(NULL
, 0, (LPTHREAD_START_ROUTINE
)func
, arg
, 0, NULL
));
361 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
365 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
367 // TODO: Implement me
373 * '_cupsThreadWait()' - Wait for a thread to exit.
376 void * /* O - Return value */
377 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
379 // TODO: Implement me
386 #else /* No threading */
388 * '_cupsCondBroadcast()' - Wake up waiting threads.
392 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
394 // TODO: Implement me
399 * '_cupsCondInit()' - Initialize a condition variable.
403 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
405 // TODO: Implement me
410 * '_cupsCondWait()' - Wait for a condition with optional timeout.
414 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
415 _cups_mutex_t
*mutex
, /* I - Mutex */
416 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
418 // TODO: Implement me
423 * '_cupsMutexInit()' - Initialize a mutex.
427 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
434 * '_cupsMutexLock()' - Lock a mutex.
438 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
445 * '_cupsMutexUnlock()' - Unlock a mutex.
449 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
456 * '_cupsRWInit()' - Initialize a reader/writer lock.
460 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
467 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
471 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
478 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
482 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
489 * '_cupsRWUnlock()' - Release a reader/writer lock.
493 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
500 * '_cupsThreadCancel()' - Cancel (kill) a thread.
504 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
511 * '_cupsThreadCreate()' - Create a thread.
514 _cups_thread_t
/* O - Thread ID */
516 _cups_thread_func_t func
, /* I - Entry point */
517 void *arg
) /* I - Entry point context */
519 fputs("DEBUG: CUPS was compiled without threading support, no thread created.\n", stderr
);
529 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
533 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
540 * '_cupsThreadWait()' - Wait for a thread to exit.
543 void * /* O - Return value */
544 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
551 #endif /* HAVE_PTHREAD_H */