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