This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Prepare Module::CoreList for 5.33.6
[perl5.git] / win32 / win32thread.h
CommitLineData
d55594ae
GS
1#ifndef _WIN32THREAD_H
2#define _WIN32THREAD_H
4f63d024 3
7766f137 4#include "win32.h"
4f63d024 5
d55594ae 6typedef struct win32_cond { LONG waiters; HANDLE sem; } perl_cond;
ea0efc06 7typedef DWORD perl_key;
51dd5992 8typedef HANDLE perl_os_thread;
ea0efc06 9
4b026b9e
GS
10#ifndef DONT_USE_CRITICAL_SECTION
11
12/* Critical Sections used instead of mutexes: lightweight,
ea0efc06 13 * but can't be communicated to child processes, and can't get
4b026b9e 14 * HANDLE to it for use elsewhere.
ea0efc06 15 */
d55594ae 16typedef CRITICAL_SECTION perl_mutex;
ea0efc06
MB
17#define MUTEX_INIT(m) InitializeCriticalSection(m)
18#define MUTEX_LOCK(m) EnterCriticalSection(m)
19#define MUTEX_UNLOCK(m) LeaveCriticalSection(m)
20#define MUTEX_DESTROY(m) DeleteCriticalSection(m)
4b026b9e 21
d55594ae 22#else
ea0efc06 23
4b026b9e 24typedef HANDLE perl_mutex;
ba869deb 25# define MUTEX_INIT(m) \
ea0efc06
MB
26 STMT_START { \
27 if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) \
efc57feb 28 Perl_croak_nocontext("panic: MUTEX_INIT"); \
ea0efc06 29 } STMT_END
ba869deb
GS
30
31# define MUTEX_LOCK(m) \
ea0efc06
MB
32 STMT_START { \
33 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
4f63d024
GS
34 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
35 } STMT_END
ba869deb 36
efc57feb 37# define MUTEX_UNLOCK(m) \
4f63d024
GS
38 STMT_START { \
39 if (ReleaseMutex(*(m)) == 0) \
40 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
ea0efc06 41 } STMT_END
ba869deb
GS
42
43# define MUTEX_DESTROY(m) \
ea0efc06
MB
44 STMT_START { \
45 if (CloseHandle(*(m)) == 0) \
efc57feb 46 Perl_croak_nocontext("panic: MUTEX_DESTROY"); \
ea0efc06
MB
47 } STMT_END
48
d55594ae
GS
49#endif
50
51/* These macros assume that the mutex associated with the condition
52 * will always be held before COND_{SIGNAL,BROADCAST,WAIT,DESTROY},
53 * so there's no separate mutex protecting access to (c)->waiters
54 */
ea0efc06 55#define COND_INIT(c) \
4b026b9e
GS
56 STMT_START { \
57 (c)->waiters = 0; \
f4257e4d 58 (c)->sem = Win_CreateSemaphore(NULL,0,LONG_MAX,NULL); \
4b026b9e 59 if ((c)->sem == NULL) \
efc57feb 60 Perl_croak_nocontext("panic: COND_INIT (%ld)",GetLastError()); \
ea0efc06 61 } STMT_END
d55594ae 62
ea0efc06 63#define COND_SIGNAL(c) \
4b026b9e
GS
64 STMT_START { \
65 if ((c)->waiters > 0 && \
66 ReleaseSemaphore((c)->sem,1,NULL) == 0) \
efc57feb 67 Perl_croak_nocontext("panic: COND_SIGNAL (%ld)",GetLastError()); \
ea0efc06 68 } STMT_END
d55594ae 69
ea0efc06 70#define COND_BROADCAST(c) \
4b026b9e
GS
71 STMT_START { \
72 if ((c)->waiters > 0 && \
73 ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0) \
efc57feb 74 Perl_croak_nocontext("panic: COND_BROADCAST (%ld)",GetLastError());\
ea0efc06 75 } STMT_END
d55594ae 76
ea0efc06 77#define COND_WAIT(c, m) \
4b026b9e
GS
78 STMT_START { \
79 (c)->waiters++; \
80 MUTEX_UNLOCK(m); \
81 /* Note that there's no race here, since a \
82 * COND_BROADCAST() on another thread will have seen the\
83 * right number of waiters (i.e. including this one) */ \
84 if (WaitForSingleObject((c)->sem,INFINITE)==WAIT_FAILED)\
efc57feb 85 Perl_croak_nocontext("panic: COND_WAIT (%ld)",GetLastError()); \
4b026b9e
GS
86 /* XXX there may be an inconsequential race here */ \
87 MUTEX_LOCK(m); \
88 (c)->waiters--; \
ea0efc06 89 } STMT_END
d55594ae 90
ea0efc06 91#define COND_DESTROY(c) \
4b026b9e
GS
92 STMT_START { \
93 (c)->waiters = 0; \
94 if (CloseHandle((c)->sem) == 0) \
efc57feb 95 Perl_croak_nocontext("panic: COND_DESTROY (%ld)",GetLastError()); \
ea0efc06
MB
96 } STMT_END
97
98#define DETACH(t) \
99 STMT_START { \
46930d8f 100 if (CloseHandle((t)->self) == 0) { \
ea0efc06 101 MUTEX_UNLOCK(&(t)->mutex); \
efc57feb 102 Perl_croak_nocontext("panic: DETACH"); \
ea0efc06
MB
103 } \
104 } STMT_END
105
9811a7d7 106
2d7a9237
GS
107/* XXX Docs mention that the RTL versions of thread creation routines
108 * should be used, but that advice only seems applicable when the RTL
378eeda7
SH
109 * is not in a DLL. RTL DLLs seem to do all of the init/deinit required
110 * upon DLL_THREAD_ATTACH/DETACH. So we seem to be completely safe using
111 * straight Win32 API calls, rather than the much braindamaged RTL calls.
2d7a9237
GS
112 *
113 * _beginthread() in the RTLs call CloseHandle() just after the thread
114 * function returns, which means: 1) we have a race on our hands
115 * 2) it is impossible to implement join() semantics.
116 *
117 * IOW, do *NOT* turn on USE_RTL_THREAD_API! It is here
118 * for experimental purposes only. GSAR 98-01-02
119 */
120#ifdef USE_RTL_THREAD_API
121# include <process.h>
378eeda7 122# if defined (_MSC_VER)
2d7a9237 123# define THREAD_RET_TYPE unsigned __stdcall
2d7a9237
GS
124# else
125 /* CRTDLL.DLL doesn't allow a return value from thread function! */
126# define THREAD_RET_TYPE void __cdecl
2d7a9237
GS
127# endif
128#else /* !USE_RTL_THREAD_API */
129# define THREAD_RET_TYPE DWORD WINAPI
2d7a9237 130#endif /* !USE_RTL_THREAD_API */
ea0efc06 131
d55594ae
GS
132typedef THREAD_RET_TYPE thread_func_t(void *);
133
0fefa03b 134
d55594ae 135START_EXTERN_C
0fefa03b 136
378eeda7 137#if defined(PERLDLL) && defined(USE_DECLSPEC_THREAD)
ba869deb
GS
138extern __declspec(thread) void *PL_current_context;
139#define PERL_SET_CONTEXT(t) (PL_current_context = t)
140#define PERL_GET_CONTEXT PL_current_context
0fefa03b 141#else
ba869deb
GS
142#define PERL_GET_CONTEXT Perl_get_context()
143#define PERL_SET_CONTEXT(t) Perl_set_context(t)
0fefa03b
NIS
144#endif
145
d55594ae
GS
146END_EXTERN_C
147
ba869deb
GS
148#define INIT_THREADS NOOP
149#define ALLOC_THREAD_KEY \
150 STMT_START { \
c44d3fdb 151 if ((PL_thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) { \
adb71456 152 PerlIO_printf(PerlIO_stderr(),"panic: TlsAlloc"); \
c44d3fdb
GS
153 exit(1); \
154 } \
ba869deb 155 } STMT_END
ea0efc06 156
e1b5da64
GS
157#define FREE_THREAD_KEY \
158 STMT_START { \
159 TlsFree(PL_thr_key); \
160 } STMT_END
161
50dd6e57
GS
162#define PTHREAD_ATFORK(prepare,parent,child) NOOP
163
2d7a9237
GS
164#if defined(USE_RTL_THREAD_API) && !defined(_MSC_VER)
165#define JOIN(t, avp) \
166 STMT_START { \
167 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
8e11982f
GS
168 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
169 || (CloseHandle((t)->self) == 0)) \
efc57feb 170 Perl_croak_nocontext("panic: JOIN"); \
2d7a9237
GS
171 *avp = (AV *)((t)->i.retv); \
172 } STMT_END
173#else /* !USE_RTL_THREAD_API || _MSC_VER */
ea0efc06
MB
174#define JOIN(t, avp) \
175 STMT_START { \
46930d8f 176 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
8e11982f
GS
177 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
178 || (CloseHandle((t)->self) == 0)) \
efc57feb 179 Perl_croak_nocontext("panic: JOIN"); \
ea0efc06 180 } STMT_END
2d7a9237 181#endif /* !USE_RTL_THREAD_API || _MSC_VER */
ea0efc06 182
ea0efc06 183#define YIELD Sleep(0)
d55594ae
GS
184
185#endif /* _WIN32THREAD_H */
0fefa03b 186