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
1#ifndef _WIN32THREAD_H
2#define _WIN32THREAD_H
3
4#include "win32.h"
5
6typedef struct win32_cond { LONG waiters; HANDLE sem; } perl_cond;
7typedef DWORD perl_key;
8typedef HANDLE perl_os_thread;
9
10#ifndef DONT_USE_CRITICAL_SECTION
11
12/* Critical Sections used instead of mutexes: lightweight,
13 * but can't be communicated to child processes, and can't get
14 * HANDLE to it for use elsewhere.
15 */
16typedef CRITICAL_SECTION perl_mutex;
17#define MUTEX_INIT(m) InitializeCriticalSection(m)
18#define MUTEX_LOCK(m) EnterCriticalSection(m)
19#define MUTEX_UNLOCK(m) LeaveCriticalSection(m)
20#define MUTEX_LOCK_NOCONTEXT(m) EnterCriticalSection(m)
21#define MUTEX_UNLOCK_NOCONTEXT(m) LeaveCriticalSection(m)
22#define MUTEX_DESTROY(m) DeleteCriticalSection(m)
23
24#else
25
26typedef HANDLE perl_mutex;
27# define MUTEX_INIT(m) \
28 STMT_START { \
29 if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) \
30 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
31 } STMT_END
32
33# define MUTEX_LOCK(m) \
34 STMT_START { \
35 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
36 Perl_croak(aTHX_ "panic: MUTEX_LOCK"); \
37 } STMT_END
38
39# define MUTEX_UNLOCK(m) \
40 STMT_START { \
41 if (ReleaseMutex(*(m)) == 0) \
42 Perl_croak(aTHX_ "panic: MUTEX_UNLOCK"); \
43 } STMT_END
44
45# define MUTEX_LOCK_NOCONTEXT(m) \
46 STMT_START { \
47 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
48 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
49 } STMT_END
50
51# define MUTEX_UNLOCK_NOCONTEXT(m) \
52 STMT_START { \
53 if (ReleaseMutex(*(m)) == 0) \
54 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
55 } STMT_END
56
57# define MUTEX_DESTROY(m) \
58 STMT_START { \
59 if (CloseHandle(*(m)) == 0) \
60 Perl_croak(aTHX_ "panic: MUTEX_DESTROY"); \
61 } STMT_END
62
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 */
69#define COND_INIT(c) \
70 STMT_START { \
71 (c)->waiters = 0; \
72 (c)->sem = CreateSemaphore(NULL,0,LONG_MAX,NULL); \
73 if ((c)->sem == NULL) \
74 Perl_croak(aTHX_ "panic: COND_INIT (%ld)",GetLastError()); \
75 } STMT_END
76
77#define COND_SIGNAL(c) \
78 STMT_START { \
79 if ((c)->waiters > 0 && \
80 ReleaseSemaphore((c)->sem,1,NULL) == 0) \
81 Perl_croak(aTHX_ "panic: COND_SIGNAL (%ld)",GetLastError()); \
82 } STMT_END
83
84#define COND_BROADCAST(c) \
85 STMT_START { \
86 if ((c)->waiters > 0 && \
87 ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0) \
88 Perl_croak(aTHX_ "panic: COND_BROADCAST (%ld)",GetLastError());\
89 } STMT_END
90
91#define COND_WAIT(c, m) \
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)\
99 Perl_croak(aTHX_ "panic: COND_WAIT (%ld)",GetLastError()); \
100 /* XXX there may be an inconsequential race here */ \
101 MUTEX_LOCK(m); \
102 (c)->waiters--; \
103 } STMT_END
104
105#define COND_DESTROY(c) \
106 STMT_START { \
107 (c)->waiters = 0; \
108 if (CloseHandle((c)->sem) == 0) \
109 Perl_croak(aTHX_ "panic: COND_DESTROY (%ld)",GetLastError()); \
110 } STMT_END
111
112#define DETACH(t) \
113 STMT_START { \
114 if (CloseHandle((t)->self) == 0) { \
115 MUTEX_UNLOCK(&(t)->mutex); \
116 Perl_croak(aTHX_ "panic: DETACH"); \
117 } \
118 } STMT_END
119
120
121#define THREAD_CREATE(t, f) Perl_thread_create(t, f)
122#define THREAD_POST_CREATE(t) NOOP
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 */
156
157typedef THREAD_RET_TYPE thread_func_t(void *);
158
159
160START_EXTERN_C
161
162#if defined(PERLDLL) && defined(USE_DECLSPEC_THREAD) && (!defined(__BORLANDC__) || defined(_DLL))
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
166#else
167#define PERL_GET_CONTEXT Perl_get_context()
168#define PERL_SET_CONTEXT(t) Perl_set_context(t)
169#endif
170
171#if defined(USE_THREADS)
172struct perl_thread;
173int Perl_thread_create (struct perl_thread *thr, thread_func_t *fn);
174void Perl_set_thread_self (struct perl_thread *thr);
175void Perl_init_thread_intern (struct perl_thread *t);
176
177#define SET_THREAD_SELF(thr) Perl_set_thread_self(thr)
178
179#endif /* USE_THREADS */
180
181END_EXTERN_C
182
183#define INIT_THREADS NOOP
184#define ALLOC_THREAD_KEY \
185 STMT_START { \
186 if ((PL_thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) { \
187 fprintf(stderr,"panic: TlsAlloc"); \
188 exit(1); \
189 } \
190 } STMT_END
191
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) \
196 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
197 || (CloseHandle((t)->self) == 0)) \
198 Perl_croak(aTHX_ "panic: JOIN"); \
199 *avp = (AV *)((t)->i.retv); \
200 } STMT_END
201#else /* !USE_RTL_THREAD_API || _MSC_VER */
202#define JOIN(t, avp) \
203 STMT_START { \
204 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
205 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
206 || (CloseHandle((t)->self) == 0)) \
207 Perl_croak(aTHX_ "panic: JOIN"); \
208 } STMT_END
209#endif /* !USE_RTL_THREAD_API || _MSC_VER */
210
211#define YIELD Sleep(0)
212
213#endif /* _WIN32THREAD_H */
214