| 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 | #include <time.h> |
| 57 | #define dng_timespec timespec |
| 58 | |
| 59 | #ifdef __cplusplus |
| 60 | extern "C" |
| 61 | { |
| 62 | #endif |
| 63 | |
| 64 | /*****************************************************************************/ |
| 65 | |
| 66 | #define DNG_ETIMEDOUT 60 /* Operation timed out */ |
| 67 | |
| 68 | typedef unsigned long dng_pthread_t; |
| 69 | |
| 70 | typedef struct dng_pthread_mutex_impl *dng_pthread_mutex_t; |
| 71 | typedef struct dng_pthread_cond_impl *dng_pthread_cond_t; |
| 72 | typedef unsigned long dng_pthread_key_t; |
| 73 | |
| 74 | |
| 75 | #define DNG_PTHREAD_MUTEX_INITIALIZER ((struct dng_pthread_mutex_impl *)-1) |
| 76 | #define DNG_PTHREAD_COND_INITIALIZER ((struct dng_pthread_cond_impl *)-1) |
| 77 | |
| 78 | struct _dng_pthread_once_t { |
| 79 | int inited; |
| 80 | long semaphore; |
| 81 | }; |
| 82 | |
| 83 | typedef struct _dng_pthread_once_t dng_pthread_once_t; |
| 84 | #define DNG_PTHREAD_ONCE_INIT { 0, -1 } |
| 85 | |
| 86 | #define dng_pthread_equal(t1, t2) ((t1) == (t2)) |
| 87 | |
| 88 | typedef struct dng_pthread_attr_impl *dng_pthread_attr_t; |
| 89 | |
| 90 | int dng_pthread_attr_init(dng_pthread_attr_t *attr); |
| 91 | int dng_pthread_attr_destroy(dng_pthread_attr_t *attr); |
| 92 | |
| 93 | int dng_pthread_attr_setstacksize(dng_pthread_attr_t *attr, size_t stacksize); |
| 94 | int dng_pthread_attr_getstacksize(const dng_pthread_attr_t *attr, size_t *stacksize); |
| 95 | |
| 96 | int dng_pthread_create(dng_pthread_t *thread, const dng_pthread_attr_t * /* attrs */, void * (*func)(void *), void *arg); |
| 97 | int dng_pthread_detach(dng_pthread_t thread); |
| 98 | int dng_pthread_join(dng_pthread_t thread, void **result); |
| 99 | dng_pthread_t dng_pthread_self(); |
| 100 | void dng_pthread_exit(void *result); |
| 101 | |
| 102 | #define DNG_PTHREAD_MUTEX_RECURSIVE 0 |
| 103 | typedef unsigned long dng_pthread_mutexattr_t; |
| 104 | |
| 105 | int dng_pthread_mutexattr_init(dng_pthread_mutexattr_t *mutexattr); |
| 106 | int dng_pthread_mutexattr_settype(dng_pthread_mutexattr_t *mutexattr, int /*the options*/); |
| 107 | |
| 108 | int dng_pthread_mutex_init(dng_pthread_mutex_t *mutex, void * /* attrs */); |
| 109 | int dng_pthread_mutex_destroy(dng_pthread_mutex_t *mutex); |
| 110 | int dng_pthread_mutex_lock(dng_pthread_mutex_t *mutex); |
| 111 | int dng_pthread_mutex_unlock(dng_pthread_mutex_t *mutex); |
| 112 | |
| 113 | int dng_pthread_cond_init(dng_pthread_cond_t *cond, void * /* attrs */); |
| 114 | int dng_pthread_cond_destroy(dng_pthread_cond_t *cond); |
| 115 | int dng_pthread_cond_wait(dng_pthread_cond_t *cond, dng_pthread_mutex_t *mutex); |
| 116 | int dng_pthread_cond_timedwait(dng_pthread_cond_t *cond, dng_pthread_mutex_t *mutex, struct dng_timespec *latest_time); |
| 117 | int dng_pthread_cond_signal(dng_pthread_cond_t *cond); |
| 118 | int dng_pthread_cond_broadcast(dng_pthread_cond_t *cond); |
| 119 | |
| 120 | int dng_pthread_once(dng_pthread_once_t *once, void (*init_func)()); |
| 121 | |
| 122 | int dng_pthread_key_create(dng_pthread_key_t * key, void (*destructor) (void *)); |
| 123 | int dng_pthread_key_delete(dng_pthread_key_t key); |
| 124 | int dng_pthread_setspecific(dng_pthread_key_t key, const void *value); |
| 125 | void *dng_pthread_getspecific(dng_pthread_key_t key); |
| 126 | |
| 127 | typedef struct dng_pthread_rwlock_impl *dng_pthread_rwlock_t; |
| 128 | typedef void *pthread_rwlockattr_t; |
| 129 | |
| 130 | int dng_pthread_rwlock_destroy(dng_pthread_rwlock_t * rwlock); |
| 131 | int dng_pthread_rwlock_init(dng_pthread_rwlock_t * rwlock, const pthread_rwlockattr_t * attrs); |
| 132 | int dng_pthread_rwlock_rdlock(dng_pthread_rwlock_t * rwlock); |
| 133 | int dng_pthread_rwlock_tryrdlock(dng_pthread_rwlock_t * rwlock); |
| 134 | int dng_pthread_rwlock_trywrlock(dng_pthread_rwlock_t * rwlock); |
| 135 | int dng_pthread_rwlock_unlock(dng_pthread_rwlock_t * rwlock); |
| 136 | int dng_pthread_rwlock_wrlock(dng_pthread_rwlock_t * rwlock); |
| 137 | |
| 138 | typedef struct dng_pthread_rwlock_impl *dng_pthread_rwlock_t; |
| 139 | typedef void *pthread_rwlockattr_t; |
| 140 | |
| 141 | int dng_pthread_rwlock_destroy(dng_pthread_rwlock_t * rwlock); |
| 142 | int dng_pthread_rwlock_init(dng_pthread_rwlock_t * rwlock, const pthread_rwlockattr_t * attrs); |
| 143 | int dng_pthread_rwlock_rdlock(dng_pthread_rwlock_t * rwlock); |
| 144 | int dng_pthread_rwlock_tryrdlock(dng_pthread_rwlock_t * rwlock); |
| 145 | int dng_pthread_rwlock_trywrlock(dng_pthread_rwlock_t * rwlock); |
| 146 | int dng_pthread_rwlock_unlock(dng_pthread_rwlock_t * rwlock); |
| 147 | int dng_pthread_rwlock_wrlock(dng_pthread_rwlock_t * rwlock); |
| 148 | |
| 149 | // dng_pthread may maintain per-thread global state. This routine frees that global state. |
| 150 | // there is no need to call this for threads created by dng_pthread and one can call |
| 151 | // dng_pthread routines of a thread after dng_pthread_disassociate as the global state will |
| 152 | // be recreated as necessary. However dng_pthread_disassociate will need to be called again |
| 153 | // and there is a slight performance cost. Do not call this routine while holding a mutex, etc. |
| 154 | void dng_pthread_disassociate(); |
| 155 | |
| 156 | void dng_pthread_terminate(); |
| 157 | |
| 158 | /*****************************************************************************/ |
| 159 | |
| 160 | // Map symbols back to plain pthread names. This whole mechanism is so the DNG pthreads library |
| 161 | // symbols do not collide with another pthread emulation library |
| 162 | // that may be in use in the same linked entity. However if that is the case, it would be far better |
| 163 | // to have the DNG code use the same pthread library as the rest of the code. |
| 164 | |
| 165 | #define pthread_t dng_pthread_t |
| 166 | #define pthread_mutex_t dng_pthread_mutex_t |
| 167 | #define pthread_cond_t dng_pthread_cond_t |
| 168 | #define pthread_once_t dng_pthread_once_t |
| 169 | #define pthread_key_t dng_pthread_key_t |
| 170 | |
| 171 | #undef PTHREAD_MUTEX_INITIALIZER |
| 172 | #define PTHREAD_MUTEX_INITIALIZER DNG_PTHREAD_MUTEX_INITIALIZER |
| 173 | #undef PTHREAD_COND_INITIALIZER |
| 174 | #define PTHREAD_COND_INITIALIZER DNG_PTHREAD_COND_INITIALIZER |
| 175 | |
| 176 | #undef PTHREAD_ONCE_INIT |
| 177 | #define PTHREAD_ONCE_INIT DNG_PTHREAD_ONCE_INIT |
| 178 | |
| 179 | /* If it is defined on Windows, it probably has the wrong value... */ |
| 180 | #if defined(WIN32) || !defined(ETIMEDOUT) |
| 181 | #undef ETIMEDOUT |
| 182 | #define ETIMEDOUT DNG_ETIMEDOUT |
| 183 | #endif |
| 184 | |
| 185 | #define pthread_equal dng_pthread_equal |
| 186 | |
| 187 | #define pthread_attr_t dng_pthread_attr_t |
| 188 | |
| 189 | #define pthread_attr_init dng_pthread_attr_init |
| 190 | #define pthread_attr_destroy dng_pthread_attr_destroy |
| 191 | |
| 192 | #define pthread_attr_setstacksize dng_pthread_attr_setstacksize |
| 193 | #define pthread_attr_getstacksize dng_pthread_attr_getstacksize |
| 194 | |
| 195 | #define pthread_create dng_pthread_create |
| 196 | #define pthread_detach dng_pthread_detach |
| 197 | #define pthread_join dng_pthread_join |
| 198 | #define pthread_self dng_pthread_self |
| 199 | #define pthread_exit dng_pthread_exit |
| 200 | |
| 201 | #define pthread_mutex_init dng_pthread_mutex_init |
| 202 | #define pthread_mutex_destroy dng_pthread_mutex_destroy |
| 203 | #define pthread_mutex_lock dng_pthread_mutex_lock |
| 204 | #define pthread_mutex_unlock dng_pthread_mutex_unlock |
| 205 | |
| 206 | #define pthread_cond_init dng_pthread_cond_init |
| 207 | #define pthread_cond_destroy dng_pthread_cond_destroy |
| 208 | #define pthread_cond_wait dng_pthread_cond_wait |
| 209 | #define pthread_cond_timedwait dng_pthread_cond_timedwait |
| 210 | #define pthread_cond_signal dng_pthread_cond_signal |
| 211 | #define pthread_cond_broadcast dng_pthread_cond_broadcast |
| 212 | |
| 213 | #define pthread_once dng_pthread_once |
| 214 | |
| 215 | #define pthread_key_create dng_pthread_key_create |
| 216 | #define pthread_key_delete dng_pthread_key_delete |
| 217 | #define pthread_setspecific dng_pthread_setspecific |
| 218 | #define pthread_getspecific dng_pthread_getspecific |
| 219 | |
| 220 | #define pthread_rwlock_t dng_pthread_rwlock_t |
| 221 | |
| 222 | #define pthread_rwlock_destroy dng_pthread_rwlock_destroy |
| 223 | #define pthread_rwlock_init dng_pthread_rwlock_init |
| 224 | #define pthread_rwlock_rdlock dng_pthread_rwlock_rdlock |
| 225 | #define pthread_rwlock_tryrdlock dng_pthread_rwlock_tryrdlock |
| 226 | #define pthread_rwlock_trywrlock dng_pthread_rwlock_trywrlock |
| 227 | #define pthread_rwlock_unlock dng_pthread_rwlock_unlock |
| 228 | #define pthread_rwlock_wrlock dng_pthread_rwlock_wrlock |
| 229 | |
| 230 | /*****************************************************************************/ |
| 231 | |
| 232 | #ifdef __cplusplus |
| 233 | } |
| 234 | #endif |
| 235 | |
| 236 | /*****************************************************************************/ |
| 237 | |
| 238 | #endif |
| 239 | |
| 240 | /*****************************************************************************/ |
| 241 | |
| 242 | #ifdef __cplusplus |
| 243 | extern "C" |
| 244 | { |
| 245 | #endif |
| 246 | |
| 247 | int dng_pthread_now (struct timespec *now); |
| 248 | |
| 249 | #ifdef __cplusplus |
| 250 | } |
| 251 | #endif |
| 252 | |
| 253 | /*****************************************************************************/ |
| 254 | |
| 255 | #endif // qDNGThreadSafe |
| 256 | |
| 257 | /*****************************************************************************/ |
| 258 | |
| 259 | #endif |
| 260 | |
| 261 | /*****************************************************************************/ |
| 262 | |