3 typedef struct win32_cond { LONG waiters; HANDLE sem; } perl_cond;
4 typedef DWORD perl_key;
5 typedef HANDLE perl_thread;
7 #ifndef DONT_USE_CRITICAL_SECTION
9 /* Critical Sections used instead of mutexes: lightweight,
10 * but can't be communicated to child processes, and can't get
11 * HANDLE to it for use elsewhere.
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)
21 typedef HANDLE perl_mutex;
22 #define MUTEX_INIT(m) \
24 if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) \
25 croak("panic: MUTEX_INIT"); \
27 #define MUTEX_LOCK(m) \
29 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
30 croak("panic: MUTEX_LOCK"); \
32 #define MUTEX_UNLOCK(m) \
34 if (ReleaseMutex(*(m)) == 0) \
35 croak("panic: MUTEX_UNLOCK"); \
37 #define MUTEX_DESTROY(m) \
39 if (CloseHandle(*(m)) == 0) \
40 croak("panic: MUTEX_DESTROY"); \
45 /* These macros assume that the mutex associated with the condition
46 * will always be held before COND_{SIGNAL,BROADCAST,WAIT,DESTROY},
47 * so there's no separate mutex protecting access to (c)->waiters
49 #define COND_INIT(c) \
52 (c)->sem = CreateSemaphore(NULL,0,LONG_MAX,NULL); \
53 if ((c)->sem == NULL) \
54 croak("panic: COND_INIT (%ld)",GetLastError()); \
57 #define COND_SIGNAL(c) \
59 if ((c)->waiters > 0 && \
60 ReleaseSemaphore((c)->sem,1,NULL) == 0) \
61 croak("panic: COND_SIGNAL (%ld)",GetLastError()); \
64 #define COND_BROADCAST(c) \
66 if ((c)->waiters > 0 && \
67 ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0) \
68 croak("panic: COND_BROADCAST (%ld)",GetLastError());\
71 #define COND_WAIT(c, m) \
75 /* Note that there's no race here, since a \
76 * COND_BROADCAST() on another thread will have seen the\
77 * right number of waiters (i.e. including this one) */ \
78 if (WaitForSingleObject((c)->sem,INFINITE)==WAIT_FAILED)\
79 croak("panic: COND_WAIT (%ld)",GetLastError()); \
80 /* XXX there may be an inconsequential race here */ \
85 #define COND_DESTROY(c) \
88 if (CloseHandle((c)->sem) == 0) \
89 croak("panic: COND_DESTROY (%ld)",GetLastError()); \
94 if (CloseHandle((t)->self) == 0) { \
95 MUTEX_UNLOCK(&(t)->mutex); \
96 croak("panic: DETACH"); \
101 #define THREAD_CREATE(t, f) Perl_thread_create(t, f)
102 #define THREAD_POST_CREATE(t) NOOP
103 #define THREAD_RET_TYPE DWORD WINAPI
104 #define THREAD_RET_CAST(p) ((DWORD)(p))
106 typedef THREAD_RET_TYPE thread_func_t(void *);
111 #if defined(PERLDLL) && defined(_DLL)
112 extern __declspec(thread) struct thread *Perl_current_thread;
113 #define SET_THR(t) (Perl_current_thread = t)
114 #define THR Perl_current_thread
116 #define THR Perl_getTHR()
117 #define SET_THR(t) Perl_setTHR(t)
120 void Perl_alloc_thread_key _((void));
121 int Perl_thread_create _((struct thread *thr, thread_func_t *fn));
122 void Perl_set_thread_self _((struct thread *thr));
123 struct thread *Perl_getTHR _((void));
124 void Perl_setTHR _((struct thread *t));
128 #define INIT_THREADS NOOP
129 #define ALLOC_THREAD_KEY NOOP
130 #define SET_THREAD_SELF(thr) Perl_set_thread_self(thr)
132 #define JOIN(t, avp) \
134 if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED) \
135 || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0)) \
136 croak("panic: JOIN"); \
139 #define YIELD Sleep(0)
141 #endif /* _WIN32THREAD_H */