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