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