This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Yet another twist.
[perl5.git] / win32 / win32thread.c
CommitLineData
ea0efc06
MB
1#include "EXTERN.h"
2#include "perl.h"
d55594ae 3
c69f112c 4#ifdef USE_DECLSPEC_THREAD
ba869deb 5__declspec(thread) void *PL_current_context = NULL;
c69f112c 6#endif
9811a7d7
NIS
7
8void
ba869deb 9Perl_set_context(void *t)
9811a7d7 10{
4d1ff10f 11#if defined(USE_5005THREADS) || defined(USE_ITHREADS)
ba869deb
GS
12# ifdef USE_DECLSPEC_THREAD
13 Perl_current_context = t;
14# else
15 DWORD err = GetLastError();
16 TlsSetValue(PL_thr_key,t);
17 SetLastError(err);
18# endif
4c36ecb7 19#endif
9811a7d7
NIS
20}
21
ba869deb
GS
22void *
23Perl_get_context(void)
9811a7d7 24{
4d1ff10f 25#if defined(USE_5005THREADS) || defined(USE_ITHREADS)
ba869deb
GS
26# ifdef USE_DECLSPEC_THREAD
27 return Perl_current_context;
28# else
29 DWORD err = GetLastError();
30 void *result = TlsGetValue(PL_thr_key);
31 SetLastError(err);
32 return result;
33# endif
4c36ecb7 34#else
ba869deb 35 return NULL;
4c36ecb7 36#endif
9811a7d7
NIS
37}
38
4d1ff10f 39#ifdef USE_5005THREADS
ea0efc06 40void
22239a37 41Perl_init_thread_intern(struct perl_thread *athr)
c53bd28a 42{
c53bd28a
NIS
43#ifndef USE_DECLSPEC_THREAD
44
45 /*
46 * Initialize port-specific per-thread data in thr->i
47 * as only things we have there are just static areas for
48 * return values we don't _need_ to do anything but
49 * this is good practice:
50 */
22239a37 51 memset(&athr->i,0,sizeof(athr->i));
c53bd28a
NIS
52
53#endif
c53bd28a
NIS
54}
55
56void
52e1cb5e 57Perl_set_thread_self(struct perl_thread *thr)
ea0efc06 58{
4b026b9e
GS
59 /* Set thr->self. GetCurrentThread() retrurns a pseudo handle, need
60 this to convert it into a handle another thread can use.
d55594ae 61 */
ea0efc06
MB
62 DuplicateHandle(GetCurrentProcess(),
63 GetCurrentThread(),
64 GetCurrentProcess(),
46930d8f 65 &thr->self,
ea0efc06
MB
66 0,
67 FALSE,
68 DUPLICATE_SAME_ACCESS);
ea0efc06
MB
69}
70
71int
52e1cb5e 72Perl_thread_create(struct perl_thread *thr, thread_func_t *fn)
ea0efc06
MB
73{
74 DWORD junk;
2d7a9237 75 unsigned long th;
ea0efc06 76
bf49b057 77 DEBUG_S(PerlIO_printf(Perl_debug_log,
d55594ae 78 "%p: create OS thread\n", thr));
2d7a9237
GS
79#ifdef USE_RTL_THREAD_API
80 /* See comment about USE_RTL_THREAD_API in win32thread.h */
81#if defined(__BORLANDC__)
82 th = _beginthreadNT(fn, /* start address */
83 0, /* stack size */
84 (void *)thr, /* parameters */
85 (void *)NULL, /* security attrib */
86 0, /* creation flags */
87 (unsigned long *)&junk); /* tid */
88 if (th == (unsigned long)-1)
89 th = 0;
90#elif defined(_MSC_VER_)
91 th = _beginthreadex((void *)NULL, /* security attrib */
92 0, /* stack size */
93 fn, /* start address */
94 (void*)thr, /* parameters */
95 0, /* creation flags */
96 (unsigned *)&junk); /* tid */
97#else /* compilers using CRTDLL.DLL only have _beginthread() */
98 th = _beginthread(fn, /* start address */
99 0, /* stack size */
100 (void*)thr); /* parameters */
101 if (th == (unsigned long)-1)
102 th = 0;
103#endif
104 thr->self = (HANDLE)th;
105#else /* !USE_RTL_THREAD_API */
46930d8f 106 thr->self = CreateThread(NULL, 0, fn, (void*)thr, 0, &junk);
2d7a9237 107#endif /* !USE_RTL_THREAD_API */
bf49b057 108 DEBUG_S(PerlIO_printf(Perl_debug_log,
d55594ae 109 "%p: OS thread = %p, id=%ld\n", thr, thr->self, junk));
46930d8f 110 return thr->self ? 0 : -1;
ea0efc06 111}
d55594ae 112#endif
22239a37 113