win32thread.* not in MANIFEST which has muddled moving
[p5sagit/p5-mst-13.2.git] / win32 / win32thread.h
1 #ifndef _WIN32THREAD_H
2 #define _WIN32THREAD_H
3 typedef struct win32_cond { LONG waiters; HANDLE sem; } perl_cond;
4 typedef DWORD perl_key;
5 typedef 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  */
11
12 #ifndef DONT_USE_CRITICAL_SECTION
13 typedef CRITICAL_SECTION perl_mutex;
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)
18 #else
19 typedef HANDLE perl_mutex;
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
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  */
48 #define COND_INIT(c) \
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());     \
54     } STMT_END
55
56 #define COND_SIGNAL(c) \
57     STMT_START {                                                \
58         if (ReleaseSemaphore((c)->sem,1,NULL) == 0)             \
59             croak("panic: COND_SIGNAL (%ld)",GetLastError());   \
60     } STMT_END
61
62 #define COND_BROADCAST(c) \
63     STMT_START {                                                \
64         if ((c)->waiters > 0 &&                                 \
65             ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0)  \
66             croak("panic: COND_BROADCAST (%ld)",GetLastError());\
67     } STMT_END
68
69 #define COND_WAIT(c, m) \
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--;                                         \
80     } STMT_END
81
82 #define COND_DESTROY(c) \
83     STMT_START {                                                \
84         (c)->waiters = 0;                                       \
85         if (CloseHandle((c)->sem) == 0)                         \
86             croak("panic: COND_DESTROY (%ld)",GetLastError());  \
87     } STMT_END
88
89 #define DETACH(t) \
90     STMT_START {                                                \
91         if (CloseHandle((t)->self) == 0) {                      \
92             MUTEX_UNLOCK(&(t)->mutex);                          \
93             croak("panic: DETACH");                             \
94         }                                                       \
95     } STMT_END
96
97 #define THR ((struct thread *) TlsGetValue(thr_key))
98
99 #define HAVE_THREAD_INTERN
100 void init_thread_intern _((struct thread *thr));
101
102 #define JOIN(t, avp)                                                    \
103     STMT_START {                                                        \
104         if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED)    \
105              || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0))     \
106             croak("panic: JOIN");                                       \
107     } STMT_END
108
109 #define SET_THR(t)                                      \
110     STMT_START {                                        \
111         if (TlsSetValue(thr_key, (void *) (t)) == 0)    \
112             croak("panic: TlsSetValue");                \
113     } STMT_END
114
115 #define THREAD_CREATE(t, f)     Perl_thread_create(t, f)
116 #define THREAD_POST_CREATE(t)   NOOP
117 #define THREAD_RET_TYPE         DWORD WINAPI
118 #define THREAD_RET_CAST(p)      ((DWORD)(p))
119 #define YIELD                   Sleep(0)
120
121 typedef THREAD_RET_TYPE thread_func_t(void *);
122
123 int Perl_thread_create _((struct thread *thr, thread_func_t *fn));
124
125 #endif /* _WIN32THREAD_H */