Un-botch gcc workround
[p5sagit/p5-mst-13.2.git] / win32 / win32thread.c
CommitLineData
ea0efc06 1#include "EXTERN.h"
2#include "perl.h"
d55594ae 3
61c8b479 4__declspec(thread) struct perl_thread *Perl_current_thread = NULL;
9811a7d7 5
6void
61c8b479 7Perl_setTHR(struct perl_thread *t)
9811a7d7 8{
0fefa03b 9 Perl_current_thread = t;
9811a7d7 10}
11
61c8b479 12struct perl_thread *
9811a7d7 13Perl_getTHR(void)
14{
0fefa03b 15 return Perl_current_thread;
9811a7d7 16}
17
d55594ae 18void
19Perl_alloc_thread_key(void)
20{
21#ifdef USE_THREADS
22 static int key_allocated = 0;
23 if (!key_allocated) {
24 if ((thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
25 croak("panic: TlsAlloc");
26 key_allocated = 1;
27 }
28#endif
29}
ea0efc06 30
31void
52e1cb5e 32Perl_set_thread_self(struct perl_thread *thr)
ea0efc06 33{
d55594ae 34#ifdef USE_THREADS
4b026b9e 35 /* Set thr->self. GetCurrentThread() retrurns a pseudo handle, need
36 this to convert it into a handle another thread can use.
d55594ae 37 */
ea0efc06 38 DuplicateHandle(GetCurrentProcess(),
39 GetCurrentThread(),
40 GetCurrentProcess(),
46930d8f 41 &thr->self,
ea0efc06 42 0,
43 FALSE,
44 DUPLICATE_SAME_ACCESS);
d55594ae 45#endif
ea0efc06 46}
47
d55594ae 48#ifdef USE_THREADS
ea0efc06 49int
52e1cb5e 50Perl_thread_create(struct perl_thread *thr, thread_func_t *fn)
ea0efc06 51{
52 DWORD junk;
53
54 MUTEX_LOCK(&thr->mutex);
d55594ae 55 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
56 "%p: create OS thread\n", thr));
46930d8f 57 thr->self = CreateThread(NULL, 0, fn, (void*)thr, 0, &junk);
d55594ae 58 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
59 "%p: OS thread = %p, id=%ld\n", thr, thr->self, junk));
ea0efc06 60 MUTEX_UNLOCK(&thr->mutex);
46930d8f 61 return thr->self ? 0 : -1;
ea0efc06 62}
d55594ae 63#endif