4 #define WIN32_LEAN_AND_MEAN
7 typedef struct win32_cond { LONG waiters; HANDLE sem; } perl_cond;
8 typedef DWORD perl_key;
9 typedef HANDLE perl_os_thread;
11 #ifndef DONT_USE_CRITICAL_SECTION
13 /* Critical Sections used instead of mutexes: lightweight,
14 * but can't be communicated to child processes, and can't get
15 * HANDLE to it for use elsewhere.
17 typedef CRITICAL_SECTION perl_mutex;
18 #define MUTEX_INIT(m) InitializeCriticalSection(m)
19 #define MUTEX_LOCK(m) EnterCriticalSection(m)
20 #define MUTEX_UNLOCK(m) LeaveCriticalSection(m)
21 #define MUTEX_LOCK_NOCONTEXT(m) EnterCriticalSection(m)
22 #define MUTEX_UNLOCK_NOCONTEXT(m) LeaveCriticalSection(m)
23 #define MUTEX_DESTROY(m) DeleteCriticalSection(m)
27 typedef HANDLE perl_mutex;
28 #define MUTEX_INIT(m) \
30 if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) \
31 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
33 #define MUTEX_LOCK(m) \
35 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
36 Perl_croak(aTHX_ "panic: MUTEX_LOCK"); \
38 #define MUTEX_UNLOCK(m) \
40 if (ReleaseMutex(*(m)) == 0) \
41 Perl_croak(aTHX_ "panic: MUTEX_UNLOCK"); \
43 #define MUTEX_LOCK_NOCONTEXT(m) \
45 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
46 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
48 #define MUTEX_UNLOCK_NOCONTEXT(m) \
50 if (ReleaseMutex(*(m)) == 0) \
51 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
53 #define MUTEX_DESTROY(m) \
55 if (CloseHandle(*(m)) == 0) \
56 Perl_croak(aTHX_ "panic: MUTEX_DESTROY"); \
61 /* These macros assume that the mutex associated with the condition
62 * will always be held before COND_{SIGNAL,BROADCAST,WAIT,DESTROY},
63 * so there's no separate mutex protecting access to (c)->waiters
65 #define COND_INIT(c) \
68 (c)->sem = CreateSemaphore(NULL,0,LONG_MAX,NULL); \
69 if ((c)->sem == NULL) \
70 Perl_croak(aTHX_ "panic: COND_INIT (%ld)",GetLastError()); \
73 #define COND_SIGNAL(c) \
75 if ((c)->waiters > 0 && \
76 ReleaseSemaphore((c)->sem,1,NULL) == 0) \
77 Perl_croak(aTHX_ "panic: COND_SIGNAL (%ld)",GetLastError()); \
80 #define COND_BROADCAST(c) \
82 if ((c)->waiters > 0 && \
83 ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0) \
84 Perl_croak(aTHX_ "panic: COND_BROADCAST (%ld)",GetLastError());\
87 #define COND_WAIT(c, m) \
91 /* Note that there's no race here, since a \
92 * COND_BROADCAST() on another thread will have seen the\
93 * right number of waiters (i.e. including this one) */ \
94 if (WaitForSingleObject((c)->sem,INFINITE)==WAIT_FAILED)\
95 Perl_croak(aTHX_ "panic: COND_WAIT (%ld)",GetLastError()); \
96 /* XXX there may be an inconsequential race here */ \
101 #define COND_DESTROY(c) \
104 if (CloseHandle((c)->sem) == 0) \
105 Perl_croak(aTHX_ "panic: COND_DESTROY (%ld)",GetLastError()); \
110 if (CloseHandle((t)->self) == 0) { \
111 MUTEX_UNLOCK(&(t)->mutex); \
112 Perl_croak(aTHX_ "panic: DETACH"); \
117 #define THREAD_CREATE(t, f) Perl_thread_create(t, f)
118 #define THREAD_POST_CREATE(t) NOOP
120 /* XXX Docs mention that the RTL versions of thread creation routines
121 * should be used, but that advice only seems applicable when the RTL
122 * is not in a DLL. RTL DLLs in both Borland and VC seem to do all of
123 * the init/deinit required upon DLL_THREAD_ATTACH/DETACH. So we seem
124 * to be completely safe using straight Win32 API calls, rather than
125 * the much braindamaged RTL calls.
127 * _beginthread() in the RTLs call CloseHandle() just after the thread
128 * function returns, which means: 1) we have a race on our hands
129 * 2) it is impossible to implement join() semantics.
131 * IOW, do *NOT* turn on USE_RTL_THREAD_API! It is here
132 * for experimental purposes only. GSAR 98-01-02
134 #ifdef USE_RTL_THREAD_API
135 # include <process.h>
136 # if defined(__BORLANDC__)
137 /* Borland RTL doesn't allow a return value from thread function! */
138 # define THREAD_RET_TYPE void _USERENTRY
139 # define THREAD_RET_CAST(p) ((void)(thr->i.retv = (void *)(p)))
140 # elif defined (_MSC_VER)
141 # define THREAD_RET_TYPE unsigned __stdcall
142 # define THREAD_RET_CAST(p) ((unsigned)(p))
144 /* CRTDLL.DLL doesn't allow a return value from thread function! */
145 # define THREAD_RET_TYPE void __cdecl
146 # define THREAD_RET_CAST(p) ((void)(thr->i.retv = (void *)(p)))
148 #else /* !USE_RTL_THREAD_API */
149 # define THREAD_RET_TYPE DWORD WINAPI
150 # define THREAD_RET_CAST(p) ((DWORD)(p))
151 #endif /* !USE_RTL_THREAD_API */
153 typedef THREAD_RET_TYPE thread_func_t(void *);
158 #if defined(PERLDLL) && defined(USE_DECLSPEC_THREAD) && (!defined(__BORLANDC__) || defined(_DLL))
159 extern __declspec(thread) struct perl_thread *Perl_current_thread;
160 #define SET_THR(t) (Perl_current_thread = t)
161 #define THR Perl_current_thread
163 #define THR Perl_getTHR()
164 #define SET_THR(t) Perl_setTHR(t)
168 void Perl_alloc_thread_key (void);
169 int Perl_thread_create (struct perl_thread *thr, thread_func_t *fn);
170 void Perl_set_thread_self (struct perl_thread *thr);
171 struct perl_thread *Perl_getTHR (void);
172 void Perl_setTHR (struct perl_thread *t);
173 void Perl_init_thread_intern (struct perl_thread *t);
177 #define INIT_THREADS NOOP
178 #define ALLOC_THREAD_KEY Perl_alloc_thread_key()
179 #define SET_THREAD_SELF(thr) Perl_set_thread_self(thr)
181 #if defined(USE_RTL_THREAD_API) && !defined(_MSC_VER)
182 #define JOIN(t, avp) \
184 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
185 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
186 || (CloseHandle((t)->self) == 0)) \
187 Perl_croak(aTHX_ "panic: JOIN"); \
188 *avp = (AV *)((t)->i.retv); \
190 #else /* !USE_RTL_THREAD_API || _MSC_VER */
191 #define JOIN(t, avp) \
193 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
194 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
195 || (CloseHandle((t)->self) == 0)) \
196 Perl_croak(aTHX_ "panic: JOIN"); \
198 #endif /* !USE_RTL_THREAD_API || _MSC_VER */
200 #define YIELD Sleep(0)
202 #endif /* _WIN32THREAD_H */