]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/thread.c
2 * Threading primitives for CUPS.
4 * Copyright © 2009-2018 by Apple Inc.
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 * missing or damaged, see the license at "http://www.cups.org/".
12 * This file is subject to the Apple OS-Developed Software exception.
16 * Include necessary headers...
19 #include "cups-private.h"
20 #include "thread-private.h"
23 #if defined(HAVE_PTHREAD_H)
25 * '_cupsCondBroadcast()' - Wake up waiting threads.
29 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
31 pthread_cond_broadcast(cond
);
36 * '_cupsCondInit()' - Initialize a condition variable.
40 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
42 pthread_cond_init(cond
, NULL
);
47 * '_cupsCondWait()' - Wait for a condition with optional timeout.
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) */
57 struct timespec abstime
; /* Timeout */
59 clock_gettime(CLOCK_REALTIME
, &abstime
);
61 abstime
.tv_sec
+= (long)timeout
;
62 abstime
.tv_nsec
+= (long)(1000000000 * (timeout
- (long)timeout
));
64 while (abstime
.tv_nsec
>= 1000000000)
66 abstime
.tv_nsec
-= 1000000000;
70 pthread_cond_timedwait(cond
, mutex
, &abstime
);
73 pthread_cond_wait(cond
, mutex
);
78 * '_cupsMutexInit()' - Initialize a mutex.
82 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
84 pthread_mutex_init(mutex
, NULL
);
89 * '_cupsMutexLock()' - Lock a mutex.
93 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
95 pthread_mutex_lock(mutex
);
100 * '_cupsMutexUnlock()' - Unlock a mutex.
104 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
106 pthread_mutex_unlock(mutex
);
111 * '_cupsRWInit()' - Initialize a reader/writer lock.
115 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
117 pthread_rwlock_init(rwlock
, NULL
);
122 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
126 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
128 pthread_rwlock_rdlock(rwlock
);
133 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
137 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
139 pthread_rwlock_wrlock(rwlock
);
144 * '_cupsRWUnlock()' - Release a reader/writer lock.
148 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
150 pthread_rwlock_unlock(rwlock
);
155 * '_cupsThreadCancel()' - Cancel (kill) a thread.
159 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
161 pthread_cancel(thread
);
166 * '_cupsThreadCreate()' - Create a thread.
169 _cups_thread_t
/* O - Thread ID */
171 _cups_thread_func_t func
, /* I - Entry point */
172 void *arg
) /* I - Entry point context */
176 if (pthread_create(&thread
, NULL
, (void *(*)(void *))func
, arg
))
184 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
188 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
190 pthread_detach(thread
);
195 * '_cupsThreadWait()' - Wait for a thread to exit.
198 void * /* O - Return value */
199 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
201 void *ret
; /* Return value */
204 if (pthread_join(thread
, &ret
))
212 # include <process.h>
216 * '_cupsCondBroadcast()' - Wake up waiting threads.
220 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
222 // TODO: Implement me
227 * '_cupsCondInit()' - Initialize a condition variable.
231 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
233 // TODO: Implement me
238 * '_cupsCondWait()' - Wait for a condition with optional timeout.
242 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
243 _cups_mutex_t
*mutex
, /* I - Mutex */
244 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
246 // TODO: Implement me
251 * '_cupsMutexInit()' - Initialize a mutex.
255 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
257 InitializeCriticalSection(&mutex
->m_criticalSection
);
263 * '_cupsMutexLock()' - Lock a mutex.
267 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
275 InitializeCriticalSection(&mutex
->m_criticalSection
);
282 EnterCriticalSection(&mutex
->m_criticalSection
);
287 * '_cupsMutexUnlock()' - Unlock a mutex.
291 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
293 LeaveCriticalSection(&mutex
->m_criticalSection
);
298 * '_cupsRWInit()' - Initialize a reader/writer lock.
302 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
304 _cupsMutexInit((_cups_mutex_t
*)rwlock
);
309 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
313 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
315 _cupsMutexLock((_cups_mutex_t
*)rwlock
);
320 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
324 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
326 _cupsMutexLock((_cups_mutex_t
*)rwlock
);
331 * '_cupsRWUnlock()' - Release a reader/writer lock.
335 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
337 _cupsMutexUnlock((_cups_mutex_t
*)rwlock
);
342 * '_cupsThreadCancel()' - Cancel (kill) a thread.
346 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
348 // TODO: Implement me
353 * '_cupsThreadCreate()' - Create a thread.
356 _cups_thread_t
/* O - Thread ID */
358 _cups_thread_func_t func
, /* I - Entry point */
359 void *arg
) /* I - Entry point context */
361 return (_beginthreadex(NULL
, 0, (LPTHREAD_START_ROUTINE
)func
, arg
, 0, NULL
));
366 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
370 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
372 // TODO: Implement me
378 * '_cupsThreadWait()' - Wait for a thread to exit.
381 void * /* O - Return value */
382 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
384 // TODO: Implement me
391 #else /* No threading */
393 * '_cupsCondBroadcast()' - Wake up waiting threads.
397 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
399 // TODO: Implement me
404 * '_cupsCondInit()' - Initialize a condition variable.
408 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
410 // TODO: Implement me
415 * '_cupsCondWait()' - Wait for a condition with optional timeout.
419 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
420 _cups_mutex_t
*mutex
, /* I - Mutex */
421 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
423 // TODO: Implement me
428 * '_cupsMutexInit()' - Initialize a mutex.
432 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
439 * '_cupsMutexLock()' - Lock a mutex.
443 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
450 * '_cupsMutexUnlock()' - Unlock a mutex.
454 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
461 * '_cupsRWInit()' - Initialize a reader/writer lock.
465 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
472 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
476 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
483 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
487 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
494 * '_cupsRWUnlock()' - Release a reader/writer lock.
498 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
505 * '_cupsThreadCancel()' - Cancel (kill) a thread.
509 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
516 * '_cupsThreadCreate()' - Create a thread.
519 _cups_thread_t
/* O - Thread ID */
521 _cups_thread_func_t func
, /* I - Entry point */
522 void *arg
) /* I - Entry point context */
524 fputs("DEBUG: CUPS was compiled without threading support, no thread created.\n", stderr
);
534 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
538 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
545 * '_cupsThreadWait()' - Wait for a thread to exit.
548 void * /* O - Return value */
549 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
556 #endif /* HAVE_PTHREAD_H */