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"); \
33 # define MUTEX_LOCK(m) \
35 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
36 Perl_croak(aTHX_ "panic: MUTEX_LOCK"); \
39 # define MUTEX_UNLOCK(m) \
41 if (ReleaseMutex(*(m)) == 0) \
42 Perl_croak(aTHX_ "panic: MUTEX_UNLOCK"); \
45 # define MUTEX_LOCK_NOCONTEXT(m) \
47 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
48 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
51 # define MUTEX_UNLOCK_NOCONTEXT(m) \
53 if (ReleaseMutex(*(m)) == 0) \
54 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
57 # define MUTEX_DESTROY(m) \
59 if (CloseHandle(*(m)) == 0) \
60 Perl_croak(aTHX_ "panic: MUTEX_DESTROY"); \
65 /* These macros assume that the mutex associated with the condition
66 * will always be held before COND_{SIGNAL,BROADCAST,WAIT,DESTROY},
67 * so there's no separate mutex protecting access to (c)->waiters
69 #define COND_INIT(c) \
72 (c)->sem = CreateSemaphore(NULL,0,LONG_MAX,NULL); \
73 if ((c)->sem == NULL) \
74 Perl_croak(aTHX_ "panic: COND_INIT (%ld)",GetLastError()); \
77 #define COND_SIGNAL(c) \
79 if ((c)->waiters > 0 && \
80 ReleaseSemaphore((c)->sem,1,NULL) == 0) \
81 Perl_croak(aTHX_ "panic: COND_SIGNAL (%ld)",GetLastError()); \
84 #define COND_BROADCAST(c) \
86 if ((c)->waiters > 0 && \
87 ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0) \
88 Perl_croak(aTHX_ "panic: COND_BROADCAST (%ld)",GetLastError());\
91 #define COND_WAIT(c, m) \
95 /* Note that there's no race here, since a \
96 * COND_BROADCAST() on another thread will have seen the\
97 * right number of waiters (i.e. including this one) */ \
98 if (WaitForSingleObject((c)->sem,INFINITE)==WAIT_FAILED)\
99 Perl_croak(aTHX_ "panic: COND_WAIT (%ld)",GetLastError()); \
100 /* XXX there may be an inconsequential race here */ \
105 #define COND_DESTROY(c) \
108 if (CloseHandle((c)->sem) == 0) \
109 Perl_croak(aTHX_ "panic: COND_DESTROY (%ld)",GetLastError()); \
114 if (CloseHandle((t)->self) == 0) { \
115 MUTEX_UNLOCK(&(t)->mutex); \
116 Perl_croak(aTHX_ "panic: DETACH"); \
121 #define THREAD_CREATE(t, f) Perl_thread_create(t, f)
122 #define THREAD_POST_CREATE(t) NOOP
124 /* XXX Docs mention that the RTL versions of thread creation routines
125 * should be used, but that advice only seems applicable when the RTL
126 * is not in a DLL. RTL DLLs in both Borland and VC seem to do all of
127 * the init/deinit required upon DLL_THREAD_ATTACH/DETACH. So we seem
128 * to be completely safe using straight Win32 API calls, rather than
129 * the much braindamaged RTL calls.
131 * _beginthread() in the RTLs call CloseHandle() just after the thread
132 * function returns, which means: 1) we have a race on our hands
133 * 2) it is impossible to implement join() semantics.
135 * IOW, do *NOT* turn on USE_RTL_THREAD_API! It is here
136 * for experimental purposes only. GSAR 98-01-02
138 #ifdef USE_RTL_THREAD_API
139 # include <process.h>
140 # if defined(__BORLANDC__)
141 /* Borland RTL doesn't allow a return value from thread function! */
142 # define THREAD_RET_TYPE void _USERENTRY
143 # define THREAD_RET_CAST(p) ((void)(thr->i.retv = (void *)(p)))
144 # elif defined (_MSC_VER)
145 # define THREAD_RET_TYPE unsigned __stdcall
146 # define THREAD_RET_CAST(p) ((unsigned)(p))
148 /* CRTDLL.DLL doesn't allow a return value from thread function! */
149 # define THREAD_RET_TYPE void __cdecl
150 # define THREAD_RET_CAST(p) ((void)(thr->i.retv = (void *)(p)))
152 #else /* !USE_RTL_THREAD_API */
153 # define THREAD_RET_TYPE DWORD WINAPI
154 # define THREAD_RET_CAST(p) ((DWORD)(p))
155 #endif /* !USE_RTL_THREAD_API */
157 typedef THREAD_RET_TYPE thread_func_t(void *);
162 #if defined(PERLDLL) && defined(USE_DECLSPEC_THREAD) && (!defined(__BORLANDC__) || defined(_DLL))
163 extern __declspec(thread) void *PL_current_context;
164 #define PERL_SET_CONTEXT(t) (PL_current_context = t)
165 #define PERL_GET_CONTEXT PL_current_context
167 #define PERL_GET_CONTEXT Perl_get_context()
168 #define PERL_SET_CONTEXT(t) Perl_set_context(t)
171 #if defined(USE_THREADS)
173 int Perl_thread_create (struct perl_thread *thr, thread_func_t *fn);
174 void Perl_set_thread_self (struct perl_thread *thr);
175 void Perl_init_thread_intern (struct perl_thread *t);
177 #define SET_THREAD_SELF(thr) Perl_set_thread_self(thr)
179 #endif /* USE_THREADS */
183 #define INIT_THREADS NOOP
184 #define ALLOC_THREAD_KEY \
186 if ((PL_thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) { \
187 fprintf(stderr,"panic: TlsAlloc"); \
192 #if defined(USE_RTL_THREAD_API) && !defined(_MSC_VER)
193 #define JOIN(t, avp) \
195 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
196 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
197 || (CloseHandle((t)->self) == 0)) \
198 Perl_croak(aTHX_ "panic: JOIN"); \
199 *avp = (AV *)((t)->i.retv); \
201 #else /* !USE_RTL_THREAD_API || _MSC_VER */
202 #define JOIN(t, avp) \
204 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
205 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0) \
206 || (CloseHandle((t)->self) == 0)) \
207 Perl_croak(aTHX_ "panic: JOIN"); \
209 #endif /* !USE_RTL_THREAD_API || _MSC_VER */
211 #define YIELD Sleep(0)
213 #endif /* _WIN32THREAD_H */