]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/thread.c
2 * Threading primitives for CUPS.
4 * Copyright 2009-2016 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 * '_cupsThreadWait()' - Wait for a thread to exit.
179 void * /* O - Return value */
180 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
182 void *ret
; /* Return value */
185 if (pthread_join(thread
, &ret
))
193 # include <process.h>
197 * '_cupsCondBroadcast()' - Wake up waiting threads.
201 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
203 // TODO: Implement me
208 * '_cupsCondInit()' - Initialize a condition variable.
212 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
214 // TODO: Implement me
219 * '_cupsCondWait()' - Wait for a condition with optional timeout.
223 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
224 _cups_mutex_t
*mutex
, /* I - Mutex */
225 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
227 // TODO: Implement me
232 * '_cupsMutexInit()' - Initialize a mutex.
236 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
238 InitializeCriticalSection(&mutex
->m_criticalSection
);
244 * '_cupsMutexLock()' - Lock a mutex.
248 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
256 InitializeCriticalSection(&mutex
->m_criticalSection
);
263 EnterCriticalSection(&mutex
->m_criticalSection
);
268 * '_cupsMutexUnlock()' - Unlock a mutex.
272 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
274 LeaveCriticalSection(&mutex
->m_criticalSection
);
279 * '_cupsRWInit()' - Initialize a reader/writer lock.
283 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
285 _cupsMutexInit((_cups_mutex_t
*)rwlock
);
290 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
294 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
296 _cupsMutexLock((_cups_mutex_t
*)rwlock
);
301 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
305 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
307 _cupsMutexLock((_cups_mutex_t
*)rwlock
);
312 * '_cupsRWUnlock()' - Release a reader/writer lock.
316 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
318 _cupsMutexUnlock((_cups_mutex_t
*)rwlock
);
323 * '_cupsThreadCancel()' - Cancel (kill) a thread.
327 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
329 // TODO: Implement me
334 * '_cupsThreadCreate()' - Create a thread.
337 _cups_thread_t
/* O - Thread ID */
339 _cups_thread_func_t func
, /* I - Entry point */
340 void *arg
) /* I - Entry point context */
342 return (_beginthreadex(NULL
, 0, (LPTHREAD_START_ROUTINE
)func
, arg
, 0, NULL
));
347 * '_cupsThreadWait()' - Wait for a thread to exit.
350 void * /* O - Return value */
351 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
353 // TODO: Implement me
360 #else /* No threading */
362 * '_cupsCondBroadcast()' - Wake up waiting threads.
366 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
368 // TODO: Implement me
373 * '_cupsCondInit()' - Initialize a condition variable.
377 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
379 // TODO: Implement me
384 * '_cupsCondWait()' - Wait for a condition with optional timeout.
388 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
389 _cups_mutex_t
*mutex
, /* I - Mutex */
390 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
392 // TODO: Implement me
397 * '_cupsMutexInit()' - Initialize a mutex.
401 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
408 * '_cupsMutexLock()' - Lock a mutex.
412 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
419 * '_cupsMutexUnlock()' - Unlock a mutex.
423 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
430 * '_cupsRWInit()' - Initialize a reader/writer lock.
434 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
441 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
445 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
452 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
456 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
463 * '_cupsRWUnlock()' - Release a reader/writer lock.
467 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
474 * '_cupsThreadCancel()' - Cancel (kill) a thread.
478 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
485 * '_cupsThreadCreate()' - Create a thread.
488 _cups_thread_t
/* O - Thread ID */
490 _cups_thread_func_t func
, /* I - Entry point */
491 void *arg
) /* I - Entry point context */
493 fputs("DEBUG: CUPS was compiled without threading support, no thread "
494 "created.\n", stderr
);
504 * '_cupsThreadWait()' - Wait for a thread to exit.
507 void * /* O - Return value */
508 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
515 #endif /* HAVE_PTHREAD_H */