This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add Data-Dumper, up patchlevel to 71, various misc tweaks to
[perl5.git] / win32 / win32thread.c
1 #include "EXTERN.h"
2 #include "perl.h"
3
4 #if defined(PERL_OBJECT)
5 #define NO_XSLOCKS
6 extern CPerlObj* pPerl;
7 #include "XSUB.h"
8 #endif
9
10 #ifdef USE_DECLSPEC_THREAD
11 __declspec(thread) struct perl_thread *Perl_current_thread = NULL;
12 #endif
13
14 void
15 Perl_setTHR(struct perl_thread *t)
16 {
17 #ifdef USE_THREADS
18 #ifdef USE_DECLSPEC_THREAD
19  Perl_current_thread = t;
20 #else
21  TlsSetValue(thr_key,t);
22 #endif
23 #endif
24 }
25
26 struct perl_thread *
27 Perl_getTHR(void)
28 {
29 #ifdef USE_THREADS
30 #ifdef USE_DECLSPEC_THREAD
31  return Perl_current_thread;
32 #else
33  return (struct perl_thread *) TlsGetValue(thr_key);
34 #endif
35 #else
36  return NULL;
37 #endif
38 }
39
40 void
41 Perl_alloc_thread_key(void)
42 {
43 #ifdef USE_THREADS
44     static int key_allocated = 0;
45     if (!key_allocated) {
46         if ((thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
47             croak("panic: TlsAlloc");
48         key_allocated = 1;
49     }
50 #endif
51 }
52
53 void
54 Perl_init_thread_intern(struct perl_thread *athr)
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   */
65  memset(&athr->i,0,sizeof(athr->i));
66
67 #endif
68 #endif
69 }
70
71 void
72 Perl_set_thread_self(struct perl_thread *thr)
73 {
74 #ifdef USE_THREADS
75     /* Set thr->self.  GetCurrentThread() retrurns a pseudo handle, need
76        this to convert it into a handle another thread can use.
77      */
78     DuplicateHandle(GetCurrentProcess(),
79                     GetCurrentThread(),
80                     GetCurrentProcess(),
81                     &thr->self,
82                     0,
83                     FALSE,
84                     DUPLICATE_SAME_ACCESS);
85 #endif
86 }
87
88 #ifdef USE_THREADS
89 int
90 Perl_thread_create(struct perl_thread *thr, thread_func_t *fn)
91 {
92     DWORD junk;
93     unsigned long th;
94
95     MUTEX_LOCK(&thr->mutex);
96     DEBUG_L(PerlIO_printf(PerlIO_stderr(),
97                           "%p: create OS thread\n", thr));
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 */
125     thr->self = CreateThread(NULL, 0, fn, (void*)thr, 0, &junk);
126 #endif  /* !USE_RTL_THREAD_API */
127     DEBUG_L(PerlIO_printf(PerlIO_stderr(),
128                           "%p: OS thread = %p, id=%ld\n", thr, thr->self, junk));
129     MUTEX_UNLOCK(&thr->mutex);
130     return thr->self ? 0 : -1;
131 }
132 #endif
133