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