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