#if !defined(_IMPLEMENT_H)
#define _IMPLEMENT_H
#if !defined(_WIN32_WINNT)
#define _WIN32_WINNT 0x0400
#endif
#include <windows.h>
#if defined(WINCE)
typedef VOID (APIENTRY *PAPCFUNC)(DWORD dwParam);
#endif
#include <winsock.h>
#if !defined(ETIMEDOUT)
# define ETIMEDOUT 10060
#endif
#if !defined(malloc)
#include <malloc.h>
#endif
#if defined(__CLEANUP_C)
# include <setjmp.h>
#endif
#if !defined(INT_MAX)
#include <limits.h>
#endif
#include "semaphore.h"
#include "sched.h"
#if defined(HAVE_C_INLINE) || defined(__cplusplus)
#define INLINE inline
#else
#define INLINE
#endif
#if defined(_MSC_VER) && _MSC_VER < 1300
#define PTW32_INTERLOCKED_VOLATILE
#else
#define PTW32_INTERLOCKED_VOLATILE volatile
#endif
#define PTW32_INTERLOCKED_LONG long
#define PTW32_INTERLOCKED_SIZE size_t
#define PTW32_INTERLOCKED_PVOID PVOID
#define PTW32_INTERLOCKED_LONGPTR PTW32_INTERLOCKED_VOLATILE long*
#define PTW32_INTERLOCKED_SIZEPTR PTW32_INTERLOCKED_VOLATILE size_t*
#define PTW32_INTERLOCKED_PVOID_PTR PTW32_INTERLOCKED_VOLATILE PVOID*
#if defined(__MINGW64__) || defined(__MINGW32__)
# include <stdint.h>
#elif defined(__BORLANDC__)
# define int64_t ULONGLONG
#else
# define int64_t _int64
# if defined(_MSC_VER) && _MSC_VER < 1300
typedef long intptr_t;
# endif
#endif
#if defined(PTW32_STATIC_LIB)
void ptw32_autostatic_anchor(void);
# if defined(__MINGW64__) || defined(__MINGW32__)
__attribute__((unused, used))
# endif
static void (*local_autostatic_anchor)(void) = ptw32_autostatic_anchor;
#endif
typedef enum
{
PThreadStateInitial = 0,
PThreadStateRunning,
PThreadStateSuspended,
PThreadStateCancelPending,
PThreadStateCanceling,
PThreadStateExiting,
PThreadStateLast,
PThreadStateReuse
}
PThreadState;
typedef struct ptw32_mcs_node_t_ ptw32_mcs_local_node_t;
typedef struct ptw32_mcs_node_t_* ptw32_mcs_lock_t;
typedef struct ptw32_robust_node_t_ ptw32_robust_node_t;
typedef struct ptw32_thread_t_ ptw32_thread_t;
struct ptw32_thread_t_
{
unsigned __int64 seqNumber;
HANDLE threadH;
pthread_t ptHandle;
ptw32_thread_t * prevReuse;
volatile PThreadState state;
ptw32_mcs_lock_t threadLock;
ptw32_mcs_lock_t stateLock;
HANDLE cancelEvent;
void *exitStatus;
void *parms;
void *keys;
void *nextAssoc;
#if defined(__CLEANUP_C)
jmp_buf start_mark;
#endif
#if defined(HAVE_SIGSET_T)
sigset_t sigmask;
#endif
ptw32_mcs_lock_t
robustMxListLock;
ptw32_robust_node_t*
robustMxList;
int ptErrno;
int detachState;
int sched_priority;
int cancelState;
int cancelType;
int implicit:1;
DWORD thread;
#if defined(_UWIN)
DWORD dummy[5];
#endif
size_t align;
};
#define PTW32_ATTR_VALID ((unsigned long) 0xC4C0FFEE)
struct pthread_attr_t_
{
unsigned long valid;
void *stackaddr;
size_t stacksize;
int detachstate;
struct sched_param param;
int inheritsched;
int contentionscope;
#if defined(HAVE_SIGSET_T)
sigset_t sigmask;
#endif
};
struct sem_t_
{
int value;
pthread_mutex_t lock;
HANDLE sem;
#if defined(NEED_SEM)
int leftToUnblock;
#endif
};
#define PTW32_OBJECT_AUTO_INIT ((void *)(size_t) -1)
#define PTW32_OBJECT_INVALID NULL
struct pthread_mutex_t_
{
LONG lock_idx;
int recursive_count;
int kind;
pthread_t ownerThread;
HANDLE event;
ptw32_robust_node_t*
robustNode;
};
enum ptw32_robust_state_t_
{
PTW32_ROBUST_CONSISTENT,
PTW32_ROBUST_INCONSISTENT,
PTW32_ROBUST_NOTRECOVERABLE
};
typedef enum ptw32_robust_state_t_ ptw32_robust_state_t;
struct ptw32_robust_node_t_
{
pthread_mutex_t mx;
ptw32_robust_state_t stateInconsistent;
ptw32_robust_node_t* prev;
ptw32_robust_node_t* next;
};
struct pthread_mutexattr_t_
{
int pshared;
int kind;
int robustness;
};
#define PTW32_SPIN_INVALID (0)
#define PTW32_SPIN_UNLOCKED (1)
#define PTW32_SPIN_LOCKED (2)
#define PTW32_SPIN_USE_MUTEX (3)
struct pthread_spinlock_t_
{
long interlock;
union
{
int cpus;
pthread_mutex_t mutex;
} u;
};
struct ptw32_mcs_node_t_
{
struct ptw32_mcs_node_t_ **lock;
struct ptw32_mcs_node_t_ *next;
HANDLE readyFlag;
HANDLE nextFlag;
};
struct pthread_barrier_t_
{
unsigned int nCurrentBarrierHeight;
unsigned int nInitialBarrierHeight;
int pshared;
sem_t semBarrierBreeched;
ptw32_mcs_lock_t lock;
ptw32_mcs_local_node_t proxynode;
};
struct pthread_barrierattr_t_
{
int pshared;
};
struct pthread_key_t_
{
DWORD key;
void (PTW32_CDECL *destructor) (void *);
ptw32_mcs_lock_t keyLock;
void *threads;
};
typedef struct ThreadParms ThreadParms;
struct ThreadParms
{
pthread_t tid;
void *(PTW32_CDECL *start) (void *);
void *arg;
};
struct pthread_cond_t_
{
long nWaitersBlocked;
long nWaitersGone;
long nWaitersToUnblock;
sem_t semBlockQueue;
sem_t semBlockLock;
pthread_mutex_t mtxUnblockLock;
pthread_cond_t next;
pthread_cond_t prev;
};
struct pthread_condattr_t_
{
int pshared;
};
#define PTW32_RWLOCK_MAGIC 0xfacade2
struct pthread_rwlock_t_
{
pthread_mutex_t mtxExclusiveAccess;
pthread_mutex_t mtxSharedAccessCompleted;
pthread_cond_t cndSharedAccessCompleted;
int nSharedAccessCount;
int nExclusiveAccessCount;
int nCompletedSharedAccessCount;
int nMagic;
};
struct pthread_rwlockattr_t_
{
int pshared;
};
typedef struct ThreadKeyAssoc ThreadKeyAssoc;
struct ThreadKeyAssoc
{
ptw32_thread_t * thread;
pthread_key_t key;
ThreadKeyAssoc *nextKey;
ThreadKeyAssoc *nextThread;
ThreadKeyAssoc *prevKey;
ThreadKeyAssoc *prevThread;
};
#if defined(__CLEANUP_SEH)
#define SE_SUCCESS 0x00
#define SE_INFORMATION 0x01
#define SE_WARNING 0x02
#define SE_ERROR 0x03
#define MAKE_SOFTWARE_EXCEPTION( _severity, _facility, _exception ) \
( (DWORD) ( ( (_severity) << 30 ) | \
( 1 << 29 ) | \
( 0 << 28 ) | \
( (_facility) << 16 ) | \
( (_exception) << 0 ) \
) )
#define EXCEPTION_PTW32_SERVICES \
MAKE_SOFTWARE_EXCEPTION( SE_ERROR, \
PTW32_SERVICES_FACILITY, \
PTW32_SERVICES_ERROR )
#define PTW32_SERVICES_FACILITY 0xBAD
#define PTW32_SERVICES_ERROR 0xDEED
#endif
#define PTW32_EPS_EXIT (1)
#define PTW32_EPS_CANCEL (2)
#define PTW32_MAX(a,b) ((a)<(b)?(b):(a))
#define PTW32_MIN(a,b) ((a)>(b)?(b):(a))
extern DWORD (*ptw32_register_cancelation) (PAPCFUNC, HANDLE, DWORD);
#define PTW32_THREAD_REUSE_EMPTY ((ptw32_thread_t *)(size_t) 1)
extern int ptw32_processInitialized;
extern ptw32_thread_t * ptw32_threadReuseTop;
extern ptw32_thread_t * ptw32_threadReuseBottom;
extern pthread_key_t ptw32_selfThreadKey;
extern pthread_key_t ptw32_cleanupKey;
extern pthread_cond_t ptw32_cond_list_head;
extern pthread_cond_t ptw32_cond_list_tail;
extern int ptw32_mutex_default_kind;
extern unsigned __int64 ptw32_threadSeqNumber;
extern int ptw32_concurrency;
extern int ptw32_features;
extern ptw32_mcs_lock_t ptw32_thread_reuse_lock;
extern ptw32_mcs_lock_t ptw32_mutex_test_init_lock;
extern ptw32_mcs_lock_t ptw32_cond_list_lock;
extern ptw32_mcs_lock_t ptw32_cond_test_init_lock;
extern ptw32_mcs_lock_t ptw32_rwlock_test_init_lock;
extern ptw32_mcs_lock_t ptw32_spinlock_test_init_lock;
#if defined(_UWIN)
extern int pthread_count;
#endif
#if defined(__cplusplus)
extern "C"
{
#endif
int ptw32_is_attr (const pthread_attr_t * attr);
int ptw32_cond_check_need_init (pthread_cond_t * cond);
int ptw32_mutex_check_need_init (pthread_mutex_t * mutex);
int ptw32_rwlock_check_need_init (pthread_rwlock_t * rwlock);
int ptw32_spinlock_check_need_init (pthread_spinlock_t * lock);
int ptw32_robust_mutex_inherit(pthread_mutex_t * mutex);
void ptw32_robust_mutex_add(pthread_mutex_t* mutex, pthread_t self);
void ptw32_robust_mutex_remove(pthread_mutex_t* mutex, ptw32_thread_t* otp);
DWORD
ptw32_RegisterCancelation (PAPCFUNC callback,
HANDLE threadH, DWORD callback_arg);
int ptw32_processInitialize (void);
void ptw32_processTerminate (void);
void ptw32_threadDestroy (pthread_t tid);
void ptw32_pop_cleanup_all (int execute);
pthread_t ptw32_new (void);
pthread_t ptw32_threadReusePop (void);
void ptw32_threadReusePush (pthread_t thread);
int ptw32_getprocessors (int *count);
int ptw32_setthreadpriority (pthread_t thread, int policy, int priority);
void ptw32_rwlock_cancelwrwait (void *arg);
#if ! (defined (__MINGW64__) || defined(__MINGW32__)) || (defined(__MSVCRT__) && ! defined(__DMC__))
unsigned __stdcall
#else
void
#endif
ptw32_threadStart (void *vthreadParms);
void ptw32_callUserDestroyRoutines (pthread_t thread);
int ptw32_tkAssocCreate (ptw32_thread_t * thread, pthread_key_t key);
void ptw32_tkAssocDestroy (ThreadKeyAssoc * assoc);
int ptw32_semwait (sem_t * sem);
DWORD ptw32_relmillisecs (const struct timespec * abstime);
void ptw32_mcs_lock_acquire (ptw32_mcs_lock_t * lock, ptw32_mcs_local_node_t * node);
int ptw32_mcs_lock_try_acquire (ptw32_mcs_lock_t * lock, ptw32_mcs_local_node_t * node);
void ptw32_mcs_lock_release (ptw32_mcs_local_node_t * node);
void ptw32_mcs_node_transfer (ptw32_mcs_local_node_t * new_node, ptw32_mcs_local_node_t * old_node);
#if defined(NEED_FTIME)
void ptw32_timespec_to_filetime (const struct timespec *ts, FILETIME * ft);
void ptw32_filetime_to_timespec (const FILETIME * ft, struct timespec *ts);
#endif
#if defined(NEED_CALLOC)
#define calloc(n, s) ptw32_calloc(n, s)
void *ptw32_calloc (size_t n, size_t s);
#endif
#if defined(_MSC_VER)
#pragma warning(disable:4290)
#endif
void ptw32_throw (DWORD exception)
#if defined(__CLEANUP_CXX)
throw(ptw32_exception_cancel,ptw32_exception_exit)
#endif
;
#if defined(__cplusplus)
}
#endif
#if defined(_UWIN_)
# if defined(_MT)
# if defined(__cplusplus)
extern "C"
{
# endif
_CRTIMP unsigned long __cdecl _beginthread (void (__cdecl *) (void *),
unsigned, void *);
_CRTIMP void __cdecl _endthread (void);
_CRTIMP unsigned long __cdecl _beginthreadex (void *, unsigned,
unsigned (__stdcall *) (void *),
void *, unsigned, unsigned *);
_CRTIMP void __cdecl _endthreadex (unsigned);
# if defined(__cplusplus)
}
# endif
# endif
#else
# include <process.h>
# endif
#if defined(__cplusplus)
# define PTW32_TO_VLONG64PTR(ptr) reinterpret_cast<volatile LONG64 *>(ptr)
#else
# define PTW32_TO_VLONG64PTR(ptr) (ptr)
#endif
#if defined(__GNUC__)
# if defined(_WIN64)
# define PTW32_INTERLOCKED_COMPARE_EXCHANGE_64(location, value, comparand) \
({ \
__typeof (value) _result; \
__asm__ __volatile__ \
( \
"lock\n\t" \
"cmpxchgq %2,(%1)" \
:"=a" (_result) \
:"r" (location), "r" (value), "a" (comparand) \
:"memory", "cc"); \
_result; \
})
# define PTW32_INTERLOCKED_EXCHANGE_64(location, value) \
({ \
__typeof (value) _result; \
__asm__ __volatile__ \
( \
"xchgq %0,(%1)" \
:"=r" (_result) \
:"r" (location), "0" (value) \
:"memory", "cc"); \
_result; \
})
# define PTW32_INTERLOCKED_EXCHANGE_ADD_64(location, value) \
({ \
__typeof (value) _result; \
__asm__ __volatile__ \
( \
"lock\n\t" \
"xaddq %0,(%1)" \
:"=r" (_result) \
:"r" (location), "0" (value) \
:"memory", "cc"); \
_result; \
})
# define PTW32_INTERLOCKED_INCREMENT_64(location) \
({ \
PTW32_INTERLOCKED_LONG _temp = 1; \
__asm__ __volatile__ \
( \
"lock\n\t" \
"xaddq %0,(%1)" \
:"+r" (_temp) \
:"r" (location) \
:"memory", "cc"); \
++_temp; \
})
# define PTW32_INTERLOCKED_DECREMENT_64(location) \
({ \
PTW32_INTERLOCKED_LONG _temp = -1; \
__asm__ __volatile__ \
( \
"lock\n\t" \
"xaddq %2,(%1)" \
:"+r" (_temp) \
:"r" (location) \
:"memory", "cc"); \
--_temp; \
})
#endif
# define PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG(location, value, comparand) \
({ \
__typeof (value) _result; \
__asm__ __volatile__ \
( \
"lock\n\t" \
"cmpxchgl %2,(%1)" \
:"=a" (_result) \
:"r" (location), "r" (value), "a" (comparand) \
:"memory", "cc"); \
_result; \
})
# define PTW32_INTERLOCKED_EXCHANGE_LONG(location, value) \
({ \
__typeof (value) _result; \
__asm__ __volatile__ \
( \
"xchgl %0,(%1)" \
:"=r" (_result) \
:"r" (location), "0" (value) \
:"memory", "cc"); \
_result; \
})
# define PTW32_INTERLOCKED_EXCHANGE_ADD_LONG(location, value) \
({ \
__typeof (value) _result; \
__asm__ __volatile__ \
( \
"lock\n\t" \
"xaddl %0,(%1)" \
:"=r" (_result) \
:"r" (location), "0" (value) \
:"memory", "cc"); \
_result; \
})
# define PTW32_INTERLOCKED_INCREMENT_LONG(location) \
({ \
PTW32_INTERLOCKED_LONG _temp = 1; \
__asm__ __volatile__ \
( \
"lock\n\t" \
"xaddl %0,(%1)" \
:"+r" (_temp) \
:"r" (location) \
:"memory", "cc"); \
++_temp; \
})
# define PTW32_INTERLOCKED_DECREMENT_LONG(location) \
({ \
PTW32_INTERLOCKED_LONG _temp = -1; \
__asm__ __volatile__ \
( \
"lock\n\t" \
"xaddl %0,(%1)" \
:"+r" (_temp) \
:"r" (location) \
:"memory", "cc"); \
--_temp; \
})
# define PTW32_INTERLOCKED_COMPARE_EXCHANGE_PTR(location, value, comparand) \
PTW32_INTERLOCKED_COMPARE_EXCHANGE_SIZE((PTW32_INTERLOCKED_SIZEPTR)location, \
(PTW32_INTERLOCKED_SIZE)value, \
(PTW32_INTERLOCKED_SIZE)comparand)
# define PTW32_INTERLOCKED_EXCHANGE_PTR(location, value) \
PTW32_INTERLOCKED_EXCHANGE_SIZE((PTW32_INTERLOCKED_SIZEPTR)location, \
(PTW32_INTERLOCKED_SIZE)value)
#else
# if defined(_WIN64)
# define PTW32_INTERLOCKED_COMPARE_EXCHANGE_64(p,v,c) InterlockedCompareExchange64(PTW32_TO_VLONG64PTR(p),(v),(c))
# define PTW32_INTERLOCKED_EXCHANGE_64(p,v) InterlockedExchange64(PTW32_TO_VLONG64PTR(p),(v))
# define PTW32_INTERLOCKED_EXCHANGE_ADD_64(p,v) InterlockedExchangeAdd64(PTW32_TO_VLONG64PTR(p),(v))
# define PTW32_INTERLOCKED_INCREMENT_64(p) InterlockedIncrement64(PTW32_TO_VLONG64PTR(p))
# define PTW32_INTERLOCKED_DECREMENT_64(p) InterlockedDecrement64(PTW32_TO_VLONG64PTR(p))
# endif
# if defined(_MSC_VER) && _MSC_VER < 1300 && !defined(_WIN64)
# define PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG(location, value, comparand) \
((LONG)InterlockedCompareExchange((PVOID *)(location), (PVOID)(value), (PVOID)(comparand)))
# else
# define PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG InterlockedCompareExchange
# endif
# define PTW32_INTERLOCKED_EXCHANGE_LONG(p,v) InterlockedExchange((p),(v))
# define PTW32_INTERLOCKED_EXCHANGE_ADD_LONG(p,v) InterlockedExchangeAdd((p),(v))
# define PTW32_INTERLOCKED_INCREMENT_LONG(p) InterlockedIncrement((p))
# define PTW32_INTERLOCKED_DECREMENT_LONG(p) InterlockedDecrement((p))
# if defined(_MSC_VER) && _MSC_VER < 1300 && !defined(_WIN64)
# define PTW32_INTERLOCKED_COMPARE_EXCHANGE_PTR InterlockedCompareExchange
# define PTW32_INTERLOCKED_EXCHANGE_PTR(location, value) \
((PVOID)InterlockedExchange((LPLONG)(location), (LONG)(value)))
# else
# define PTW32_INTERLOCKED_COMPARE_EXCHANGE_PTR(p,v,c) InterlockedCompareExchangePointer((p),(v),(c))
# define PTW32_INTERLOCKED_EXCHANGE_PTR(p,v) InterlockedExchangePointer((p),(v))
# endif
#endif
#if defined(_WIN64)
# define PTW32_INTERLOCKED_COMPARE_EXCHANGE_SIZE(p,v,c) PTW32_INTERLOCKED_COMPARE_EXCHANGE_64(PTW32_TO_VLONG64PTR(p),(v),(c))
# define PTW32_INTERLOCKED_EXCHANGE_SIZE(p,v) PTW32_INTERLOCKED_EXCHANGE_64(PTW32_TO_VLONG64PTR(p),(v))
# define PTW32_INTERLOCKED_EXCHANGE_ADD_SIZE(p,v) PTW32_INTERLOCKED_EXCHANGE_ADD_64(PTW32_TO_VLONG64PTR(p),(v))
# define PTW32_INTERLOCKED_INCREMENT_SIZE(p) PTW32_INTERLOCKED_INCREMENT_64(PTW32_TO_VLONG64PTR(p))
# define PTW32_INTERLOCKED_DECREMENT_SIZE(p) PTW32_INTERLOCKED_DECREMENT_64(PTW32_TO_VLONG64PTR(p))
#else
# define PTW32_INTERLOCKED_COMPARE_EXCHANGE_SIZE(p,v,c) PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG((p),(v),(c))
# define PTW32_INTERLOCKED_EXCHANGE_SIZE(p,v) PTW32_INTERLOCKED_EXCHANGE_LONG((p),(v))
# define PTW32_INTERLOCKED_EXCHANGE_ADD_SIZE(p,v) PTW32_INTERLOCKED_EXCHANGE_ADD_LONG((p),(v))
# define PTW32_INTERLOCKED_INCREMENT_SIZE(p) PTW32_INTERLOCKED_INCREMENT_LONG((p))
# define PTW32_INTERLOCKED_DECREMENT_SIZE(p) PTW32_INTERLOCKED_DECREMENT_LONG((p))
#endif
#if defined(NEED_CREATETHREAD)
#define _beginthreadex(security, \
stack_size, \
start_proc, \
arg, \
flags, \
pid) \
CreateThread(security, \
stack_size, \
(LPTHREAD_START_ROUTINE) start_proc, \
arg, \
flags, \
pid)
#define _endthreadex ExitThread
#endif
#endif