92199678e265b9c6e251c1b06f278613fc2d5419
[p5sagit/p5-mst-13.2.git] / win32 / win32thread.c
1 #include "EXTERN.h"
2 #include "perl.h"
3
4 void
5 Perl_alloc_thread_key(void)
6 {
7 #ifdef USE_THREADS
8     static int key_allocated = 0;
9     if (!key_allocated) {
10         if ((thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
11             croak("panic: TlsAlloc");
12         key_allocated = 1;
13     }
14 #endif
15 }
16
17 void
18 Perl_set_thread_self(struct thread *thr)
19 {
20 #ifdef USE_THREADS
21     /* GetCurrentThread() retrurns a pseudo handle, need
22        this to convert it into a handle another thread can use
23      */
24     DuplicateHandle(GetCurrentProcess(),
25                     GetCurrentThread(),
26                     GetCurrentProcess(),
27                     &thr->self,
28                     0,
29                     FALSE,
30                     DUPLICATE_SAME_ACCESS);
31 #endif
32 }
33
34 #ifdef USE_THREADS
35 int
36 Perl_thread_create(struct thread *thr, thread_func_t *fn)
37 {
38     DWORD junk;
39
40     MUTEX_LOCK(&thr->mutex);
41     DEBUG_L(PerlIO_printf(PerlIO_stderr(),
42                           "%p: create OS thread\n", thr));
43     thr->self = CreateThread(NULL, 0, fn, (void*)thr, 0, &junk);
44     DEBUG_L(PerlIO_printf(PerlIO_stderr(),
45                           "%p: OS thread = %p, id=%ld\n", thr, thr->self, junk));
46     MUTEX_UNLOCK(&thr->mutex);
47     return thr->self ? 0 : -1;
48 }
49 #endif