Changes USE_THREADS to USE_5005THREADS in the entire source.
[p5sagit/p5-mst-13.2.git] / win32 / perllib.c
index 2a0cb85..2d5a87d 100644 (file)
@@ -1,8 +1,7 @@
 /*
  * "The Road goes ever on and on, down from the door where it began."
  */
-
-
+#define PERLIO_NOT_STDIO 0
 #include "EXTERN.h"
 #include "perl.h"
 
@@ -143,6 +142,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 +163,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;
-       PERL_SET_THX(NULL);
+       perl_free(my_perl);
     }
 }
 
@@ -185,21 +188,19 @@ 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
+    win32_delete_internal_host(host);
     PERL_SET_THX(NULL);
 }
 
@@ -207,10 +208,10 @@ 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 +221,8 @@ perl_run(PerlInterpreter* my_perl)
        win32_fprintf(stderr, "Error: Runtime exception\n");
        retVal = -1;
     }
-    return retVal;
 #endif
+    return retVal;
 }
 
 EXTERN_C int
@@ -259,7 +260,6 @@ 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
@@ -289,7 +289,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);
@@ -312,15 +312,15 @@ RunPerl(int argc, char **argv, char **env)
 #  else
        new_perl = perl_clone(my_perl, 1);
 #  endif
-       exitstatus = perl_run( new_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) {
        PERL_SET_THX(new_perl);
@@ -337,6 +337,13 @@ RunPerl(int argc, char **argv, char **env)
 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 */
@@ -363,6 +370,17 @@ DllMain(HANDLE hModule,            /* DLL module handle */
         * process termination or call to FreeLibrary.
         */
     case DLL_PROCESS_DETACH:
+        /* As long as we use TerminateProcess()/TerminateThread() etc. for mimicing kill()
+           anything here had better be harmless if:
+            A. Not called at all.
+            B. Called after memory allocation for Heap has been forcibly removed by OS.
+            PerlIO_cleanup() was done here but fails (B).
+         */     
+       EndSockets();
+#if defined(USE_5005THREADS) || defined(USE_ITHREADS)
+       if (PL_curinterp)
+           FREE_THREAD_KEY;
+#endif
        break;
 
        /* The attached process creates a new thread. */
@@ -378,3 +396,26 @@ DllMain(HANDLE hModule,            /* DLL module handle */
     }
     return TRUE;
 }
+
+#if defined(USE_ITHREADS) && defined(PERL_IMPLICIT_SYS)
+EXTERN_C PerlInterpreter *
+perl_clone_host(PerlInterpreter* proto_perl, UV flags) {
+    dTHXo;
+    CPerlHost *h;
+    h = new CPerlHost(*(CPerlHost*)PL_sys_intern.internal_host);
+    proto_perl = perl_clone_using(proto_perl, flags,
+                        h->m_pHostperlMem,
+                        h->m_pHostperlMemShared,
+                        h->m_pHostperlMemParse,
+                        h->m_pHostperlEnv,
+                        h->m_pHostperlStdIO,
+                        h->m_pHostperlLIO,
+                        h->m_pHostperlDir,
+                        h->m_pHostperlSock,
+                        h->m_pHostperlProc
+    );
+    proto_perl->Isys_intern.internal_host = h;
+    return proto_perl;
+       
+}
+#endif