[project @ 1999-10-07 13:09:48 by sof]
authorsof <unknown>
Thu, 7 Oct 1999 13:09:48 +0000 (13:09 +0000)
committersof <unknown>
Thu, 7 Oct 1999 13:09:48 +0000 (13:09 +0000)
Delay shutting down WinSock until exit() time

ghc/lib/misc/cbits/initWinSock.c

index f928f3e..672a098 100644 (file)
@@ -11,6 +11,9 @@
 
 #ifdef USE_WINSOCK
 
+static int winsock_inited = 0;
+static int winsock_uninited = 0;
+
 /* Initialising WinSock... */
 StgInt
 initWinSock ()
@@ -19,26 +22,38 @@ initWinSock ()
   WSADATA wsaData;  
   int err;
 
-  wVersionRequested = MAKEWORD( 1, 1 );
+  if (!winsock_inited) {
+    wVersionRequested = MAKEWORD( 1, 1 );
 
-  err = WSAStartup ( wVersionRequested, &wsaData );
+    err = WSAStartup ( wVersionRequested, &wsaData );
 
-  if ( err != 0 ) {
-     return err;
-  }
+    if ( err != 0 ) {
+       return err;
+    }
 
-  if ( LOBYTE( wsaData.wVersion ) != 1 ||
+    if ( LOBYTE( wsaData.wVersion ) != 1 ||
        HIBYTE( wsaData.wVersion ) != 1 ) {
-    WSACleanup();
-    return (-1);
+      WSACleanup();
+      return (-1);
+    }
+    winsock_inited = 1;
   }
   return 0;
 }
 
+static void
+shutdownHandler()
+{
+  WSACleanup();
+}
+
 void
 shutdownWinSock()
 {
- WSACleanup();
+    if (!winsock_uninited) {
+       atexit(shutdownHandler);
+       winsock_uninited = 1;
+    }
 }
 
 #endif