This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Two more delays added to test suite to help *-solaris-thread.
[perl5.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 #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))
102
103 typedef THREAD_RET_TYPE thread_func_t(void *);
104
105 START_EXTERN_C
106 void Perl_alloc_thread_key _((void));
107 int Perl_thread_create _((struct thread *thr, thread_func_t *fn));
108 void Perl_init_thread_intern _((struct thread *thr));
109 END_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)
114
115 #define JOIN(t, avp)                                                    \
116     STMT_START {                                                        \
117         if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED)    \
118              || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0))     \
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
128 #define YIELD                   Sleep(0)
129
130 #endif /* _WIN32THREAD_H */