]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/thread.c
983abec2d54a3f481e15d075d01f594dd5b8998f
2 * Threading primitives for CUPS.
4 * Copyright 2009-2017 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 abstime
.tv_sec
= (long)timeout
;
60 abstime
.tv_nsec
= (long)(1000000000 * (timeout
- (long)timeout
));
62 pthread_cond_timedwait(cond
, mutex
, &abstime
);
65 pthread_cond_wait(cond
, mutex
);
70 * '_cupsMutexInit()' - Initialize a mutex.
74 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
76 pthread_mutex_init(mutex
, NULL
);
81 * '_cupsMutexLock()' - Lock a mutex.
85 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
87 pthread_mutex_lock(mutex
);
92 * '_cupsMutexUnlock()' - Unlock a mutex.
96 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
98 pthread_mutex_unlock(mutex
);
103 * '_cupsRWInit()' - Initialize a reader/writer lock.
107 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
109 pthread_rwlock_init(rwlock
, NULL
);
114 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
118 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
120 pthread_rwlock_rdlock(rwlock
);
125 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
129 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
131 pthread_rwlock_wrlock(rwlock
);
136 * '_cupsRWUnlock()' - Release a reader/writer lock.
140 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
142 pthread_rwlock_unlock(rwlock
);
147 * '_cupsThreadCancel()' - Cancel (kill) a thread.
151 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
153 pthread_cancel(thread
);
158 * '_cupsThreadCreate()' - Create a thread.
161 _cups_thread_t
/* O - Thread ID */
163 _cups_thread_func_t func
, /* I - Entry point */
164 void *arg
) /* I - Entry point context */
168 if (pthread_create(&thread
, NULL
, (void *(*)(void *))func
, arg
))
176 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
180 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
182 pthread_detach(thread
);
187 * '_cupsThreadWait()' - Wait for a thread to exit.
190 void * /* O - Return value */
191 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
193 void *ret
; /* Return value */
196 if (pthread_join(thread
, &ret
))
204 # include <process.h>
208 * '_cupsCondBroadcast()' - Wake up waiting threads.
212 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
214 // TODO: Implement me
219 * '_cupsCondInit()' - Initialize a condition variable.
223 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
225 // TODO: Implement me
230 * '_cupsCondWait()' - Wait for a condition with optional timeout.
234 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
235 _cups_mutex_t
*mutex
, /* I - Mutex */
236 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
238 // TODO: Implement me
243 * '_cupsMutexInit()' - Initialize a mutex.
247 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
249 InitializeCriticalSection(&mutex
->m_criticalSection
);
255 * '_cupsMutexLock()' - Lock a mutex.
259 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
267 InitializeCriticalSection(&mutex
->m_criticalSection
);
274 EnterCriticalSection(&mutex
->m_criticalSection
);
279 * '_cupsMutexUnlock()' - Unlock a mutex.
283 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
285 LeaveCriticalSection(&mutex
->m_criticalSection
);
290 * '_cupsRWInit()' - Initialize a reader/writer lock.
294 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
296 _cupsMutexInit((_cups_mutex_t
*)rwlock
);
301 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
305 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
307 _cupsMutexLock((_cups_mutex_t
*)rwlock
);
312 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
316 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
318 _cupsMutexLock((_cups_mutex_t
*)rwlock
);
323 * '_cupsRWUnlock()' - Release a reader/writer lock.
327 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
329 _cupsMutexUnlock((_cups_mutex_t
*)rwlock
);
334 * '_cupsThreadCancel()' - Cancel (kill) a thread.
338 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
340 // TODO: Implement me
345 * '_cupsThreadCreate()' - Create a thread.
348 _cups_thread_t
/* O - Thread ID */
350 _cups_thread_func_t func
, /* I - Entry point */
351 void *arg
) /* I - Entry point context */
353 return (_beginthreadex(NULL
, 0, (LPTHREAD_START_ROUTINE
)func
, arg
, 0, NULL
));
358 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
362 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
364 // TODO: Implement me
370 * '_cupsThreadWait()' - Wait for a thread to exit.
373 void * /* O - Return value */
374 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
376 // TODO: Implement me
383 #else /* No threading */
385 * '_cupsCondBroadcast()' - Wake up waiting threads.
389 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
391 // TODO: Implement me
396 * '_cupsCondInit()' - Initialize a condition variable.
400 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
402 // TODO: Implement me
407 * '_cupsCondWait()' - Wait for a condition with optional timeout.
411 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
412 _cups_mutex_t
*mutex
, /* I - Mutex */
413 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
415 // TODO: Implement me
420 * '_cupsMutexInit()' - Initialize a mutex.
424 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
431 * '_cupsMutexLock()' - Lock a mutex.
435 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
442 * '_cupsMutexUnlock()' - Unlock a mutex.
446 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
453 * '_cupsRWInit()' - Initialize a reader/writer lock.
457 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
464 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
468 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
475 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
479 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
486 * '_cupsRWUnlock()' - Release a reader/writer lock.
490 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
497 * '_cupsThreadCancel()' - Cancel (kill) a thread.
501 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
508 * '_cupsThreadCreate()' - Create a thread.
511 _cups_thread_t
/* O - Thread ID */
513 _cups_thread_func_t func
, /* I - Entry point */
514 void *arg
) /* I - Entry point context */
516 fputs("DEBUG: CUPS was compiled without threading support, no thread created.\n", stderr
);
526 * '_cupsThreadDetach()' - Tell the OS that the thread is running independently.
530 _cupsThreadDetach(_cups_thread_t thread
)/* I - Thread ID */
537 * '_cupsThreadWait()' - Wait for a thread to exit.
540 void * /* O - Return value */
541 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
548 #endif /* HAVE_PTHREAD_H */