This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
integrate cfgperl change#6217 into mainline
[perl5.git] / win32 / perllib.c
index 26135f8..fae3346 100644 (file)
@@ -143,6 +143,13 @@ perl_alloc(void)
     return my_perl;
 }
 
+EXTERN_C void
+win32_delete_internal_host(void *h)
+{
+    CPerlHost *host = (CPerlHost*)h;
+    delete host;
+}
+
 #ifdef PERL_OBJECT
 
 EXTERN_C void
@@ -157,10 +164,7 @@ perl_construct(PerlInterpreter* my_perl)
     {
        win32_fprintf(stderr, "%s\n",
                      "Error: Unable to construct data structures");
-       CPerlHost* pHost = (CPerlHost*)w32_internal_host;
-       Perl_free();
-       delete pHost;
-       SetPerlInterpreter(NULL);
+       perl_free(my_perl);
     }
 }
 
@@ -185,32 +189,30 @@ EXTERN_C void
 perl_free(PerlInterpreter* my_perl)
 {
     CPerlObj* pPerl = (CPerlObj*)my_perl;
+    void *host = w32_internal_host;
 #ifdef DEBUGGING
-    CPerlHost* pHost = (CPerlHost*)w32_internal_host;
     Perl_free();
-    delete pHost;
 #else
     try
     {
-       CPerlHost* pHost = (CPerlHost*)w32_internal_host;
        Perl_free();
-       delete pHost;
     }
     catch(...)
     {
     }
 #endif
-    SetPerlInterpreter(NULL);
+    win32_delete_internal_host(host);
+    PERL_SET_THX(NULL);
 }
 
 EXTERN_C int
 perl_run(PerlInterpreter* my_perl)
 {
     CPerlObj* pPerl = (CPerlObj*)my_perl;
+    int retVal;
 #ifdef DEBUGGING
-    return Perl_run();
+    retVal = Perl_run();
 #else
-    int retVal;
     try
     {
        retVal = Perl_run();
@@ -220,8 +222,8 @@ perl_run(PerlInterpreter* my_perl)
        win32_fprintf(stderr, "Error: Runtime exception\n");
        retVal = -1;
     }
-    return retVal;
 #endif
+    return retVal;
 }
 
 EXTERN_C int
@@ -254,32 +256,11 @@ 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)
 {
     int exitstatus;
     PerlInterpreter *my_perl, *new_perl = NULL;
-    struct perl_thread *thr;
 
 #ifndef __BORLANDC__
     /* XXX this _may_ be a problem on some compilers (e.g. Borland) that
@@ -309,7 +290,7 @@ RunPerl(int argc, char **argv, char **env)
 
     if (!(my_perl = perl_alloc()))
        return (1);
-    perl_construct( my_perl );
+    perl_construct(my_perl);
     PL_perl_destruct_level = 0;
 
     exitstatus = perl_parse(my_perl, xs_init, argc, argv, env);
@@ -332,18 +313,18 @@ RunPerl(int argc, char **argv, char **env)
 #  else
        new_perl = perl_clone(my_perl, 1);
 #  endif
-       exitstatus = perl_run( new_perl );
-       SetPerlInterpreter(my_perl);
+       exitstatus = perl_run(new_perl);
+       PERL_SET_THX(my_perl);
 #else
-       exitstatus = perl_run( my_perl );
+       exitstatus = perl_run(my_perl);
 #endif
     }
 
-    perl_destruct( my_perl );
-    perl_free( my_perl );
+    perl_destruct(my_perl);
+    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);
     }
@@ -354,6 +335,16 @@ RunPerl(int argc, char **argv, char **env)
     return (exitstatus);
 }
 
+EXTERN_C void
+set_w32_module_name(void);
+
+EXTERN_C void
+EndSockets(void);
+
+
+#ifdef __MINGW32__
+EXTERN_C               /* GCC in C++ mode mangles the name, otherwise */
+#endif
 BOOL APIENTRY
 DllMain(HANDLE hModule,                /* DLL module handle */
        DWORD fdwReason,        /* reason called */
@@ -371,16 +362,16 @@ 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;
+       set_w32_module_name();
        break;
 
        /* The DLL is detaching from a process due to
         * process termination or call to FreeLibrary.
         */
     case DLL_PROCESS_DETACH:
-       TlsFree(g_TlsAllocIndex);
+       EndSockets();
        break;
 
        /* The attached process creates a new thread. */