6 typedef struct win32_cond { LONG waiters; HANDLE sem; } perl_cond;
7 typedef DWORD perl_key;
8 typedef HANDLE perl_os_thread;
10 #ifndef DONT_USE_CRITICAL_SECTION
12 /* Critical Sections used instead of mutexes: lightweight,
13 * but can't be communicated to child processes, and can't get
14 * HANDLE to it for use elsewhere.
16 typedef CRITICAL_SECTION perl_mutex;
17 #define MUTEX_INIT(m) InitializeCriticalSection(m)
18 #define MUTEX_LOCK(m) EnterCriticalSection(m)
19 #define MUTEX_UNLOCK(m) LeaveCriticalSection(m)
20 #define MUTEX_LOCK_NOCONTEXT(m) EnterCriticalSection(m)
21 #define MUTEX_UNLOCK_NOCONTEXT(m) LeaveCriticalSection(m)
22 #define MUTEX_DESTROY(m) DeleteCriticalSection(m)
26 typedef HANDLE perl_mutex;
27 #define MUTEX_INIT(m) \
29 if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) \
30 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
32 #define MUTEX_LOCK(m) \
34 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
35 Perl_croak(aTHX_ "panic: MUTEX_LOCK"); \
37 #define MUTEX_UNLOCK(m) \
39 if (ReleaseMutex(*(m)) == 0) \
40 Perl_croak(aTHX_ "panic: MUTEX_UNLOCK"); \
42 #define MUTEX_LOCK_NOCONTEXT(m) \
44 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
45 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
47 #define MUTEX_UNLOCK_NOCONTEXT(m) \
49 if (ReleaseMutex(*(m)) == 0) \
50 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
52 #define MUTEX_DESTROY(m) \
54 if (CloseHandle(*(m)) == 0) \
55 Perl_croak(aTHX_ "panic: MUTEX_DESTROY"); \
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
64 #define COND_INIT(c) \
67 (c)->sem = CreateSemaphore(NULL,0,LONG_MAX,NULL); \
68 if ((c)->sem == NULL) \
69 Perl_croak(aTHX_ "panic: COND_INIT (%ld)",GetLastError()); \
72 #define COND_SIGNAL(c) \
74 if ((c)->waiters > 0 && \
75 ReleaseSemaphore((c)->sem,1,NULL) == 0) \
76 Perl_croak(aTHX_ "panic: COND_SIGNAL (%ld)",GetLastError()); \
79 #define COND_BROADCAST(c) \
81 if ((c)->waiters > 0 && \
82 ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0) \
83 Perl_croak(aTHX_ "panic: COND_BROADCAST (%ld)",GetLastError());\
86 #define COND_WAIT(c, 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)\
94 Perl_croak(aTHX_ "panic: COND_WAIT (%ld)",GetLastError()); \
95 /* XXX there may be an inconsequential race here */ \
100 #define COND_DESTROY(c) \
103 if (CloseHandle((c)->sem) == 0) \
104 Perl_croak(aTHX_ "panic: COND_DESTROY (%ld)",GetLastError()); \
109 if (CloseHandle((t)->self) == 0) { \
110 MUTEX_UNLOCK(&(t)->mutex); \
111 Perl_croak(aTHX_ "panic: DETACH"); \
116 #define THREAD_CREATE(t, f) Perl_thread_create(t, f)
117 #define THREAD_POST_CREATE(t) NOOP
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.
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.
130 * IOW, do *NOT* turn on USE_RTL_THREAD_API! It is here
131 * for experimental purposes only. GSAR 98-01-02
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))
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)))
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 */
152 typedef THREAD_RET_TYPE thread_func_t(void *);
157 #if defined(PERLDLL) && defined(USE_DECLSPEC_THREAD) && (!defined(__BORLANDC__) || defined(_DLL))
158 extern __declspec(thread) struct perl_thread *Perl_current_thread;
159 #define SET_THR(t) (Perl_current_thread = t)
160 #define THR Perl_current_thread
162 #define THR Perl_getTHR()
163 #define SET_THR(t) Perl_setTHR(t)
167 void Perl_alloc_thread_key (void);
168 int Perl_thread_create (struct perl_thread *thr, thread_func_t *fn);
169 void Perl_set_thread_self (struct perl_thread *thr);
170 struct perl_thread *Perl_getTHR (void);
171 void Perl_setTHR (struct perl_thread *t);
172 void Perl_init_thread_intern (struct perl_thread *t);
176 #define INIT_THREADS NOOP
177 #define ALLOC_THREAD_KEY Perl_alloc_thread_key()
178 #define SET_THREAD_SELF(thr) Perl_set_thread_self(thr)
180 #if defined(USE_RTL_THREAD_API) && !defined(_MSC_VER)
181 #define JOIN(t, avp) \
183 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
184 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
185 || (CloseHandle((t)->self) == 0)) \
186 Perl_croak(aTHX_ "panic: JOIN"); \
187 *avp = (AV *)((t)->i.retv); \
189 #else /* !USE_RTL_THREAD_API || _MSC_VER */
190 #define JOIN(t, avp) \
192 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
193 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
194 || (CloseHandle((t)->self) == 0)) \
195 Perl_croak(aTHX_ "panic: JOIN"); \
197 #endif /* !USE_RTL_THREAD_API || _MSC_VER */
199 #define YIELD Sleep(0)
201 #endif /* _WIN32THREAD_H */