File indexing completed on 2025-01-19 03:55:12
0001 /*****************************************************************************/ 0002 // Copyright 2002-2019 Adobe Systems Incorporated 0003 // All Rights Reserved. 0004 // 0005 // NOTICE: Adobe permits you to use, modify, and distribute this file in 0006 // accordance with the terms of the Adobe license agreement accompanying it. 0007 /*****************************************************************************/ 0008 0009 #ifndef __dng_pthread__ 0010 #define __dng_pthread__ 0011 0012 /*****************************************************************************/ 0013 0014 #include "dng_flags.h" 0015 0016 /*****************************************************************************/ 0017 0018 #if qDNGThreadSafe 0019 0020 /*****************************************************************************/ 0021 0022 //#if !qWinOS 0023 #ifndef _MSC_VER 0024 0025 /*****************************************************************************/ 0026 0027 /* Try generic POSIX compile */ 0028 0029 #include <errno.h> 0030 #include <pthread.h> 0031 0032 #define dng_pthread_disassociate() 0033 #define dng_pthread_terminate() 0034 0035 /*****************************************************************************/ 0036 0037 #else 0038 0039 /*****************************************************************************/ 0040 0041 #include <stdlib.h> 0042 0043 #if _MSC_VER >= 1600 0044 0045 // Get this included so ETIMEDOUT is predefined. 0046 #include <errno.h> 0047 0048 #endif 0049 0050 #ifdef __cplusplus 0051 extern "C" 0052 { 0053 #endif 0054 0055 /*****************************************************************************/ 0056 0057 #define DNG_ETIMEDOUT 60 /* Operation timed out */ 0058 0059 struct dng_timespec { 0060 long tv_sec; 0061 long tv_nsec; 0062 }; 0063 0064 0065 typedef unsigned long dng_pthread_t; 0066 0067 typedef struct dng_pthread_mutex_impl *dng_pthread_mutex_t; 0068 typedef struct dng_pthread_cond_impl *dng_pthread_cond_t; 0069 typedef unsigned long dng_pthread_key_t; 0070 0071 0072 #define DNG_PTHREAD_MUTEX_INITIALIZER ((struct dng_pthread_mutex_impl *)-1) 0073 #define DNG_PTHREAD_COND_INITIALIZER ((struct dng_pthread_cond_impl *)-1) 0074 0075 struct _dng_pthread_once_t { 0076 int inited; 0077 long semaphore; 0078 }; 0079 0080 typedef struct _dng_pthread_once_t dng_pthread_once_t; 0081 #define DNG_PTHREAD_ONCE_INIT { 0, -1 } 0082 0083 #define dng_pthread_equal(t1, t2) ((t1) == (t2)) 0084 0085 typedef struct dng_pthread_attr_impl *dng_pthread_attr_t; 0086 0087 int dng_pthread_attr_init(dng_pthread_attr_t *attr); 0088 int dng_pthread_attr_destroy(dng_pthread_attr_t *attr); 0089 0090 int dng_pthread_attr_setstacksize(dng_pthread_attr_t *attr, size_t stacksize); 0091 int dng_pthread_attr_getstacksize(const dng_pthread_attr_t *attr, size_t *stacksize); 0092 0093 int dng_pthread_create(dng_pthread_t *thread, const dng_pthread_attr_t * /* attrs */, void * (*func)(void *), void *arg); 0094 int dng_pthread_detach(dng_pthread_t thread); 0095 int dng_pthread_join(dng_pthread_t thread, void **result); 0096 dng_pthread_t dng_pthread_self(); 0097 void dng_pthread_exit(void *result); 0098 0099 #define DNG_PTHREAD_MUTEX_RECURSIVE 0 0100 typedef unsigned long dng_pthread_mutexattr_t; 0101 0102 int dng_pthread_mutexattr_init(dng_pthread_mutexattr_t *mutexattr); 0103 int dng_pthread_mutexattr_settype(dng_pthread_mutexattr_t *mutexattr, int /*the options*/); 0104 0105 int dng_pthread_mutex_init(dng_pthread_mutex_t *mutex, void * /* attrs */); 0106 int dng_pthread_mutex_destroy(dng_pthread_mutex_t *mutex); 0107 int dng_pthread_mutex_lock(dng_pthread_mutex_t *mutex); 0108 int dng_pthread_mutex_unlock(dng_pthread_mutex_t *mutex); 0109 0110 int dng_pthread_cond_init(dng_pthread_cond_t *cond, void * /* attrs */); 0111 int dng_pthread_cond_destroy(dng_pthread_cond_t *cond); 0112 int dng_pthread_cond_wait(dng_pthread_cond_t *cond, dng_pthread_mutex_t *mutex); 0113 int dng_pthread_cond_timedwait(dng_pthread_cond_t *cond, dng_pthread_mutex_t *mutex, struct dng_timespec *latest_time); 0114 int dng_pthread_cond_signal(dng_pthread_cond_t *cond); 0115 int dng_pthread_cond_broadcast(dng_pthread_cond_t *cond); 0116 0117 int dng_pthread_once(dng_pthread_once_t *once, void (*init_func)()); 0118 0119 int dng_pthread_key_create(dng_pthread_key_t * key, void (*destructor) (void *)); 0120 int dng_pthread_key_delete(dng_pthread_key_t key); 0121 int dng_pthread_setspecific(dng_pthread_key_t key, const void *value); 0122 void *dng_pthread_getspecific(dng_pthread_key_t key); 0123 0124 typedef struct dng_pthread_rwlock_impl *dng_pthread_rwlock_t; 0125 typedef void *pthread_rwlockattr_t; 0126 0127 int dng_pthread_rwlock_destroy(dng_pthread_rwlock_t * rwlock); 0128 int dng_pthread_rwlock_init(dng_pthread_rwlock_t * rwlock, const pthread_rwlockattr_t * attrs); 0129 int dng_pthread_rwlock_rdlock(dng_pthread_rwlock_t * rwlock); 0130 int dng_pthread_rwlock_tryrdlock(dng_pthread_rwlock_t * rwlock); 0131 int dng_pthread_rwlock_trywrlock(dng_pthread_rwlock_t * rwlock); 0132 int dng_pthread_rwlock_unlock(dng_pthread_rwlock_t * rwlock); 0133 int dng_pthread_rwlock_wrlock(dng_pthread_rwlock_t * rwlock); 0134 0135 // dng_pthread may maintain per-thread global state. This routine frees that global state. 0136 // there is no need to call this for threads created by dng_pthread and one can call 0137 // dng_pthread routines of a thread after dng_pthread_disassociate as the global state will 0138 // be recreated as necessary. However dng_pthread_disassociate will need to be called again 0139 // and there is a slight performance cost. Do not call this routine while holding a mutex, etc. 0140 void dng_pthread_disassociate(); 0141 0142 void dng_pthread_terminate(); 0143 0144 /*****************************************************************************/ 0145 0146 // Map symbols back to plain pthread names. This whole mechanism is so the DNG pthreads library 0147 // symbols do not collide with another pthread emulation library 0148 // that may be in use in the same linked entity. However if that is the case, it would be far better 0149 // to have the DNG code use the same pthread library as the rest of the code. 0150 0151 #define pthread_t dng_pthread_t 0152 #define pthread_mutex_t dng_pthread_mutex_t 0153 #define pthread_cond_t dng_pthread_cond_t 0154 #define pthread_once_t dng_pthread_once_t 0155 #define pthread_key_t dng_pthread_key_t 0156 0157 #undef PTHREAD_MUTEX_INITIALIZER 0158 #define PTHREAD_MUTEX_INITIALIZER DNG_PTHREAD_MUTEX_INITIALIZER 0159 #undef PTHREAD_COND_INITIALIZER 0160 #define PTHREAD_COND_INITIALIZER DNG_PTHREAD_COND_INITIALIZER 0161 0162 #undef PTHREAD_ONCE_INIT 0163 #define PTHREAD_ONCE_INIT DNG_PTHREAD_ONCE_INIT 0164 0165 #if _MSC_VER < 1900 0166 #define timespec dng_timespec 0167 #endif 0168 0169 /* If it is defined on Windows, it probably has the wrong value... */ 0170 #if defined(WIN32) || !defined(ETIMEDOUT) 0171 #undef ETIMEDOUT 0172 #define ETIMEDOUT DNG_ETIMEDOUT 0173 #endif 0174 0175 #define pthread_equal dng_pthread_equal 0176 0177 #define pthread_attr_t dng_pthread_attr_t 0178 0179 #define pthread_attr_init dng_pthread_attr_init 0180 #define pthread_attr_destroy dng_pthread_attr_destroy 0181 0182 #define pthread_attr_setstacksize dng_pthread_attr_setstacksize 0183 #define pthread_attr_getstacksize dng_pthread_attr_getstacksize 0184 0185 #define pthread_create dng_pthread_create 0186 #define pthread_detach dng_pthread_detach 0187 #define pthread_join dng_pthread_join 0188 #define pthread_self dng_pthread_self 0189 #define pthread_exit dng_pthread_exit 0190 0191 #define pthread_mutex_init dng_pthread_mutex_init 0192 #define pthread_mutex_destroy dng_pthread_mutex_destroy 0193 #define pthread_mutex_lock dng_pthread_mutex_lock 0194 #define pthread_mutex_unlock dng_pthread_mutex_unlock 0195 0196 #define pthread_cond_init dng_pthread_cond_init 0197 #define pthread_cond_destroy dng_pthread_cond_destroy 0198 #define pthread_cond_wait dng_pthread_cond_wait 0199 #define pthread_cond_timedwait dng_pthread_cond_timedwait 0200 #define pthread_cond_signal dng_pthread_cond_signal 0201 #define pthread_cond_broadcast dng_pthread_cond_broadcast 0202 0203 #define pthread_once dng_pthread_once 0204 0205 #define pthread_key_create dng_pthread_key_create 0206 #define pthread_key_delete dng_pthread_key_delete 0207 #define pthread_setspecific dng_pthread_setspecific 0208 #define pthread_getspecific dng_pthread_getspecific 0209 0210 #define pthread_rwlock_t dng_pthread_rwlock_t 0211 0212 #define pthread_rwlock_destroy dng_pthread_rwlock_destroy 0213 #define pthread_rwlock_init dng_pthread_rwlock_init 0214 #define pthread_rwlock_rdlock dng_pthread_rwlock_rdlock 0215 #define pthread_rwlock_tryrdlock dng_pthread_rwlock_tryrdlock 0216 #define pthread_rwlock_trywrlock dng_pthread_rwlock_trywrlock 0217 #define pthread_rwlock_unlock dng_pthread_rwlock_unlock 0218 #define pthread_rwlock_wrlock dng_pthread_rwlock_wrlock 0219 0220 /*****************************************************************************/ 0221 0222 #ifdef __cplusplus 0223 } 0224 #endif 0225 0226 /*****************************************************************************/ 0227 0228 #endif 0229 0230 /*****************************************************************************/ 0231 0232 #ifdef __cplusplus 0233 extern "C" 0234 { 0235 #endif 0236 0237 int dng_pthread_now (struct timespec *now); 0238 0239 #ifdef __cplusplus 0240 } 0241 #endif 0242 0243 /*****************************************************************************/ 0244 0245 #endif // qDNGThreadSafe 0246 0247 /*****************************************************************************/ 0248 0249 #endif 0250 0251 /*****************************************************************************/