]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/thread.c
Import CUPS v2.0b1
[thirdparty/cups.git] / cups / thread.c
1 /*
2 * "$Id: thread.c 11627 2014-02-20 16:15:09Z msweet $"
3 *
4 * Threading primitives for CUPS.
5 *
6 * Copyright 2009-2014 by Apple Inc.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
13 *
14 * This file is subject to the Apple OS-Developed Software exception.
15 */
16
17 /*
18 * Include necessary headers...
19 */
20
21 #include "cups-private.h"
22 #include "thread-private.h"
23
24
25 #if defined(HAVE_PTHREAD_H)
26 /*
27 * '_cupsMutexInit()' - Initialize a mutex.
28 */
29
30 void
31 _cupsMutexInit(_cups_mutex_t *mutex) /* I - Mutex */
32 {
33 pthread_mutex_init(mutex, NULL);
34 }
35
36
37 /*
38 * '_cupsMutexLock()' - Lock a mutex.
39 */
40
41 void
42 _cupsMutexLock(_cups_mutex_t *mutex) /* I - Mutex */
43 {
44 pthread_mutex_lock(mutex);
45 }
46
47
48 /*
49 * '_cupsMutexUnlock()' - Unlock a mutex.
50 */
51
52 void
53 _cupsMutexUnlock(_cups_mutex_t *mutex) /* I - Mutex */
54 {
55 pthread_mutex_unlock(mutex);
56 }
57
58
59 /*
60 * '_cupsRWInit()' - Initialize a reader/writer lock.
61 */
62
63 void
64 _cupsRWInit(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */
65 {
66 pthread_rwlock_init(rwlock, NULL);
67 }
68
69
70 /*
71 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
72 */
73
74 void
75 _cupsRWLockRead(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */
76 {
77 pthread_rwlock_rdlock(rwlock);
78 }
79
80
81 /*
82 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
83 */
84
85 void
86 _cupsRWLockWrite(_cups_rwlock_t *rwlock)/* I - Reader/writer lock */
87 {
88 pthread_rwlock_wrlock(rwlock);
89 }
90
91
92 /*
93 * '_cupsRWUnlock()' - Release a reader/writer lock.
94 */
95
96 void
97 _cupsRWUnlock(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */
98 {
99 pthread_rwlock_unlock(rwlock);
100 }
101
102
103 /*
104 * '_cupsThreadCreate()' - Create a thread.
105 */
106
107 int /* O - 0 on failure, 1 on success */
108 _cupsThreadCreate(
109 _cups_thread_func_t func, /* I - Entry point */
110 void *arg) /* I - Entry point context */
111 {
112 pthread_t thread;
113
114 return (pthread_create(&thread, NULL, (void *(*)(void *))func, arg) == 0);
115 }
116
117
118 #elif defined(WIN32)
119 # include <process.h>
120
121
122 /*
123 * '_cupsMutexInit()' - Initialize a mutex.
124 */
125
126 void
127 _cupsMutexInit(_cups_mutex_t *mutex) /* I - Mutex */
128 {
129 InitializeCriticalSection(&mutex->m_criticalSection);
130 mutex->m_init = 1;
131 }
132
133
134 /*
135 * '_cupsMutexLock()' - Lock a mutex.
136 */
137
138 void
139 _cupsMutexLock(_cups_mutex_t *mutex) /* I - Mutex */
140 {
141 if (!mutex->m_init)
142 {
143 _cupsGlobalLock();
144
145 if (!mutex->m_init)
146 {
147 InitializeCriticalSection(&mutex->m_criticalSection);
148 mutex->m_init = 1;
149 }
150
151 _cupsGlobalUnlock();
152 }
153
154 EnterCriticalSection(&mutex->m_criticalSection);
155 }
156
157
158 /*
159 * '_cupsMutexUnlock()' - Unlock a mutex.
160 */
161
162 void
163 _cupsMutexUnlock(_cups_mutex_t *mutex) /* I - Mutex */
164 {
165 LeaveCriticalSection(&mutex->m_criticalSection);
166 }
167
168
169 /*
170 * '_cupsRWInit()' - Initialize a reader/writer lock.
171 */
172
173 void
174 _cupsRWInit(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */
175 {
176 _cupsMutexInit((_cups_mutex_t *)rwlock);
177 }
178
179
180 /*
181 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
182 */
183
184 void
185 _cupsRWLockRead(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */
186 {
187 _cupsMutexLock((_cups_mutex_t *)rwlock);
188 }
189
190
191 /*
192 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
193 */
194
195 void
196 _cupsRWLockWrite(_cups_rwlock_t *rwlock)/* I - Reader/writer lock */
197 {
198 _cupsMutexLock((_cups_mutex_t *)rwlock);
199 }
200
201
202 /*
203 * '_cupsRWUnlock()' - Release a reader/writer lock.
204 */
205
206 void
207 _cupsRWUnlock(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */
208 {
209 _cupsMutexUnlock((_cups_mutex_t *)rwlock);
210 }
211
212
213 /*
214 * '_cupsThreadCreate()' - Create a thread.
215 */
216
217 int /* O - 0 on failure, 1 on success */
218 _cupsThreadCreate(
219 _cups_thread_func_t func, /* I - Entry point */
220 void *arg) /* I - Entry point context */
221 {
222 return (_beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL)
223 != 0);
224 }
225
226
227 #else
228 /*
229 * '_cupsMutexInit()' - Initialize a mutex.
230 */
231
232 void
233 _cupsMutexInit(_cups_mutex_t *mutex) /* I - Mutex */
234 {
235 (void)mutex;
236 }
237
238
239 /*
240 * '_cupsMutexLock()' - Lock a mutex.
241 */
242
243 void
244 _cupsMutexLock(_cups_mutex_t *mutex) /* I - Mutex */
245 {
246 (void)mutex;
247 }
248
249
250 /*
251 * '_cupsMutexUnlock()' - Unlock a mutex.
252 */
253
254 void
255 _cupsMutexUnlock(_cups_mutex_t *mutex) /* I - Mutex */
256 {
257 (void)mutex;
258 }
259
260
261 /*
262 * '_cupsRWInit()' - Initialize a reader/writer lock.
263 */
264
265 void
266 _cupsRWInit(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */
267 {
268 (void)rwlock;
269 }
270
271
272 /*
273 * '_cupsRWLockRead()' - Acquire a reader/writer lock for reading.
274 */
275
276 void
277 _cupsRWLockRead(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */
278 {
279 (void)rwlock;
280 }
281
282
283 /*
284 * '_cupsRWLockWrite()' - Acquire a reader/writer lock for writing.
285 */
286
287 void
288 _cupsRWLockWrite(_cups_rwlock_t *rwlock)/* I - Reader/writer lock */
289 {
290 (void)rwlock;
291 }
292
293
294 /*
295 * '_cupsRWUnlock()' - Release a reader/writer lock.
296 */
297
298 void
299 _cupsRWUnlock(_cups_rwlock_t *rwlock) /* I - Reader/writer lock */
300 {
301 (void)rwlock;
302 }
303
304
305 /*
306 * '_cupsThreadCreate()' - Create a thread.
307 */
308
309 int /* O - 0 on failure, 1 on success */
310 _cupsThreadCreate(
311 _cups_thread_func_t func, /* I - Entry point */
312 void *arg) /* I - Entry point context */
313 {
314 fputs("DEBUG: CUPS was compiled without threading support, no thread "
315 "created.\n", stderr);
316
317 (void)func;
318 (void)arg;
319
320 return (0);
321 }
322 #endif /* HAVE_PTHREAD_H */
323
324
325 /*
326 * End of "$Id: thread.c 11627 2014-02-20 16:15:09Z msweet $".
327 */