Commit | Line | Data |
---|---|---|
ea0efc06 MB |
1 | #include "EXTERN.h" |
2 | #include "perl.h" | |
d55594ae | 3 | |
c69f6586 GS |
4 | #if defined(PERL_OBJECT) |
5 | #define NO_XSLOCKS | |
6 | extern CPerlObj* pPerl; | |
7 | #include "XSUB.h" | |
8 | #endif | |
9 | ||
c69f112c | 10 | #ifdef USE_DECLSPEC_THREAD |
61c8b479 | 11 | __declspec(thread) struct perl_thread *Perl_current_thread = NULL; |
c69f112c | 12 | #endif |
9811a7d7 NIS |
13 | |
14 | void | |
61c8b479 | 15 | Perl_setTHR(struct perl_thread *t) |
9811a7d7 | 16 | { |
4c36ecb7 | 17 | #ifdef USE_THREADS |
c69f112c | 18 | #ifdef USE_DECLSPEC_THREAD |
0fefa03b | 19 | Perl_current_thread = t; |
c69f112c | 20 | #else |
6b88bc9c | 21 | TlsSetValue(PL_thr_key,t); |
c69f112c | 22 | #endif |
4c36ecb7 | 23 | #endif |
9811a7d7 NIS |
24 | } |
25 | ||
61c8b479 | 26 | struct perl_thread * |
9811a7d7 NIS |
27 | Perl_getTHR(void) |
28 | { | |
4c36ecb7 | 29 | #ifdef USE_THREADS |
c69f112c | 30 | #ifdef USE_DECLSPEC_THREAD |
0fefa03b | 31 | return Perl_current_thread; |
c69f112c | 32 | #else |
6b88bc9c | 33 | return (struct perl_thread *) TlsGetValue(PL_thr_key); |
c69f112c | 34 | #endif |
4c36ecb7 NIS |
35 | #else |
36 | return NULL; | |
37 | #endif | |
9811a7d7 NIS |
38 | } |
39 | ||
d55594ae GS |
40 | void |
41 | Perl_alloc_thread_key(void) | |
42 | { | |
43 | #ifdef USE_THREADS | |
44 | static int key_allocated = 0; | |
45 | if (!key_allocated) { | |
6b88bc9c | 46 | if ((PL_thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) |
4f63d024 | 47 | Perl_croak_nocontext("panic: TlsAlloc"); |
d55594ae GS |
48 | key_allocated = 1; |
49 | } | |
50 | #endif | |
51 | } | |
ea0efc06 MB |
52 | |
53 | void | |
22239a37 | 54 | Perl_init_thread_intern(struct perl_thread *athr) |
c53bd28a NIS |
55 | { |
56 | #ifdef USE_THREADS | |
57 | #ifndef USE_DECLSPEC_THREAD | |
58 | ||
59 | /* | |
60 | * Initialize port-specific per-thread data in thr->i | |
61 | * as only things we have there are just static areas for | |
62 | * return values we don't _need_ to do anything but | |
63 | * this is good practice: | |
64 | */ | |
22239a37 | 65 | memset(&athr->i,0,sizeof(athr->i)); |
c53bd28a NIS |
66 | |
67 | #endif | |
68 | #endif | |
69 | } | |
70 | ||
71 | void | |
52e1cb5e | 72 | Perl_set_thread_self(struct perl_thread *thr) |
ea0efc06 | 73 | { |
d55594ae | 74 | #ifdef USE_THREADS |
4b026b9e GS |
75 | /* Set thr->self. GetCurrentThread() retrurns a pseudo handle, need |
76 | this to convert it into a handle another thread can use. | |
d55594ae | 77 | */ |
ea0efc06 MB |
78 | DuplicateHandle(GetCurrentProcess(), |
79 | GetCurrentThread(), | |
80 | GetCurrentProcess(), | |
46930d8f | 81 | &thr->self, |
ea0efc06 MB |
82 | 0, |
83 | FALSE, | |
84 | DUPLICATE_SAME_ACCESS); | |
d55594ae | 85 | #endif |
ea0efc06 MB |
86 | } |
87 | ||
d55594ae | 88 | #ifdef USE_THREADS |
ea0efc06 | 89 | int |
52e1cb5e | 90 | Perl_thread_create(struct perl_thread *thr, thread_func_t *fn) |
ea0efc06 MB |
91 | { |
92 | DWORD junk; | |
2d7a9237 | 93 | unsigned long th; |
ea0efc06 | 94 | |
bf49b057 | 95 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
d55594ae | 96 | "%p: create OS thread\n", thr)); |
2d7a9237 GS |
97 | #ifdef USE_RTL_THREAD_API |
98 | /* See comment about USE_RTL_THREAD_API in win32thread.h */ | |
99 | #if defined(__BORLANDC__) | |
100 | th = _beginthreadNT(fn, /* start address */ | |
101 | 0, /* stack size */ | |
102 | (void *)thr, /* parameters */ | |
103 | (void *)NULL, /* security attrib */ | |
104 | 0, /* creation flags */ | |
105 | (unsigned long *)&junk); /* tid */ | |
106 | if (th == (unsigned long)-1) | |
107 | th = 0; | |
108 | #elif defined(_MSC_VER_) | |
109 | th = _beginthreadex((void *)NULL, /* security attrib */ | |
110 | 0, /* stack size */ | |
111 | fn, /* start address */ | |
112 | (void*)thr, /* parameters */ | |
113 | 0, /* creation flags */ | |
114 | (unsigned *)&junk); /* tid */ | |
115 | #else /* compilers using CRTDLL.DLL only have _beginthread() */ | |
116 | th = _beginthread(fn, /* start address */ | |
117 | 0, /* stack size */ | |
118 | (void*)thr); /* parameters */ | |
119 | if (th == (unsigned long)-1) | |
120 | th = 0; | |
121 | #endif | |
122 | thr->self = (HANDLE)th; | |
123 | #else /* !USE_RTL_THREAD_API */ | |
46930d8f | 124 | thr->self = CreateThread(NULL, 0, fn, (void*)thr, 0, &junk); |
2d7a9237 | 125 | #endif /* !USE_RTL_THREAD_API */ |
bf49b057 | 126 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
d55594ae | 127 | "%p: OS thread = %p, id=%ld\n", thr, thr->self, junk)); |
46930d8f | 128 | return thr->self ? 0 : -1; |
ea0efc06 | 129 | } |
d55594ae | 130 | #endif |
22239a37 | 131 |