This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use __declspec(thread) var rather tha TslAlloc & co.
[perl5.git] / win32 / win32thread.c
1 #include "EXTERN.h"
2 #include "perl.h"
3
4 __declspec(thread) struct thread *current_thread;
5
6 void
7 Perl_setTHR(struct thread *t)
8 {
9  current_thread = t;
10 }
11
12 struct thread *
13 Perl_getTHR(void)
14 {
15  return current_thread;
16 }
17
18 void
19 Perl_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 }
30
31 void
32 Perl_set_thread_self(struct thread *thr)
33 {
34 #ifdef USE_THREADS
35     /* Set thr->self.  GetCurrentThread() retrurns a pseudo handle, need
36        this to convert it into a handle another thread can use.
37      */
38     DuplicateHandle(GetCurrentProcess(),
39                     GetCurrentThread(),
40                     GetCurrentProcess(),
41                     &thr->self,
42                     0,
43                     FALSE,
44                     DUPLICATE_SAME_ACCESS);
45 #endif
46 }
47
48 #ifdef USE_THREADS
49 int
50 Perl_thread_create(struct thread *thr, thread_func_t *fn)
51 {
52     DWORD junk;
53
54     MUTEX_LOCK(&thr->mutex);
55     DEBUG_L(PerlIO_printf(PerlIO_stderr(),
56                           "%p: create OS thread\n", thr));
57     thr->self = CreateThread(NULL, 0, fn, (void*)thr, 0, &junk);
58     DEBUG_L(PerlIO_printf(PerlIO_stderr(),
59                           "%p: OS thread = %p, id=%ld\n", thr, thr->self, junk));
60     MUTEX_UNLOCK(&thr->mutex);
61     return thr->self ? 0 : -1;
62 }
63 #endif