1 /*****************************************************************************/
2 // Copyright 2002-2008 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE:  Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8 
9 /* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_pthread.h#1 $ */
10 /* $DateTime: 2012/05/30 13:28:51 $ */
11 /* $Change: 832332 $ */
12 /* $Author: tknoll $ */
13 
14 /*****************************************************************************/
15 
16 #ifndef __dng_pthread__
17 #define __dng_pthread__
18 
19 /*****************************************************************************/
20 
21 #include "dng_flags.h"
22 
23 /*****************************************************************************/
24 
25 #if qDNGThreadSafe
26 
27 /*****************************************************************************/
28 
29 #if !qWinOS
30 
31 /*****************************************************************************/
32 
33 /* Try generic POSIX compile */
34 
35 #include <errno.h>
36 #include <pthread.h>
37 
38 #define dng_pthread_disassociate()
39 #define dng_pthread_terminate()
40 
41 /*****************************************************************************/
42 
43 #else
44 
45 /*****************************************************************************/
46 
47 #include <stdlib.h>
48 
49 #if _MSC_VER >= 1600
50 
51 // Get this included so ETIMEDOUT is predefined.
52 #include <errno.h>
53 
54 #endif
55 
56 #ifdef __cplusplus
57 extern "C"
58 {
59 #endif
60 
61 /*****************************************************************************/
62 
63 #define DNG_ETIMEDOUT       60              /* Operation timed out */
64 
65 struct dng_timespec {
66 	long tv_sec;
67 	long tv_nsec;
68 };
69 
70 
71 typedef unsigned long dng_pthread_t;
72 
73 typedef struct dng_pthread_mutex_impl *dng_pthread_mutex_t;
74 typedef struct dng_pthread_cond_impl *dng_pthread_cond_t;
75 typedef unsigned long dng_pthread_key_t;
76 
77 
78 #define DNG_PTHREAD_MUTEX_INITIALIZER ((struct dng_pthread_mutex_impl *)-1)
79 #define DNG_PTHREAD_COND_INITIALIZER ((struct dng_pthread_cond_impl *)-1)
80 
81 struct _dng_pthread_once_t {
82 	int inited;
83 	long semaphore;
84 };
85 
86 typedef struct _dng_pthread_once_t dng_pthread_once_t;
87 #define DNG_PTHREAD_ONCE_INIT { 0, -1 }
88 
89 #define dng_pthread_equal(t1, t2) ((t1) == (t2))
90 
91 typedef struct dng_pthread_attr_impl *dng_pthread_attr_t;
92 
93 int dng_pthread_attr_init(dng_pthread_attr_t *attr);
94 int dng_pthread_attr_destroy(dng_pthread_attr_t *attr);
95 
96 int dng_pthread_attr_setstacksize(dng_pthread_attr_t *attr, size_t stacksize);
97 int dng_pthread_attr_getstacksize(const dng_pthread_attr_t *attr, size_t *stacksize);
98 
99 int dng_pthread_create(dng_pthread_t *thread, const dng_pthread_attr_t * /* attrs */, void * (*func)(void *), void *arg);
100 int dng_pthread_detach(dng_pthread_t thread);
101 int dng_pthread_join(dng_pthread_t thread, void **result);
102 dng_pthread_t dng_pthread_self();
103 void dng_pthread_exit(void *result);
104 
105 #define DNG_PTHREAD_MUTEX_RECURSIVE 0
106 typedef unsigned long dng_pthread_mutexattr_t;
107 
108 int dng_pthread_mutexattr_init(dng_pthread_mutexattr_t *mutexattr);
109 int dng_pthread_mutexattr_settype(dng_pthread_mutexattr_t *mutexattr, int /*the options*/);
110 
111 int dng_pthread_mutex_init(dng_pthread_mutex_t *mutex, void * /* attrs */);
112 int dng_pthread_mutex_destroy(dng_pthread_mutex_t *mutex);
113 int dng_pthread_mutex_lock(dng_pthread_mutex_t *mutex);
114 int dng_pthread_mutex_unlock(dng_pthread_mutex_t *mutex);
115 
116 int dng_pthread_cond_init(dng_pthread_cond_t *cond, void * /* attrs */);
117 int dng_pthread_cond_destroy(dng_pthread_cond_t *cond);
118 int dng_pthread_cond_wait(dng_pthread_cond_t *cond, dng_pthread_mutex_t *mutex);
119 int dng_pthread_cond_timedwait(dng_pthread_cond_t *cond, dng_pthread_mutex_t *mutex, struct dng_timespec *latest_time);
120 int dng_pthread_cond_signal(dng_pthread_cond_t *cond);
121 int dng_pthread_cond_broadcast(dng_pthread_cond_t *cond);
122 
123 int dng_pthread_once(dng_pthread_once_t *once, void (*init_func)());
124 
125 int dng_pthread_key_create(dng_pthread_key_t * key, void (*destructor) (void *));
126 int dng_pthread_key_delete(dng_pthread_key_t key);
127 int dng_pthread_setspecific(dng_pthread_key_t key, const void *value);
128 void *dng_pthread_getspecific(dng_pthread_key_t key);
129 
130 typedef struct dng_pthread_rwlock_impl *dng_pthread_rwlock_t;
131 typedef void *pthread_rwlockattr_t;
132 
133 int dng_pthread_rwlock_destroy(dng_pthread_rwlock_t * rwlock);
134 int dng_pthread_rwlock_init(dng_pthread_rwlock_t * rwlock, const pthread_rwlockattr_t * attrs);
135 int dng_pthread_rwlock_rdlock(dng_pthread_rwlock_t * rwlock);
136 int dng_pthread_rwlock_tryrdlock(dng_pthread_rwlock_t * rwlock);
137 int dng_pthread_rwlock_trywrlock(dng_pthread_rwlock_t * rwlock);
138 int dng_pthread_rwlock_unlock(dng_pthread_rwlock_t * rwlock);
139 int dng_pthread_rwlock_wrlock(dng_pthread_rwlock_t * rwlock);
140 
141 typedef struct dng_pthread_rwlock_impl *dng_pthread_rwlock_t;
142 typedef void *pthread_rwlockattr_t;
143 
144 int dng_pthread_rwlock_destroy(dng_pthread_rwlock_t * rwlock);
145 int dng_pthread_rwlock_init(dng_pthread_rwlock_t * rwlock, const pthread_rwlockattr_t * attrs);
146 int dng_pthread_rwlock_rdlock(dng_pthread_rwlock_t * rwlock);
147 int dng_pthread_rwlock_tryrdlock(dng_pthread_rwlock_t * rwlock);
148 int dng_pthread_rwlock_trywrlock(dng_pthread_rwlock_t * rwlock);
149 int dng_pthread_rwlock_unlock(dng_pthread_rwlock_t * rwlock);
150 int dng_pthread_rwlock_wrlock(dng_pthread_rwlock_t * rwlock);
151 
152 // dng_pthread may maintain per-thread global state. This routine frees that global state.
153 // there is no need to call this for threads created by dng_pthread and one can call
154 // dng_pthread routines of a thread after dng_pthread_disassociate as the global state will
155 // be recreated as necessary. However dng_pthread_disassociate will need to be called again
156 // and there is a slight performance cost. Do not call this routine while holding a mutex, etc.
157 void dng_pthread_disassociate();
158 
159 void dng_pthread_terminate();
160 
161 /*****************************************************************************/
162 
163 // Map symbols back to plain pthread names. This whole mechanism is so the DNG pthreads library
164 // symbols do not collide with another pthread emulation library
165 // that may be in use in the same linked entity. However if that is the case, it would be far better
166 // to have the DNG code use the same pthread library as the rest of the code.
167 
168 #define pthread_t dng_pthread_t
169 #define pthread_mutex_t dng_pthread_mutex_t
170 #define pthread_cond_t dng_pthread_cond_t
171 #define pthread_once_t dng_pthread_once_t
172 #define pthread_key_t dng_pthread_key_t
173 
174 #undef PTHREAD_MUTEX_INITIALIZER
175 #define PTHREAD_MUTEX_INITIALIZER DNG_PTHREAD_MUTEX_INITIALIZER
176 #undef PTHREAD_COND_INITIALIZER
177 #define PTHREAD_COND_INITIALIZER DNG_PTHREAD_COND_INITIALIZER
178 
179 #undef PTHREAD_ONCE_INIT
180 #define PTHREAD_ONCE_INIT DNG_PTHREAD_ONCE_INIT
181 
182 #define timespec dng_timespec
183 
184 /* If it is defined on Windows, it probably has the wrong value... */
185 #if defined(WIN32) || !defined(ETIMEDOUT)
186 #undef ETIMEDOUT
187 #define ETIMEDOUT DNG_ETIMEDOUT
188 #endif
189 
190 #define pthread_equal dng_pthread_equal
191 
192 #define pthread_attr_t dng_pthread_attr_t
193 
194 #define pthread_attr_init dng_pthread_attr_init
195 #define pthread_attr_destroy dng_pthread_attr_destroy
196 
197 #define pthread_attr_setstacksize dng_pthread_attr_setstacksize
198 #define pthread_attr_getstacksize dng_pthread_attr_getstacksize
199 
200 #define pthread_create dng_pthread_create
201 #define pthread_detach dng_pthread_detach
202 #define pthread_join dng_pthread_join
203 #define pthread_self dng_pthread_self
204 #define pthread_exit dng_pthread_exit
205 
206 #define pthread_mutex_init dng_pthread_mutex_init
207 #define pthread_mutex_destroy dng_pthread_mutex_destroy
208 #define pthread_mutex_lock dng_pthread_mutex_lock
209 #define pthread_mutex_unlock dng_pthread_mutex_unlock
210 
211 #define pthread_cond_init dng_pthread_cond_init
212 #define pthread_cond_destroy dng_pthread_cond_destroy
213 #define pthread_cond_wait dng_pthread_cond_wait
214 #define pthread_cond_timedwait dng_pthread_cond_timedwait
215 #define pthread_cond_signal dng_pthread_cond_signal
216 #define pthread_cond_broadcast dng_pthread_cond_broadcast
217 
218 #define pthread_once dng_pthread_once
219 
220 #define pthread_key_create dng_pthread_key_create
221 #define pthread_key_delete dng_pthread_key_delete
222 #define pthread_setspecific dng_pthread_setspecific
223 #define pthread_getspecific dng_pthread_getspecific
224 
225 #define pthread_rwlock_t dng_pthread_rwlock_t
226 
227 #define pthread_rwlock_destroy dng_pthread_rwlock_destroy
228 #define pthread_rwlock_init dng_pthread_rwlock_init
229 #define pthread_rwlock_rdlock dng_pthread_rwlock_rdlock
230 #define pthread_rwlock_tryrdlock dng_pthread_rwlock_tryrdlock
231 #define pthread_rwlock_trywrlock dng_pthread_rwlock_trywrlock
232 #define pthread_rwlock_unlock dng_pthread_rwlock_unlock
233 #define pthread_rwlock_wrlock dng_pthread_rwlock_wrlock
234 
235 /*****************************************************************************/
236 
237 #ifdef __cplusplus
238 }
239 #endif
240 
241 /*****************************************************************************/
242 
243 #endif
244 
245 /*****************************************************************************/
246 
247 #ifdef __cplusplus
248 extern "C"
249 {
250 #endif
251 
252 int dng_pthread_now (struct timespec *now);
253 
254 #ifdef __cplusplus
255 }
256 #endif
257 
258 /*****************************************************************************/
259 
260 #endif // qDNGThreadSafe
261 
262 /*****************************************************************************/
263 
264 #endif
265 
266 /*****************************************************************************/
267