This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Initial (untested) integration of mainline changes.
[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
MB
4typedef DWORD perl_key;
5typedef HANDLE perl_thread;
6
7/* XXX Critical Sections used instead of mutexes: lightweight,
8 * but can't be communicated to child processes, and can't get
9 * HANDLE to it for use elsewhere
10 */
d55594ae
GS
11
12#ifndef DONT_USE_CRITICAL_SECTION
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)
d55594ae
GS
18#else
19typedef HANDLE perl_mutex;
ea0efc06
MB
20
21#define MUTEX_INIT(m) \
22 STMT_START { \
23 if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) \
24 croak("panic: MUTEX_INIT"); \
25 } STMT_END
26#define MUTEX_LOCK(m) \
27 STMT_START { \
28 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
29 croak("panic: MUTEX_LOCK"); \
30 } STMT_END
31#define MUTEX_UNLOCK(m) \
32 STMT_START { \
33 if (ReleaseMutex(*(m)) == 0) \
34 croak("panic: MUTEX_UNLOCK"); \
35 } STMT_END
36#define MUTEX_DESTROY(m) \
37 STMT_START { \
38 if (CloseHandle(*(m)) == 0) \
39 croak("panic: MUTEX_DESTROY"); \
40 } STMT_END
41
d55594ae
GS
42#endif
43
44/* These macros assume that the mutex associated with the condition
45 * will always be held before COND_{SIGNAL,BROADCAST,WAIT,DESTROY},
46 * so there's no separate mutex protecting access to (c)->waiters
47 */
ea0efc06 48#define COND_INIT(c) \
d55594ae
GS
49 STMT_START { \
50 (c)->waiters = 0; \
51 (c)->sem = CreateSemaphore(NULL,0,LONG_MAX,NULL); \
52 if ((c)->sem == NULL) \
53 croak("panic: COND_INIT (%ld)",GetLastError()); \
ea0efc06 54 } STMT_END
d55594ae 55
ea0efc06 56#define COND_SIGNAL(c) \
d55594ae
GS
57 STMT_START { \
58 if (ReleaseSemaphore((c)->sem,1,NULL) == 0) \
59 croak("panic: COND_SIGNAL (%ld)",GetLastError()); \
ea0efc06 60 } STMT_END
d55594ae 61
ea0efc06 62#define COND_BROADCAST(c) \
d55594ae
GS
63 STMT_START { \
64 if ((c)->waiters > 0 && \
65 ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0) \
66 croak("panic: COND_BROADCAST (%ld)",GetLastError());\
ea0efc06 67 } STMT_END
d55594ae 68
ea0efc06 69#define COND_WAIT(c, m) \
d55594ae
GS
70 STMT_START { \
71 (c)->waiters++; \
72 MUTEX_UNLOCK(m); \
73 /* Note that there's no race here, since a \
74 * COND_BROADCAST() on another thread will have seen the\
75 * right number of waiters (i.e. including this one) */ \
76 if (WaitForSingleObject((c)->sem,INFINITE)==WAIT_FAILED)\
77 croak("panic: COND_WAIT (%ld)",GetLastError()); \
78 MUTEX_LOCK(m); \
79 (c)->waiters--; \
ea0efc06 80 } STMT_END
d55594ae 81
ea0efc06 82#define COND_DESTROY(c) \
d55594ae
GS
83 STMT_START { \
84 (c)->waiters = 0; \
85 if (CloseHandle((c)->sem) == 0) \
86 croak("panic: COND_DESTROY (%ld)",GetLastError()); \
ea0efc06
MB
87 } STMT_END
88
89#define DETACH(t) \
90 STMT_START { \
46930d8f 91 if (CloseHandle((t)->self) == 0) { \
ea0efc06
MB
92 MUTEX_UNLOCK(&(t)->mutex); \
93 croak("panic: DETACH"); \
94 } \
95 } STMT_END
96
97#define THR ((struct thread *) TlsGetValue(thr_key))
d55594ae
GS
98#define THREAD_CREATE(t, f) Perl_thread_create(t, f)
99#define THREAD_POST_CREATE(t) NOOP
100#define THREAD_RET_TYPE DWORD WINAPI
101#define THREAD_RET_CAST(p) ((DWORD)(p))
ea0efc06 102
d55594ae
GS
103typedef THREAD_RET_TYPE thread_func_t(void *);
104
105START_EXTERN_C
106void Perl_alloc_thread_key _((void));
107int Perl_thread_create _((struct thread *thr, thread_func_t *fn));
108void Perl_init_thread_intern _((struct thread *thr));
109END_EXTERN_C
110
111#define INIT_THREADS NOOP
112#define ALLOC_THREAD_KEY Perl_alloc_thread_key()
113#define INIT_THREAD_INTERN(thr) Perl_init_thread_intern(thr)
ea0efc06
MB
114
115#define JOIN(t, avp) \
116 STMT_START { \
46930d8f
MB
117 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
118 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0)) \
ea0efc06
MB
119 croak("panic: JOIN"); \
120 } STMT_END
121
122#define SET_THR(t) \
123 STMT_START { \
124 if (TlsSetValue(thr_key, (void *) (t)) == 0) \
125 croak("panic: TlsSetValue"); \
126 } STMT_END
127
ea0efc06 128#define YIELD Sleep(0)
d55594ae
GS
129
130#endif /* _WIN32THREAD_H */