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