]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/thread.c
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 * '_cupsThreadWait()' - Wait for a thread to exit.
361 void * /* O - Return value */
362 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
364 // TODO: Implement me
371 #else /* No threading */
373 * '_cupsCondBroadcast()' - Wake up waiting threads.
377 _cupsCondBroadcast(_cups_cond_t
*cond
) /* I - Condition */
379 // TODO: Implement me
384 * '_cupsCondInit()' - Initialize a condition variable.
388 _cupsCondInit(_cups_cond_t
*cond
) /* I - Condition */
390 // TODO: Implement me
395 * '_cupsCondWait()' - Wait for a condition with optional timeout.
399 _cupsCondWait(_cups_cond_t
*cond
, /* I - Condition */
400 _cups_mutex_t
*mutex
, /* I - Mutex */
401 double timeout
) /* I - Timeout in seconds (0 or negative for none) */
403 // TODO: Implement me
408 * '_cupsMutexInit()' - Initialize a mutex.
412 _cupsMutexInit(_cups_mutex_t
*mutex
) /* I - Mutex */
419 * '_cupsMutexLock()' - Lock a mutex.
423 _cupsMutexLock(_cups_mutex_t
*mutex
) /* I - Mutex */
430 * '_cupsMutexUnlock()' - Unlock a mutex.
434 _cupsMutexUnlock(_cups_mutex_t
*mutex
) /* I - Mutex */
441 * '_cupsRWInit()' - Initialize a reader/writer lock.
445 _cupsRWInit(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
452 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
456 _cupsRWLockRead(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
463 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
467 _cupsRWLockWrite(_cups_rwlock_t
*rwlock
)/* I - Reader/writer lock */
474 * '_cupsRWUnlock()' - Release a reader/writer lock.
478 _cupsRWUnlock(_cups_rwlock_t
*rwlock
) /* I - Reader/writer lock */
485 * '_cupsThreadCancel()' - Cancel (kill) a thread.
489 _cupsThreadCancel(_cups_thread_t thread
)/* I - Thread ID */
496 * '_cupsThreadCreate()' - Create a thread.
499 _cups_thread_t
/* O - Thread ID */
501 _cups_thread_func_t func
, /* I - Entry point */
502 void *arg
) /* I - Entry point context */
504 fputs("DEBUG: CUPS was compiled without threading support, no thread "
505 "created.\n", stderr
);
515 * '_cupsThreadWait()' - Wait for a thread to exit.
518 void * /* O - Return value */
519 _cupsThreadWait(_cups_thread_t thread
) /* I - Thread ID */
526 #endif /* HAVE_PTHREAD_H */