diff options
author | Gurusamy Sarathy <gsar@cpan.org> | 2000-02-29 04:53:00 +0000 |
---|---|---|
committer | Gurusamy Sarathy <gsar@cpan.org> | 2000-02-29 04:53:00 +0000 |
commit | ba869debd80c55cfae8e9d4de0991d62f9efcb9b (patch) | |
tree | 73f5810de88203f0df413aee0bc7f3c94d686329 /win32 | |
parent | d5c3ff09f505a33428a0772c6e16c91e1df4ce85 (diff) | |
download | perl-ba869debd80c55cfae8e9d4de0991d62f9efcb9b.tar.gz |
support fetching current interpreter from TLS under useithreads
p4raw-id: //depot/perl@5342
Diffstat (limited to 'win32')
-rw-r--r-- | win32/perlhost.h | 6 | ||||
-rw-r--r-- | win32/perllib.c | 30 | ||||
-rw-r--r-- | win32/win32.h | 7 | ||||
-rw-r--r-- | win32/win32thread.c | 56 | ||||
-rw-r--r-- | win32/win32thread.h | 50 |
5 files changed, 63 insertions, 86 deletions
diff --git a/win32/perlhost.h b/win32/perlhost.h index a748ead0b2..a3f4c28350 100644 --- a/win32/perlhost.h +++ b/win32/perlhost.h @@ -1650,7 +1650,7 @@ win32_start_child(LPVOID arg) #endif - PERL_SET_INTERP(my_perl); + PERL_SET_THX(my_perl); /* set $$ to pseudo id */ #ifdef PERL_SYNC_FORK @@ -1745,7 +1745,7 @@ PerlProcFork(struct IPerlProc* piPerl) new_perl->Isys_intern.internal_host = h; # ifdef PERL_SYNC_FORK id = win32_start_child((LPVOID)new_perl); - PERL_SET_INTERP(aTHXo); + PERL_SET_THX(aTHXo); # else # ifdef USE_RTL_THREAD_API handle = (HANDLE)_beginthreadex((void*)NULL, 0, win32_start_child, @@ -1754,7 +1754,7 @@ PerlProcFork(struct IPerlProc* piPerl) handle = CreateThread(NULL, 0, win32_start_child, (LPVOID)new_perl, 0, &id); # endif - PERL_SET_INTERP(aTHXo); /* XXX perl_clone*() set TLS */ + PERL_SET_THX(aTHXo); /* XXX perl_clone*() set TLS */ if (!handle) Perl_croak(aTHX_ "panic: pseudo fork() failed"); w32_pseudo_child_handles[w32_num_pseudo_children] = handle; diff --git a/win32/perllib.c b/win32/perllib.c index 26135f864e..3aed2415fd 100644 --- a/win32/perllib.c +++ b/win32/perllib.c @@ -160,7 +160,7 @@ perl_construct(PerlInterpreter* my_perl) CPerlHost* pHost = (CPerlHost*)w32_internal_host; Perl_free(); delete pHost; - SetPerlInterpreter(NULL); + PERL_SET_THX(NULL); } } @@ -200,7 +200,7 @@ perl_free(PerlInterpreter* my_perl) { } #endif - SetPerlInterpreter(NULL); + PERL_SET_THX(NULL); } EXTERN_C int @@ -254,26 +254,6 @@ perl_parse(PerlInterpreter* my_perl, void (*xsinit)(CPerlObj*), int argc, char** EXTERN_C HANDLE w32_perldll_handle; -static DWORD g_TlsAllocIndex; - -EXTERN_C DllExport bool -SetPerlInterpreter(void *interp) -{ - DWORD dwErr = GetLastError(); - bool bResult = TlsSetValue(g_TlsAllocIndex, interp); - SetLastError(dwErr); - return bResult; -} - -EXTERN_C DllExport void* -GetPerlInterpreter(void) -{ - DWORD dwErr = GetLastError(); - LPVOID pResult = TlsGetValue(g_TlsAllocIndex); - SetLastError(dwErr); - return pResult; -} - EXTERN_C DllExport int RunPerl(int argc, char **argv, char **env) { @@ -333,7 +313,7 @@ RunPerl(int argc, char **argv, char **env) new_perl = perl_clone(my_perl, 1); # endif exitstatus = perl_run( new_perl ); - SetPerlInterpreter(my_perl); + PERL_SET_THX(my_perl); #else exitstatus = perl_run( my_perl ); #endif @@ -343,7 +323,7 @@ RunPerl(int argc, char **argv, char **env) perl_free( my_perl ); #ifdef USE_ITHREADS if (new_perl) { - SetPerlInterpreter(new_perl); + PERL_SET_THX(new_perl); perl_destruct(new_perl); perl_free(new_perl); } @@ -371,7 +351,6 @@ DllMain(HANDLE hModule, /* DLL module handle */ setmode( fileno( stderr ), O_BINARY ); _fmode = O_BINARY; #endif - g_TlsAllocIndex = TlsAlloc(); DisableThreadLibraryCalls((HMODULE)hModule); w32_perldll_handle = hModule; break; @@ -380,7 +359,6 @@ DllMain(HANDLE hModule, /* DLL module handle */ * process termination or call to FreeLibrary. */ case DLL_PROCESS_DETACH: - TlsFree(g_TlsAllocIndex); break; /* The attached process creates a new thread. */ diff --git a/win32/win32.h b/win32/win32.h index 4e73a23fe6..f102234b78 100644 --- a/win32/win32.h +++ b/win32/win32.h @@ -28,8 +28,9 @@ #endif #if defined(PERL_IMPLICIT_CONTEXT) -# define PERL_GET_INTERP ((PerlInterpreter*)GetPerlInterpreter()) -# define PERL_SET_INTERP(i) (SetPerlInterpreter(i)) +/* compat */ +# define GetPerlInterpreter Perl_get_context +# define SetPerlInterpreter Perl_set_context #endif #ifdef __GNUC__ @@ -298,8 +299,6 @@ DllExport void Perl_win32_init(int *argcp, char ***argvp); DllExport void Perl_init_os_extras(); DllExport void win32_str_os_error(void *sv, DWORD err); DllExport int RunPerl(int argc, char **argv, char **env); -DllExport bool SetPerlInterpreter(void* interp); -DllExport void* GetPerlInterpreter(void); typedef struct { HANDLE childStdIn; diff --git a/win32/win32thread.c b/win32/win32thread.c index 1bca3c3ca5..900f5fedff 100644 --- a/win32/win32thread.c +++ b/win32/win32thread.c @@ -8,52 +8,44 @@ extern CPerlObj* pPerl; #endif #ifdef USE_DECLSPEC_THREAD -__declspec(thread) struct perl_thread *Perl_current_thread = NULL; +__declspec(thread) void *PL_current_context = NULL; #endif void -Perl_setTHR(struct perl_thread *t) +Perl_set_context(void *t) { -#ifdef USE_THREADS -#ifdef USE_DECLSPEC_THREAD - Perl_current_thread = t; -#else - TlsSetValue(PL_thr_key,t); -#endif +#if defined(USE_THREADS) || defined(USE_ITHREADS) +# ifdef USE_DECLSPEC_THREAD + Perl_current_context = t; +# else + DWORD err = GetLastError(); + TlsSetValue(PL_thr_key,t); + SetLastError(err); +# endif #endif } -struct perl_thread * -Perl_getTHR(void) +void * +Perl_get_context(void) { -#ifdef USE_THREADS -#ifdef USE_DECLSPEC_THREAD - return Perl_current_thread; -#else - return (struct perl_thread *) TlsGetValue(PL_thr_key); -#endif +#if defined(USE_THREADS) || defined(USE_ITHREADS) +# ifdef USE_DECLSPEC_THREAD + return Perl_current_context; +# else + DWORD err = GetLastError(); + void *result = TlsGetValue(PL_thr_key); + SetLastError(err); + return result; +# endif #else - return NULL; + return NULL; #endif } -void -Perl_alloc_thread_key(void) -{ #ifdef USE_THREADS - static int key_allocated = 0; - if (!key_allocated) { - if ((PL_thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) - Perl_croak_nocontext("panic: TlsAlloc"); - key_allocated = 1; - } -#endif -} - void Perl_init_thread_intern(struct perl_thread *athr) { -#ifdef USE_THREADS #ifndef USE_DECLSPEC_THREAD /* @@ -65,13 +57,11 @@ Perl_init_thread_intern(struct perl_thread *athr) memset(&athr->i,0,sizeof(athr->i)); #endif -#endif } void Perl_set_thread_self(struct perl_thread *thr) { -#ifdef USE_THREADS /* Set thr->self. GetCurrentThread() retrurns a pseudo handle, need this to convert it into a handle another thread can use. */ @@ -82,10 +72,8 @@ Perl_set_thread_self(struct perl_thread *thr) 0, FALSE, DUPLICATE_SAME_ACCESS); -#endif } -#ifdef USE_THREADS int Perl_thread_create(struct perl_thread *thr, thread_func_t *fn) { diff --git a/win32/win32thread.h b/win32/win32thread.h index d4f8ee409e..cfa13cc9f5 100644 --- a/win32/win32thread.h +++ b/win32/win32thread.h @@ -24,32 +24,37 @@ typedef CRITICAL_SECTION perl_mutex; #else typedef HANDLE perl_mutex; -#define MUTEX_INIT(m) \ +# define MUTEX_INIT(m) \ STMT_START { \ if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) \ - Perl_croak(aTHX_ "panic: MUTEX_INIT"); \ + Perl_croak(aTHX_ "panic: MUTEX_INIT"); \ } STMT_END -#define MUTEX_LOCK(m) \ + +# define MUTEX_LOCK(m) \ STMT_START { \ if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \ Perl_croak(aTHX_ "panic: MUTEX_LOCK"); \ } STMT_END -#define MUTEX_UNLOCK(m) \ + +# define MUTEX_UNLOCK(m) \ STMT_START { \ if (ReleaseMutex(*(m)) == 0) \ Perl_croak(aTHX_ "panic: MUTEX_UNLOCK"); \ } STMT_END -#define MUTEX_LOCK_NOCONTEXT(m) \ + +# define MUTEX_LOCK_NOCONTEXT(m) \ STMT_START { \ if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \ Perl_croak_nocontext("panic: MUTEX_LOCK"); \ } STMT_END -#define MUTEX_UNLOCK_NOCONTEXT(m) \ + +# define MUTEX_UNLOCK_NOCONTEXT(m) \ STMT_START { \ if (ReleaseMutex(*(m)) == 0) \ Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \ } STMT_END -#define MUTEX_DESTROY(m) \ + +# define MUTEX_DESTROY(m) \ STMT_START { \ if (CloseHandle(*(m)) == 0) \ Perl_croak(aTHX_ "panic: MUTEX_DESTROY"); \ @@ -155,27 +160,34 @@ typedef THREAD_RET_TYPE thread_func_t(void *); START_EXTERN_C #if defined(PERLDLL) && defined(USE_DECLSPEC_THREAD) && (!defined(__BORLANDC__) || defined(_DLL)) -extern __declspec(thread) struct perl_thread *Perl_current_thread; -#define SET_THR(t) (Perl_current_thread = t) -#define THR Perl_current_thread +extern __declspec(thread) void *PL_current_context; +#define PERL_SET_CONTEXT(t) (PL_current_context = t) +#define PERL_GET_CONTEXT PL_current_context #else -#define THR Perl_getTHR() -#define SET_THR(t) Perl_setTHR(t) +#define PERL_GET_CONTEXT Perl_get_context() +#define PERL_SET_CONTEXT(t) Perl_set_context(t) #endif -struct perl_thread; -void Perl_alloc_thread_key (void); +#define PERL_GET_CONTEXT_DEFINED + +#if defined(USE_THREADS) +struct perl_thread; int Perl_thread_create (struct perl_thread *thr, thread_func_t *fn); void Perl_set_thread_self (struct perl_thread *thr); -struct perl_thread *Perl_getTHR (void); -void Perl_setTHR (struct perl_thread *t); void Perl_init_thread_intern (struct perl_thread *t); +#define SET_THREAD_SELF(thr) Perl_set_thread_self(thr) + +#endif /* USE_THREADS */ + END_EXTERN_C -#define INIT_THREADS NOOP -#define ALLOC_THREAD_KEY Perl_alloc_thread_key() -#define SET_THREAD_SELF(thr) Perl_set_thread_self(thr) +#define INIT_THREADS NOOP +#define ALLOC_THREAD_KEY \ + STMT_START { \ + if ((PL_thr_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) \ + Perl_croak_nocontext("panic: TlsAlloc"); \ + } STMT_END #if defined(USE_RTL_THREAD_API) && !defined(_MSC_VER) #define JOIN(t, avp) \ |