]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/thread.c
c52f77710d1842694a1431131d1c1a30473a70e6
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 timeval curtime
; /* Current time */
53 struct timespec abstime
; /* Timeout */
55 gettimeofday(&curtime
, NULL
);
57 abstime
.tv_sec
= (long)timeout
+ curtime
.tv_sec
;
58 abstime
.tv_nsec
= (long)(1000000000 * (timeout
- (long)timeout
+ 1000 * curtime
.tv_usec
));
60 while (abstime
.tv_nsec
>= 1000000000)
62 abstime
.tv_nsec
-= 1000000000;
66 pthread_cond_timedwait(cond
, mutex
, &abstime
);
69 pthread_cond_wait(cond
, mutex
);
74 * '_cupsMutexInit()' - Initialize a mutex.
78 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
80 pthread_mutex_init(mutex
, NULL
);
85 * '_cupsMutexLock()' - Lock a mutex.
89 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
91 pthread_mutex_lock(mutex
);
96 * '_cupsMutexUnlock()' - Unlock a mutex.
100 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
102 pthread_mutex_unlock(mutex
);
107 * '_cupsRWInit()' - Initialize a reader/writer lock.
111 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
113 pthread_rwlock_init(rwlock
, NULL
);
118 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
122 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
124 pthread_rwlock_rdlock(rwlock
);
129 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
133 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
135 pthread_rwlock_wrlock(rwlock
);
140 * '_cupsRWUnlock()' - Release a reader/writer lock.
144 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
146 pthread_rwlock_unlock(rwlock
);
151 * '_cupsThreadCancel()' - Cancel (kill) a thread.
155 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
157 pthread_cancel(thread
);
162 * '_cupsThreadCreate()' - Create a thread.
165 _cups_thread_t
/* O - Thread ID */
167 _cups_thread_func_t func
, /* I - Entry point */
168 void *arg
) /* I - Entry point context */
172 if (pthread_create(&thread
, NULL
, (void *(*)(void *))func
, arg
))
180 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
184 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
186 pthread_detach(thread
);
191 * '_cupsThreadWait()' - Wait for a thread to exit.
194 void * /* O - Return value */
195 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
197 void *ret
; /* Return value */
200 if (pthread_join(thread
, &ret
))
208 # include <process.h>
212 * '_cupsCondBroadcast()' - Wake up waiting threads.
216 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
218 // TODO: Implement me
223 * '_cupsCondInit()' - Initialize a condition variable.
227 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
229 // TODO: Implement me
234 * '_cupsCondWait()' - Wait for a condition with optional timeout.
238 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
239 _cups_mutex_t
*mutex
, /* I - Mutex */
240 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
242 // TODO: Implement me
247 * '_cupsMutexInit()' - Initialize a mutex.
251 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
253 InitializeCriticalSection(&mutex
->m_criticalSection
);
259 * '_cupsMutexLock()' - Lock a mutex.
263 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
271 InitializeCriticalSection(&mutex
->m_criticalSection
);
278 EnterCriticalSection(&mutex
->m_criticalSection
);
283 * '_cupsMutexUnlock()' - Unlock a mutex.
287 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
289 LeaveCriticalSection(&mutex
->m_criticalSection
);
294 * '_cupsRWInit()' - Initialize a reader/writer lock.
298 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
300 _cupsMutexInit((_cups_mutex_t
*)rwlock
);
305 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
309 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
311 _cupsMutexLock((_cups_mutex_t
*)rwlock
);
316 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
320 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
322 _cupsMutexLock((_cups_mutex_t
*)rwlock
);
327 * '_cupsRWUnlock()' - Release a reader/writer lock.
331 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
333 _cupsMutexUnlock((_cups_mutex_t
*)rwlock
);
338 * '_cupsThreadCancel()' - Cancel (kill) a thread.
342 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
344 // TODO: Implement me
349 * '_cupsThreadCreate()' - Create a thread.
352 _cups_thread_t
/* O - Thread ID */
354 _cups_thread_func_t func
, /* I - Entry point */
355 void *arg
) /* I - Entry point context */
357 return (_beginthreadex(NULL
, 0, (LPTHREAD_START_ROUTINE
)func
, arg
, 0, NULL
));
362 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
366 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
368 // TODO: Implement me
374 * '_cupsThreadWait()' - Wait for a thread to exit.
377 void * /* O - Return value */
378 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
380 // TODO: Implement me
387 #else /* No threading */
389 * '_cupsCondBroadcast()' - Wake up waiting threads.
393 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
395 // TODO: Implement me
400 * '_cupsCondInit()' - Initialize a condition variable.
404 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
406 // TODO: Implement me
411 * '_cupsCondWait()' - Wait for a condition with optional timeout.
415 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
416 _cups_mutex_t
*mutex
, /* I - Mutex */
417 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
419 // TODO: Implement me
424 * '_cupsMutexInit()' - Initialize a mutex.
428 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
435 * '_cupsMutexLock()' - Lock a mutex.
439 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
446 * '_cupsMutexUnlock()' - Unlock a mutex.
450 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
457 * '_cupsRWInit()' - Initialize a reader/writer lock.
461 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
468 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
472 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
479 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
483 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
490 * '_cupsRWUnlock()' - Release a reader/writer lock.
494 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
501 * '_cupsThreadCancel()' - Cancel (kill) a thread.
505 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
512 * '_cupsThreadCreate()' - Create a thread.
515 _cups_thread_t
/* O - Thread ID */
517 _cups_thread_func_t func
, /* I - Entry point */
518 void *arg
) /* I - Entry point context */
520 fputs("DEBUG: CUPS was compiled without threading support, no thread created.\n", stderr
);
530 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
534 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
541 * '_cupsThreadWait()' - Wait for a thread to exit.
544 void * /* O - Return value */
545 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
552 #endif /* HAVE_PTHREAD_H */