This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
various shenanigans in change#5342
[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)
4f63d024
GS
20#define MUTEX_LOCK_NOCONTEXT(m) EnterCriticalSection(m)
21#define MUTEX_UNLOCK_NOCONTEXT(m) LeaveCriticalSection(m)
ea0efc06 22#define MUTEX_DESTROY(m) DeleteCriticalSection(m)
4b026b9e 23
d55594ae 24#else
ea0efc06 25
4b026b9e 26typedef HANDLE perl_mutex;
ba869deb 27# define MUTEX_INIT(m) \
ea0efc06
MB
28 STMT_START { \
29 if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) \
ba869deb 30 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
ea0efc06 31 } STMT_END
ba869deb
GS
32
33# define MUTEX_LOCK(m) \
ea0efc06
MB
34 STMT_START { \
35 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
4f63d024 36 Perl_croak(aTHX_ "panic: MUTEX_LOCK"); \
ea0efc06 37 } STMT_END
ba869deb
GS
38
39# define MUTEX_UNLOCK(m) \
ea0efc06
MB
40 STMT_START { \
41 if (ReleaseMutex(*(m)) == 0) \
4f63d024
GS
42 Perl_croak(aTHX_ "panic: MUTEX_UNLOCK"); \
43 } STMT_END
ba869deb
GS
44
45# define MUTEX_LOCK_NOCONTEXT(m) \
4f63d024
GS
46 STMT_START { \
47 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
48 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
49 } STMT_END
ba869deb
GS
50
51# define MUTEX_UNLOCK_NOCONTEXT(m) \
4f63d024
GS
52 STMT_START { \
53 if (ReleaseMutex(*(m)) == 0) \
54 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
ea0efc06 55 } STMT_END
ba869deb
GS
56
57# define MUTEX_DESTROY(m) \
ea0efc06
MB
58 STMT_START { \
59 if (CloseHandle(*(m)) == 0) \
4f63d024 60 Perl_croak(aTHX_ "panic: MUTEX_DESTROY"); \
ea0efc06
MB
61 } STMT_END
62
d55594ae
GS
63#endif
64
65/* These macros assume that the mutex associated with the condition
66 * will always be held before COND_{SIGNAL,BROADCAST,WAIT,DESTROY},
67 * so there's no separate mutex protecting access to (c)->waiters
68 */
ea0efc06 69#define COND_INIT(c) \
4b026b9e
GS
70 STMT_START { \
71 (c)->waiters = 0; \
72 (c)->sem = CreateSemaphore(NULL,0,LONG_MAX,NULL); \
73 if ((c)->sem == NULL) \
4f63d024 74 Perl_croak(aTHX_ "panic: COND_INIT (%ld)",GetLastError()); \
ea0efc06 75 } STMT_END
d55594ae 76
ea0efc06 77#define COND_SIGNAL(c) \
4b026b9e
GS
78 STMT_START { \
79 if ((c)->waiters > 0 && \
80 ReleaseSemaphore((c)->sem,1,NULL) == 0) \
4f63d024 81 Perl_croak(aTHX_ "panic: COND_SIGNAL (%ld)",GetLastError()); \
ea0efc06 82 } STMT_END
d55594ae 83
ea0efc06 84#define COND_BROADCAST(c) \
4b026b9e
GS
85 STMT_START { \
86 if ((c)->waiters > 0 && \
87 ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0) \
4f63d024 88 Perl_croak(aTHX_ "panic: COND_BROADCAST (%ld)",GetLastError());\
ea0efc06 89 } STMT_END
d55594ae 90
ea0efc06 91#define COND_WAIT(c, m) \
4b026b9e
GS
92 STMT_START { \
93 (c)->waiters++; \
94 MUTEX_UNLOCK(m); \
95 /* Note that there's no race here, since a \
96 * COND_BROADCAST() on another thread will have seen the\
97 * right number of waiters (i.e. including this one) */ \
98 if (WaitForSingleObject((c)->sem,INFINITE)==WAIT_FAILED)\
4f63d024 99 Perl_croak(aTHX_ "panic: COND_WAIT (%ld)",GetLastError()); \
4b026b9e
GS
100 /* XXX there may be an inconsequential race here */ \
101 MUTEX_LOCK(m); \
102 (c)->waiters--; \
ea0efc06 103 } STMT_END
d55594ae 104
ea0efc06 105#define COND_DESTROY(c) \
4b026b9e
GS
106 STMT_START { \
107 (c)->waiters = 0; \
108 if (CloseHandle((c)->sem) == 0) \
4f63d024 109 Perl_croak(aTHX_ "panic: COND_DESTROY (%ld)",GetLastError()); \
ea0efc06
MB
110 } STMT_END
111
112#define DETACH(t) \
113 STMT_START { \
46930d8f 114 if (CloseHandle((t)->self) == 0) { \
ea0efc06 115 MUTEX_UNLOCK(&(t)->mutex); \
4f63d024 116 Perl_croak(aTHX_ "panic: DETACH"); \
ea0efc06
MB
117 } \
118 } STMT_END
119
9811a7d7 120
d55594ae
GS
121#define THREAD_CREATE(t, f) Perl_thread_create(t, f)
122#define THREAD_POST_CREATE(t) NOOP
2d7a9237
GS
123
124/* XXX Docs mention that the RTL versions of thread creation routines
125 * should be used, but that advice only seems applicable when the RTL
126 * is not in a DLL. RTL DLLs in both Borland and VC seem to do all of
127 * the init/deinit required upon DLL_THREAD_ATTACH/DETACH. So we seem
128 * to be completely safe using straight Win32 API calls, rather than
129 * the much braindamaged RTL calls.
130 *
131 * _beginthread() in the RTLs call CloseHandle() just after the thread
132 * function returns, which means: 1) we have a race on our hands
133 * 2) it is impossible to implement join() semantics.
134 *
135 * IOW, do *NOT* turn on USE_RTL_THREAD_API! It is here
136 * for experimental purposes only. GSAR 98-01-02
137 */
138#ifdef USE_RTL_THREAD_API
139# include <process.h>
140# if defined(__BORLANDC__)
141 /* Borland RTL doesn't allow a return value from thread function! */
142# define THREAD_RET_TYPE void _USERENTRY
143# define THREAD_RET_CAST(p) ((void)(thr->i.retv = (void *)(p)))
144# elif defined (_MSC_VER)
145# define THREAD_RET_TYPE unsigned __stdcall
146# define THREAD_RET_CAST(p) ((unsigned)(p))
147# else
148 /* CRTDLL.DLL doesn't allow a return value from thread function! */
149# define THREAD_RET_TYPE void __cdecl
150# define THREAD_RET_CAST(p) ((void)(thr->i.retv = (void *)(p)))
151# endif
152#else /* !USE_RTL_THREAD_API */
153# define THREAD_RET_TYPE DWORD WINAPI
154# define THREAD_RET_CAST(p) ((DWORD)(p))
155#endif /* !USE_RTL_THREAD_API */
ea0efc06 156
d55594ae
GS
157typedef THREAD_RET_TYPE thread_func_t(void *);
158
0fefa03b 159
d55594ae 160START_EXTERN_C
0fefa03b 161
c69f112c 162#if defined(PERLDLL) && defined(USE_DECLSPEC_THREAD) && (!defined(__BORLANDC__) || defined(_DLL))
ba869deb
GS
163extern __declspec(thread) void *PL_current_context;
164#define PERL_SET_CONTEXT(t) (PL_current_context = t)
165#define PERL_GET_CONTEXT PL_current_context
0fefa03b 166#else
ba869deb
GS
167#define PERL_GET_CONTEXT Perl_get_context()
168#define PERL_SET_CONTEXT(t) Perl_set_context(t)
0fefa03b
NIS
169#endif
170
ba869deb
GS
171#if defined(USE_THREADS)
172struct perl_thread;
20ce7b12
GS
173int Perl_thread_create (struct perl_thread *thr, thread_func_t *fn);
174void Perl_set_thread_self (struct perl_thread *thr);
20ce7b12 175void Perl_init_thread_intern (struct perl_thread *t);
0fefa03b 176
ba869deb
GS
177#define SET_THREAD_SELF(thr) Perl_set_thread_self(thr)
178
179#endif /* USE_THREADS */
180
d55594ae
GS
181END_EXTERN_C
182
ba869deb
GS
183#define INIT_THREADS NOOP
184#define ALLOC_THREAD_KEY \
185 STMT_START { \
c44d3fdb
GS
186 if ((PL_thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) { \
187 fprintf(stderr,"panic: TlsAlloc"); \
188 exit(1); \
189 } \
ba869deb 190 } STMT_END
ea0efc06 191
2d7a9237
GS
192#if defined(USE_RTL_THREAD_API) && !defined(_MSC_VER)
193#define JOIN(t, avp) \
194 STMT_START { \
195 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
8e11982f
GS
196 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
197 || (CloseHandle((t)->self) == 0)) \
4f63d024 198 Perl_croak(aTHX_ "panic: JOIN"); \
2d7a9237
GS
199 *avp = (AV *)((t)->i.retv); \
200 } STMT_END
201#else /* !USE_RTL_THREAD_API || _MSC_VER */
ea0efc06
MB
202#define JOIN(t, avp) \
203 STMT_START { \
46930d8f 204 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
8e11982f
GS
205 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
206 || (CloseHandle((t)->self) == 0)) \
7766f137 207 Perl_croak(aTHX_ "panic: JOIN"); \
ea0efc06 208 } STMT_END
2d7a9237 209#endif /* !USE_RTL_THREAD_API || _MSC_VER */
ea0efc06 210
ea0efc06 211#define YIELD Sleep(0)
d55594ae
GS
212
213#endif /* _WIN32THREAD_H */
0fefa03b 214