This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
avoid warnings in legacy code (from David Dyck)
[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(PL_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(PL_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 ((PL_thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
47             Perl_croak_nocontext("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     DEBUG_S(PerlIO_printf(Perl_debug_log,
96                           "%p: create OS thread\n", thr));
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 */
124     thr->self = CreateThread(NULL, 0, fn, (void*)thr, 0, &junk);
125 #endif  /* !USE_RTL_THREAD_API */
126     DEBUG_S(PerlIO_printf(Perl_debug_log,
127                           "%p: OS thread = %p, id=%ld\n", thr, thr->self, junk));
128     return thr->self ? 0 : -1;
129 }
130 #endif
131