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) |
d55594ae GS |
47 | croak("panic: TlsAlloc"); |
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 MB |
94 | |
95 | MUTEX_LOCK(&thr->mutex); | |
8b73bbec | 96 | DEBUG_S(PerlIO_printf(PerlIO_stderr(), |
d55594ae | 97 | "%p: create OS thread\n", thr)); |
2d7a9237 GS |
98 | #ifdef USE_RTL_THREAD_API |
99 | /* See comment about USE_RTL_THREAD_API in win32thread.h */ | |
100 | #if defined(__BORLANDC__) | |
101 | th = _beginthreadNT(fn, /* start address */ | |
102 | 0, /* stack size */ | |
103 | (void *)thr, /* parameters */ | |
104 | (void *)NULL, /* security attrib */ | |
105 | 0, /* creation flags */ | |
106 | (unsigned long *)&junk); /* tid */ | |
107 | if (th == (unsigned long)-1) | |
108 | th = 0; | |
109 | #elif defined(_MSC_VER_) | |
110 | th = _beginthreadex((void *)NULL, /* security attrib */ | |
111 | 0, /* stack size */ | |
112 | fn, /* start address */ | |
113 | (void*)thr, /* parameters */ | |
114 | 0, /* creation flags */ | |
115 | (unsigned *)&junk); /* tid */ | |
116 | #else /* compilers using CRTDLL.DLL only have _beginthread() */ | |
117 | th = _beginthread(fn, /* start address */ | |
118 | 0, /* stack size */ | |
119 | (void*)thr); /* parameters */ | |
120 | if (th == (unsigned long)-1) | |
121 | th = 0; | |
122 | #endif | |
123 | thr->self = (HANDLE)th; | |
124 | #else /* !USE_RTL_THREAD_API */ | |
46930d8f | 125 | thr->self = CreateThread(NULL, 0, fn, (void*)thr, 0, &junk); |
2d7a9237 | 126 | #endif /* !USE_RTL_THREAD_API */ |
8b73bbec | 127 | DEBUG_S(PerlIO_printf(PerlIO_stderr(), |
d55594ae | 128 | "%p: OS thread = %p, id=%ld\n", thr, thr->self, junk)); |
ea0efc06 | 129 | MUTEX_UNLOCK(&thr->mutex); |
46930d8f | 130 | return thr->self ? 0 : -1; |
ea0efc06 | 131 | } |
d55594ae | 132 | #endif |
22239a37 | 133 |