This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
AUTHORS update.
[perl5.git] / wince / win32thread.h
1 /* Time-stamp: <01/08/01 21:00:36 keuchel@w2k> */
2
3 #ifndef _WIN32THREAD_H
4 #define _WIN32THREAD_H
5
6 #include "win32.h"
7
8 typedef struct win32_cond { LONG waiters; HANDLE sem; } perl_cond;
9 typedef DWORD perl_key;
10 typedef HANDLE perl_os_thread;
11
12 #ifndef DONT_USE_CRITICAL_SECTION
13
14 /* Critical Sections used instead of mutexes: lightweight,
15  * but can't be communicated to child processes, and can't get
16  * HANDLE to it for use elsewhere.
17  */
18 typedef CRITICAL_SECTION perl_mutex;
19 #define MUTEX_INIT(m) InitializeCriticalSection(m)
20 #define MUTEX_LOCK(m) EnterCriticalSection(m)
21 #define MUTEX_UNLOCK(m) LeaveCriticalSection(m)
22 #define MUTEX_DESTROY(m) DeleteCriticalSection(m)
23
24 #else
25
26 typedef HANDLE perl_mutex;
27 #  define MUTEX_INIT(m) \
28     STMT_START {                                                \
29         if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL)      \
30             Perl_croak_nocontext("panic: MUTEX_INIT");          \
31     } STMT_END
32
33 #  define MUTEX_LOCK(m) \
34     STMT_START {                                                \
35         if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED)  \
36             Perl_croak_nocontext("panic: MUTEX_LOCK");          \
37     } STMT_END
38
39 #  define MUTEX_UNLOCK(m) \
40     STMT_START {                                                \
41         if (ReleaseMutex(*(m)) == 0)                            \
42             Perl_croak_nocontext("panic: MUTEX_UNLOCK");        \
43     } STMT_END
44
45 #  define MUTEX_DESTROY(m) \
46     STMT_START {                                                \
47         if (CloseHandle(*(m)) == 0)                             \
48             Perl_croak_nocontext("panic: MUTEX_DESTROY");       \
49     } STMT_END
50
51 #endif
52
53 /* These macros assume that the mutex associated with the condition
54  * will always be held before COND_{SIGNAL,BROADCAST,WAIT,DESTROY},
55  * so there's no separate mutex protecting access to (c)->waiters
56  */
57 #define COND_INIT(c) \
58     STMT_START {                                                \
59         (c)->waiters = 0;                                       \
60         (c)->sem = CreateSemaphore(NULL,0,LONG_MAX,NULL);       \
61         if ((c)->sem == NULL)                                   \
62             Perl_croak_nocontext("panic: COND_INIT (%ld)",GetLastError());      \
63     } STMT_END
64
65 #define COND_SIGNAL(c) \
66     STMT_START {                                                \
67         if ((c)->waiters > 0 &&                                 \
68             ReleaseSemaphore((c)->sem,1,NULL) == 0)             \
69             Perl_croak_nocontext("panic: COND_SIGNAL (%ld)",GetLastError());    \
70     } STMT_END
71
72 #define COND_BROADCAST(c) \
73     STMT_START {                                                \
74         if ((c)->waiters > 0 &&                                 \
75             ReleaseSemaphore((c)->sem,(c)->waiters,NULL) == 0)  \
76             Perl_croak_nocontext("panic: COND_BROADCAST (%ld)",GetLastError());\
77     } STMT_END
78
79 #define COND_WAIT(c, m) \
80     STMT_START {                                                \
81         (c)->waiters++;                                         \
82         MUTEX_UNLOCK(m);                                        \
83         /* Note that there's no race here, since a              \
84          * COND_BROADCAST() on another thread will have seen the\
85          * right number of waiters (i.e. including this one) */ \
86         if (WaitForSingleObject((c)->sem,INFINITE)==WAIT_FAILED)\
87             Perl_croak_nocontext("panic: COND_WAIT (%ld)",GetLastError());      \
88         /* XXX there may be an inconsequential race here */     \
89         MUTEX_LOCK(m);                                          \
90         (c)->waiters--;                                         \
91     } STMT_END
92
93 #define COND_DESTROY(c) \
94     STMT_START {                                                \
95         (c)->waiters = 0;                                       \
96         if (CloseHandle((c)->sem) == 0)                         \
97             Perl_croak_nocontext("panic: COND_DESTROY (%ld)",GetLastError());   \
98     } STMT_END
99
100 #define DETACH(t) \
101     STMT_START {                                                \
102         if (CloseHandle((t)->self) == 0) {                      \
103             MUTEX_UNLOCK(&(t)->mutex);                          \
104             Perl_croak_nocontext("panic: DETACH");              \
105         }                                                       \
106     } STMT_END
107
108
109 #define THREAD_CREATE(t, f)     Perl_thread_create(t, f)
110 #define THREAD_POST_CREATE(t)   NOOP
111
112 /* XXX Docs mention that the RTL versions of thread creation routines
113  * should be used, but that advice only seems applicable when the RTL
114  * is not in a DLL.  RTL DLLs in both Borland and VC seem to do all of
115  * the init/deinit required upon DLL_THREAD_ATTACH/DETACH.  So we seem
116  * to be completely safe using straight Win32 API calls, rather than
117  * the much braindamaged RTL calls.
118  *
119  * _beginthread() in the RTLs call CloseHandle() just after the thread
120  * function returns, which means: 1) we have a race on our hands
121  * 2) it is impossible to implement join() semantics.
122  *
123  * IOW, do *NOT* turn on USE_RTL_THREAD_API!  It is here
124  * for experimental purposes only. GSAR 98-01-02
125  */
126 #ifdef USE_RTL_THREAD_API
127 #  include <process.h>
128 #  if defined(__BORLANDC__)
129      /* Borland RTL doesn't allow a return value from thread function! */
130 #    define THREAD_RET_TYPE     void _USERENTRY
131 #    define THREAD_RET_CAST(p)  ((void)(thr->i.retv = (void *)(p)))
132 #  elif defined (_MSC_VER)
133 #    define THREAD_RET_TYPE     unsigned __stdcall
134 #    define THREAD_RET_CAST(p)  ((unsigned)(p))
135 #  else
136      /* CRTDLL.DLL doesn't allow a return value from thread function! */
137 #    define THREAD_RET_TYPE     void __cdecl
138 #    define THREAD_RET_CAST(p)  ((void)(thr->i.retv = (void *)(p)))
139 #  endif
140 #else   /* !USE_RTL_THREAD_API */
141 #  define THREAD_RET_TYPE       DWORD WINAPI
142 #  define THREAD_RET_CAST(p)    ((DWORD)(p))
143 #endif  /* !USE_RTL_THREAD_API */
144
145 typedef THREAD_RET_TYPE thread_func_t(void *);
146
147
148 START_EXTERN_C
149
150 #if defined(PERLDLL) && defined(USE_DECLSPEC_THREAD) && (!defined(__BORLANDC__) || defined(_DLL))
151 extern __declspec(thread) void *PL_current_context;
152 #define PERL_SET_CONTEXT(t)             (PL_current_context = t)
153 #define PERL_GET_CONTEXT                PL_current_context
154 #else
155 #define PERL_GET_CONTEXT                Perl_get_context()
156 #define PERL_SET_CONTEXT(t)             Perl_set_context(t)
157 #endif
158
159 #if defined(USE_5005THREADS)
160 struct perl_thread;
161 int Perl_thread_create (struct perl_thread *thr, thread_func_t *fn);
162 void Perl_set_thread_self (struct perl_thread *thr);
163 void Perl_init_thread_intern (struct perl_thread *t);
164
165 #define SET_THREAD_SELF(thr) Perl_set_thread_self(thr)
166
167 #endif /* USE_5005THREADS */
168
169 END_EXTERN_C
170
171 #define INIT_THREADS            NOOP
172 #define ALLOC_THREAD_KEY \
173     STMT_START {                                                        \
174         if ((PL_thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) {          \
175             fprintf(stderr,"panic: TlsAlloc");                          \
176             exit(1);                                                    \
177         }                                                               \
178     } STMT_END
179
180 #if defined(USE_RTL_THREAD_API) && !defined(_MSC_VER)
181 #define JOIN(t, avp)                                                    \
182     STMT_START {                                                        \
183         if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED)    \
184              || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0)      \
185              || (CloseHandle((t)->self) == 0))                          \
186             Perl_croak_nocontext("panic: JOIN");                        \
187         *avp = (AV *)((t)->i.retv);                                     \
188     } STMT_END
189 #else   /* !USE_RTL_THREAD_API || _MSC_VER */
190 #define JOIN(t, avp)                                                    \
191     STMT_START {                                                        \
192         if ((WaitForSingleObject((t)->self,INFINITE) == WAIT_FAILED)    \
193              || (GetExitCodeThread((t)->self,(LPDWORD)(avp)) == 0)      \
194              || (CloseHandle((t)->self) == 0))                          \
195             Perl_croak_nocontext("panic: JOIN");                        \
196     } STMT_END
197 #endif  /* !USE_RTL_THREAD_API || _MSC_VER */
198
199 #define YIELD                   Sleep(0)
200
201 #endif /* _WIN32THREAD_H */
202