#ifndef _WIN32THREAD_H
#define _WIN32THREAD_H
#include "win32.h"
typedef
struct
win32_cond {
LONG
waiters;
HANDLE
sem; } perl_cond;
typedef
DWORD
perl_key;
typedef
HANDLE
perl_os_thread;
#ifndef DONT_USE_CRITICAL_SECTION
typedef
CRITICAL_SECTION perl_mutex;
#define MUTEX_INIT(m) InitializeCriticalSection(m)
#define MUTEX_LOCK(m) EnterCriticalSection(m)
#define MUTEX_UNLOCK(m) LeaveCriticalSection(m)
#define MUTEX_DESTROY(m) DeleteCriticalSection(m)
#else
typedef
HANDLE
perl_mutex;
# define MUTEX_INIT(m) \
STMT_START { \
if
((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) \
Perl_croak_nocontext(
"panic: MUTEX_INIT"
); \
} STMT_END
# define MUTEX_LOCK(m) \
STMT_START { \
if
(WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
Perl_croak_nocontext(
"panic: MUTEX_LOCK"
); \
} STMT_END
# define MUTEX_UNLOCK(m) \
STMT_START { \
if
(ReleaseMutex(*(m)) == 0) \
Perl_croak_nocontext(
"panic: MUTEX_UNLOCK"
); \
} STMT_END
# define MUTEX_DESTROY(m) \
STMT_START { \
if
(CloseHandle(*(m)) == 0) \
Perl_croak_nocontext(
"panic: MUTEX_DESTROY"
); \
} STMT_END
#endif
#define COND_INIT(c) \
STMT_START { \
(c)->waiters = 0; \
(c)->sem = Win_CreateSemaphore(NULL,0,LONG_MAX,NULL); \
if
((c)->sem == NULL) \
Perl_croak_nocontext(
"panic: COND_INIT (%ld)"
,GetLastError()); \
} STMT_END
#define COND_SIGNAL(c) \
STMT_START { \
if
((c)->waiters > 0 && \
ReleaseSemaphore((c)->sem,1,NULL) == 0) \
Perl_croak_nocontext(
"panic: COND_SIGNAL (%ld)"
,GetLastError()); \
} STMT_END
#define COND_BROADCAST(c) \
STMT_START { \
if
((c)->waiters > 0 && \
ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0) \
Perl_croak_nocontext(
"panic: COND_BROADCAST (%ld)"
,GetLastError());\
} STMT_END
#define COND_WAIT(c, m) \
STMT_START { \
(c)->waiters++; \
MUTEX_UNLOCK(m); \
\
if
(WaitForSingleObject((c)->sem,INFINITE)==WAIT_FAILED)\
Perl_croak_nocontext(
"panic: COND_WAIT (%ld)"
,GetLastError()); \
\
MUTEX_LOCK(m); \
(c)->waiters--; \
} STMT_END
#define COND_DESTROY(c) \
STMT_START { \
(c)->waiters = 0; \
if
(CloseHandle((c)->sem) == 0) \
Perl_croak_nocontext(
"panic: COND_DESTROY (%ld)"
,GetLastError()); \
} STMT_END
#define DETACH(t) \
STMT_START { \
if
(CloseHandle((t)->self) == 0) { \
MUTEX_UNLOCK(&(t)->mutex); \
Perl_croak_nocontext(
"panic: DETACH"
); \
} \
} STMT_END
#ifdef USE_RTL_THREAD_API
# include <process.h>
# if defined (_MSC_VER)
# define THREAD_RET_TYPE unsigned __stdcall
# else
# define THREAD_RET_TYPE void __cdecl
# endif
#else /* !USE_RTL_THREAD_API */
# define THREAD_RET_TYPE DWORD WINAPI
#endif /* !USE_RTL_THREAD_API */
typedef
THREAD_RET_TYPE thread_func_t(
void
*);
START_EXTERN_C
#if defined(PERLDLL) && defined(USE_DECLSPEC_THREAD)
extern
__declspec
(
thread
)
void
*PL_current_context;
#define PERL_SET_CONTEXT(t) (PL_current_context = t)
#define PERL_GET_CONTEXT PL_current_context
#else
#define PERL_GET_CONTEXT Perl_get_context()
#define PERL_SET_CONTEXT(t) Perl_set_context(t)
#endif
END_EXTERN_C
#define INIT_THREADS NOOP
#define ALLOC_THREAD_KEY \
STMT_START { \
if
((PL_thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) { \
PerlIO_printf(PerlIO_stderr(),
"panic: TlsAlloc"
); \
exit
(1); \
} \
} STMT_END
#define FREE_THREAD_KEY \
STMT_START { \
TlsFree(PL_thr_key); \
} STMT_END
#define PTHREAD_ATFORK(prepare,parent,child) NOOP
#if defined(USE_RTL_THREAD_API) && !defined(_MSC_VER)
#define JOIN(t, avp) \
STMT_START { \
if
((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
|| (GetExitCodeThread((t)->self,(
LPDWORD
)(avp)) == 0) \
|| (CloseHandle((t)->self) == 0)) \
Perl_croak_nocontext(
"panic: JOIN"
); \
*avp = (AV *)((t)->i.retv); \
} STMT_END
#else /* !USE_RTL_THREAD_API || _MSC_VER */
#define JOIN(t, avp) \
STMT_START { \
if
((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
|| (GetExitCodeThread((t)->self,(
LPDWORD
)(avp)) == 0) \
|| (CloseHandle((t)->self) == 0)) \
Perl_croak_nocontext(
"panic: JOIN"
); \
} STMT_END
#endif /* !USE_RTL_THREAD_API || _MSC_VER */
#define YIELD Sleep(0)
#endif /* _WIN32THREAD_H */