[DOC patch bleadperl] "misspelled" misspelled
[p5sagit/p5-mst-13.2.git] / wince / win32thread.c
1 // Time-stamp: <01/08/01 21:00:29 keuchel@w2k>
2
3 #include "EXTERN.h"
4 #include "perl.h"
5
6 #if defined(PERL_OBJECT)
7 #define NO_XSLOCKS
8 extern CPerlObj* pPerl;
9 #include "XSUB.h"
10 #endif
11
12 #ifdef USE_DECLSPEC_THREAD
13 __declspec(thread) void *PL_current_context = NULL;
14 #endif
15
16 void
17 Perl_set_context(void *t)
18 {
19 #if defined(USE_THREADS) || defined(USE_ITHREADS)
20 #  ifdef USE_DECLSPEC_THREAD
21     Perl_current_context = t;
22 #  else
23     DWORD err = GetLastError();
24     TlsSetValue(PL_thr_key,t);
25     SetLastError(err);
26 #  endif
27 #endif
28 }
29
30 void *
31 Perl_get_context(void)
32 {
33 #if defined(USE_THREADS) || defined(USE_ITHREADS)
34 #  ifdef USE_DECLSPEC_THREAD
35     return Perl_current_context;
36 #  else
37     DWORD err = GetLastError();
38     void *result = TlsGetValue(PL_thr_key);
39     SetLastError(err);
40     return result;
41 #  endif
42 #else
43     return NULL;
44 #endif
45 }
46
47 #ifdef USE_THREADS
48 void
49 Perl_init_thread_intern(struct perl_thread *athr)
50 {
51 #ifndef USE_DECLSPEC_THREAD
52
53  /* 
54   * Initialize port-specific per-thread data in thr->i
55   * as only things we have there are just static areas for
56   * return values we don't _need_ to do anything but 
57   * this is good practice:
58   */
59  memset(&athr->i,0,sizeof(athr->i));
60
61 #endif
62 }
63
64 void
65 Perl_set_thread_self(struct perl_thread *thr)
66 {
67     /* Set thr->self.  GetCurrentThread() retrurns a pseudo handle, need
68        this to convert it into a handle another thread can use.
69      */
70     DuplicateHandle(GetCurrentProcess(),
71                     GetCurrentThread(),
72                     GetCurrentProcess(),
73                     &thr->self,
74                     0,
75                     FALSE,
76                     DUPLICATE_SAME_ACCESS);
77 }
78
79 int
80 Perl_thread_create(struct perl_thread *thr, thread_func_t *fn)
81 {
82     DWORD junk;
83     unsigned long th;
84
85     DEBUG_S(PerlIO_printf(Perl_debug_log,
86                           "%p: create OS thread\n", thr));
87 #ifdef USE_RTL_THREAD_API
88     /* See comment about USE_RTL_THREAD_API in win32thread.h */
89 #if defined(__BORLANDC__)
90     th = _beginthreadNT(fn,                             /* start address */
91                         0,                              /* stack size */
92                         (void *)thr,                    /* parameters */
93                         (void *)NULL,                   /* security attrib */
94                         0,                              /* creation flags */
95                         (unsigned long *)&junk);        /* tid */
96     if (th == (unsigned long)-1)
97         th = 0;
98 #elif defined(_MSC_VER_)
99     th = _beginthreadex((void *)NULL,                   /* security attrib */
100                         0,                              /* stack size */
101                         fn,                             /* start address */
102                         (void*)thr,                     /* parameters */
103                         0,                              /* creation flags */
104                         (unsigned *)&junk);             /* tid */
105 #else /* compilers using CRTDLL.DLL only have _beginthread() */
106     th = _beginthread(fn,                               /* start address */
107                       0,                                /* stack size */
108                       (void*)thr);                      /* parameters */
109     if (th == (unsigned long)-1)
110         th = 0;
111 #endif
112     thr->self = (HANDLE)th;
113 #else   /* !USE_RTL_THREAD_API */
114     thr->self = CreateThread(NULL, 0, fn, (void*)thr, 0, &junk);
115 #endif  /* !USE_RTL_THREAD_API */
116     DEBUG_S(PerlIO_printf(Perl_debug_log,
117                           "%p: OS thread = %p, id=%ld\n", thr, thr->self, junk));
118     return thr->self ? 0 : -1;
119 }
120 #endif
121