Index: trunk/src/kernel32/windll.cpp
===================================================================
--- trunk/src/kernel32/windll.cpp	(revision 650)
+++ trunk/src/kernel32/windll.cpp	(revision 651)
@@ -1,3 +1,3 @@
-/* $Id: windll.cpp,v 1.13 1999-08-22 22:11:22 sandervl Exp $ */
+/* $Id: windll.cpp,v 1.14 1999-08-23 17:02:35 sandervl Exp $ */
 
 /*
@@ -31,4 +31,7 @@
 #include "exceptutil.h"
 #include "cio.h"
+#include "vmutex.h"
+
+VMutex dlllistmutex;   //protects linked lists of heaps
 
 /***********************************
@@ -40,15 +43,42 @@
 //******************************************************************************
 //******************************************************************************
-Win32Dll::Win32Dll(char *szDllName) : Win32Image(szDllName), referenced(0), 
-				      fSkipEntryCalls(FALSE), fSystemDll(FALSE)
+Win32Dll::Win32Dll(char *szDllName, Win32Image *parentImage) 
+                : Win32Image(szDllName), referenced(0), 
+		  fSkipEntryCalls(FALSE), fSystemDll(FALSE),
+                  fAttachedToProcess(FALSE)
 {
   fSystemDll   = isSystemDll(szFileName);
   fUnloaded    = FALSE;
-  next = head;
-  head = this;
+
+  dlllistmutex.enter();
+  if(head == NULL || parentImage == NULL || parentImage->isDll() == FALSE) { //does the head dll depend on this one?
+  	next = head;
+  	head = this;
+  }
+  else {
+	//If we have a parent, we must make sure it is deleted before we are!
+        //(inserted after the parent)
+	if(head == parentImage) {
+		next = head->next;
+		head->next = this;
+	}
+	else {
+		Win32Dll *cur = head;
+		while(cur) {
+			if(cur == parentImage) {
+				break;
+			}
+			cur = cur->next;
+		}
+		next = cur->next;
+		cur->next = this;
+	}
+  }
+  dlllistmutex.leave();
 
   dllEntryPoint = 0;
 
-  dprintf(("Win32Dll::Win32Dll %s %s", szFileName, szModule));
+  dprintf(("Win32Dll::Win32Dll %s %s loaded by %s", szFileName, szModule,
+          (parentImage) ? parentImage->getModuleName() : "Unknown"));
 }
 //******************************************************************************
@@ -57,10 +87,14 @@
 		   WIN32DLLENTRY DllEntryPoint) : 
 			Win32Image(hinstance, NameTableId, Win32TableId),
-			referenced(0), fSkipEntryCalls(FALSE), fSystemDll(FALSE)
+			referenced(0), fSkipEntryCalls(FALSE), fSystemDll(FALSE),
+                        fAttachedToProcess(FALSE)
 {
   dllEntryPoint = DllEntryPoint;
   fUnloaded     = FALSE;
+
+  dlllistmutex.enter();
   next = head;
   head = this;
+  dlllistmutex.leave();
 
   dprintf(("Win32Dll::Win32Dll %s", szModule));
@@ -86,4 +120,5 @@
   //first remove it from the linked list so converted win32 dlls won't
   //be deleted twice (as DosFreeModule results in a call to DllExitList (wprocess.cpp)
+  dlllistmutex.enter();
   if(head == this) {
 	head = next;
@@ -95,8 +130,10 @@
 	if(dll == NULL) {
 		dprintf(("~Win32Dll: Can't find dll!\n"));
+		dlllistmutex.leave();
 		return;
 	}
 	dll->next = next;
   }
+  dlllistmutex.leave();
   if(errorState == NO_ERROR && !fUnloaded) 
   {
@@ -281,4 +318,9 @@
  BOOL rc;
 
+  if(fAttachedToProcess)
+	return TRUE;
+  
+  fAttachedToProcess = TRUE;
+
   //Allocate TLS index for this module
   tlsAlloc();
@@ -380,6 +422,5 @@
 void Win32Dll::attachThreadToAllDlls()
 {
- Win32Dll *dll = Win32Dll::head;
-
+  Win32Dll *dll = Win32Dll::head;
   while(dll) {
 	dll->attachThread();
@@ -392,6 +433,5 @@
 void Win32Dll::detachThreadFromAllDlls()
 {
- Win32Dll *dll = Win32Dll::head;
-
+  Win32Dll *dll = Win32Dll::head;
   while(dll) {
 	dll->detachThread();
@@ -404,6 +444,5 @@
 void Win32Dll::tlsAttachThreadToAllDlls()
 {
- Win32Dll *dll = Win32Dll::head;
-
+  Win32Dll *dll = Win32Dll::head;
   while(dll) {
 	dll->tlsAttachThread();
@@ -416,6 +455,5 @@
 void Win32Dll::tlsDetachThreadFromAllDlls()
 {
- Win32Dll *dll = Win32Dll::head;
-
+  Win32Dll *dll = Win32Dll::head;
   while(dll) {
 	dll->tlsDetachThread();
@@ -427,5 +465,16 @@
 void Win32Dll::deleteAll()
 {
-  //LIFO removal
+#ifdef DEBUG
+  dlllistmutex.enter();
+  Win32Dll *dll = head;
+
+  dprintf(("Win32Dll::deleteAll: List of loaded dlls:"));
+  while(dll) {
+	dprintf(("DLL %s", dll->szModule));
+	dll = dll->next;
+  }
+  dlllistmutex.leave();
+#endif
+
   while(Win32Dll::head) {
 	delete Win32Dll::head;
@@ -436,5 +485,5 @@
 Win32Dll *Win32Dll::findModule(char *dllname)
 {
- Win32Dll *dll = head;
+ Win32Dll *dll;
  char szDllName[CCHMAXPATH];
  char *dot, *temp;
@@ -448,10 +497,15 @@
 	*dot = 0;
 
+  dlllistmutex.enter();
+  dll = head;
   while(dll) {
-	if(strcmpi(szDllName, dll->szModule) == 0)
+	if(strcmpi(szDllName, dll->szModule) == 0) {
+		dlllistmutex.leave();
 		return(dll);
+        }
 
 	dll = dll->next;
   }
+  dlllistmutex.leave();
   return(NULL);
 }
@@ -460,13 +514,17 @@
 Win32Dll *Win32Dll::findModule(WIN32DLLENTRY DllEntryPoint)
 {
- Win32Dll *mod = Win32Dll::head;
-
    dprintf(("findModule %X", DllEntryPoint));
+
+   dlllistmutex.enter();
+   Win32Dll *mod = Win32Dll::head;
    while(mod != NULL) {
 	dbgCheckObj(mod);
-	if(mod->dllEntryPoint == DllEntryPoint)
+	if(mod->dllEntryPoint == DllEntryPoint) {
+		dlllistmutex.leave();
 		return(mod);
+        }
 	mod = mod->next;
    }
+   dlllistmutex.leave();
    return(NULL);
 }
@@ -475,13 +533,16 @@
 Win32Dll *Win32Dll::findModule(HINSTANCE hinstance)
 {
- Win32Dll *mod = Win32Dll::head;
-
-//   eprintf(("findModule inst %X", hinstance));
+   dlllistmutex.enter();
+
+   Win32Dll *mod = Win32Dll::head;
    while(mod != NULL) {
 	dbgCheckObj(mod);
-	if(mod->hinstance == hinstance)
+	if(mod->hinstance == hinstance) {
+		dlllistmutex.leave();
 		return(mod);
+        }
 	mod = mod->next;
    }
+   dlllistmutex.leave();
    return(NULL);
 }
Index: trunk/src/kernel32/winimage.cpp
===================================================================
--- trunk/src/kernel32/winimage.cpp	(revision 650)
+++ trunk/src/kernel32/winimage.cpp	(revision 651)
@@ -1,3 +1,3 @@
-/* $Id: winimage.cpp,v 1.14 1999-08-23 13:54:43 sandervl Exp $ */
+/* $Id: winimage.cpp,v 1.15 1999-08-23 17:02:35 sandervl Exp $ */
 
 /*
@@ -89,5 +89,5 @@
 	sprintf(logname, "pe_%d.log", loadNr);
     	fout.open(logname, ios::out | ios::trunc);
-	dprintf(("PE LOGFILE for %s: %s (rc=%d)", szModule, logname));
+	dprintf(("PE LOGFILE for %s: %s", szModule, logname));
     	foutInit = TRUE;
   }
@@ -1132,5 +1132,5 @@
     if(WinDll == NULL)
     {  //not found, so load it
-        WinDll = new Win32Dll(pszCurModule);
+        WinDll = new Win32Dll(pszCurModule, this);
 
         if(WinDll == NULL) {
