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