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