Index: trunk/src/kernel32/makefile
===================================================================
--- trunk/src/kernel32/makefile	(revision 123)
+++ trunk/src/kernel32/makefile	(revision 125)
@@ -1,3 +1,3 @@
-# $Id: makefile,v 1.6 1999-06-17 21:52:01 phaller Exp $
+# $Id: makefile,v 1.7 1999-06-19 17:58:32 sandervl Exp $
 
 #
@@ -125,4 +125,5 @@
 thread.OBJ: \
     .\thread.cpp \
+    $(PDWIN32_INCLUDE)\wprocess.h \
     thread.h
 
@@ -172,4 +173,5 @@
     $(PDWIN32_INCLUDE)\winimage.h \
     $(PDWIN32_INCLUDE)\versionos2.h \
+    $(PDWIN32_INCLUDE)\wprocess.h \
     console2.h \
     cio.h \
Index: trunk/src/kernel32/os2util.cpp
===================================================================
--- trunk/src/kernel32/os2util.cpp	(revision 123)
+++ trunk/src/kernel32/os2util.cpp	(revision 125)
@@ -1,3 +1,3 @@
-/* $Id: os2util.cpp,v 1.4 1999-06-19 13:57:51 sandervl Exp $ */
+/* $Id: os2util.cpp,v 1.5 1999-06-19 17:58:32 sandervl Exp $ */
 
 /*
@@ -284,3 +284,19 @@
 }
 //******************************************************************************
-//******************************************************************************
+//Allocate local thread memory
+//******************************************************************************
+ULONG OS2AllocThreadLocalMemory(int nrdwords)
+{
+ APIRET rc;
+ PULONG thrdaddr;
+
+   rc = DosAllocThreadLocalMemory(nrdwords, &thrdaddr);
+   if(rc) {
+	dprintf(("DosAllocThreadLocalMemory failed %d", rc));
+	return 0;
+   }
+   return (ULONG)thrdaddr;
+}
+//******************************************************************************
+//******************************************************************************
+
Index: trunk/src/kernel32/os2util.h
===================================================================
--- trunk/src/kernel32/os2util.h	(revision 123)
+++ trunk/src/kernel32/os2util.h	(revision 125)
@@ -1,3 +1,3 @@
-/* $Id: os2util.h,v 1.4 1999-06-19 13:57:51 sandervl Exp $ */
+/* $Id: os2util.h,v 1.5 1999-06-19 17:58:33 sandervl Exp $ */
 
 /*
@@ -59,4 +59,6 @@
 ULONG OS2GetPIB(int piboff);
 
+ULONG OS2AllocThreadLocalMemory(int nrdwords);
+
 #ifdef __cplusplus
       }
Index: trunk/src/kernel32/thread.H
===================================================================
--- trunk/src/kernel32/thread.H	(revision 123)
+++ trunk/src/kernel32/thread.H	(revision 125)
@@ -1,3 +1,3 @@
-/* $Id: thread.H,v 1.3 1999-06-10 19:11:31 phaller Exp $ */
+/* $Id: thread.H,v 1.4 1999-06-19 17:58:33 sandervl Exp $ */
 
 /*
@@ -22,5 +22,5 @@
 {
 public:
-    Win32Thread(LPTHREAD_START_ROUTINE pUserCallback, LPVOID lpData);
+    Win32Thread(LPTHREAD_START_ROUTINE pUserCallback, LPVOID lpData, DWORD dwFlags);
     ~Win32Thread();
 
@@ -30,4 +30,6 @@
  LPVOID      lpUserData;
  LPTHREAD_START_ROUTINE pCallback;
+ DWORD       dwFlags;
+
  friend static DWORD OPEN32API Win32ThreadProc(LPVOID lpData);
 };
Index: trunk/src/kernel32/thread.cpp
===================================================================
--- trunk/src/kernel32/thread.cpp	(revision 123)
+++ trunk/src/kernel32/thread.cpp	(revision 125)
@@ -1,3 +1,3 @@
-/* $Id: thread.cpp,v 1.4 1999-06-02 16:00:38 cbratschi Exp $ */
+/* $Id: thread.cpp,v 1.5 1999-06-19 17:58:33 sandervl Exp $ */
 
 /*
@@ -17,4 +17,5 @@
 #include "except.h"
 #include "misc.h"
+#include <wprocess.h>
 
 static DWORD OPEN32API Win32ThreadProc(LPVOID lpData);
@@ -24,9 +25,9 @@
 //******************************************************************************
 HANDLE WIN32API CreateThread(LPSECURITY_ATTRIBUTES lpsa, DWORD cbStack,
-                   LPTHREAD_START_ROUTINE lpStartAddr,
-                   LPVOID lpvThreadParm, DWORD fdwCreate,
-                   LPDWORD lpIDThread)
+                             LPTHREAD_START_ROUTINE lpStartAddr,
+                             LPVOID lpvThreadParm, DWORD fdwCreate,
+                             LPDWORD lpIDThread)
 {
- Win32Thread *winthread = new Win32Thread(lpStartAddr, lpvThreadParm);
+ Win32Thread *winthread = new Win32Thread(lpStartAddr, lpvThreadParm, fdwCreate);
 
   if(winthread == 0)
@@ -122,12 +123,16 @@
            exitcode));
 
+#ifdef WIN32_TIBSEL
+  DestroyTIB();
+#endif
   O32_ExitThread(exitcode);
 }
 //******************************************************************************
 //******************************************************************************
-Win32Thread::Win32Thread(LPTHREAD_START_ROUTINE pUserCallback, LPVOID lpData)
+Win32Thread::Win32Thread(LPTHREAD_START_ROUTINE pUserCallback, LPVOID lpData, DWORD dwFlags)
 {
   lpUserData = lpData;
   pCallback  = pUserCallback;
+  this->dwFlags = dwFlags;
 }
 //******************************************************************************
@@ -153,8 +158,10 @@
   delete me;    //only called once
 
-  dprintf(("Win32ThreadProc %d\n",
-           GetCurrentThreadId()));
+  dprintf(("Win32ThreadProc %d\n", GetCurrentThreadId()));
 
+#ifdef WIN32_TIBSEL
+#else
   ReplaceExceptionHandler();
+#endif
   return(winthread(userdata));
 }
Index: trunk/src/kernel32/wprocess.cpp
===================================================================
--- trunk/src/kernel32/wprocess.cpp	(revision 123)
+++ trunk/src/kernel32/wprocess.cpp	(revision 125)
@@ -1,3 +1,3 @@
-/* $Id: wprocess.cpp,v 1.9 1999-06-19 13:57:51 sandervl Exp $ */
+/* $Id: wprocess.cpp,v 1.10 1999-06-19 17:58:33 sandervl Exp $ */
 
 /*
@@ -38,7 +38,6 @@
 PDB       ProcessPDB = {0};  
 USHORT	  ProcessTIBSel = 0;
-USHORT	  OS2Selector   = 0;
-
-//#define WIN32_TIBSEL
+DWORD    *TIBFlatPtr    = 0;
+
 //******************************************************************************
 // Set up the TIB selector and memory for the current thread
@@ -52,4 +51,11 @@
   USHORT tibsel;
 
+   //Allocate one dword to store the flat address of our TEB
+   TIBFlatPtr = (DWORD *)OS2AllocThreadLocalMemory(1);
+   if(TIBFlatPtr == 0) {
+	dprintf(("InitializeTIB: local thread memory alloc failed!!"));
+	DebugInt3();
+	return;
+   }
    if(OS2AllocSel(PAGE_SIZE, &tibsel) == FALSE)
    {
@@ -66,5 +72,6 @@
    }
    memset(winteb, 0, PAGE_SIZE);
-   thdb = (THDB *)(winteb+1);
+   thdb       = (THDB *)(winteb+1);
+   TIBFlatPtr = (DWORD)winteb;
 
    winteb->except      = (PVOID)-1;               /* 00 Head of exception handling chain */
@@ -83,4 +90,5 @@
    thdb->exit_code       = 0x103; /* STILL_ACTIVE */
    thdb->teb_sel         = tibsel;
+   thdb->OrgTIBSel       = GetFS();
 
    if(OS2GetPIB(PIB_TASKTYPE) == TASKTYPE_PM) 
@@ -96,5 +104,4 @@
         //TLS in executable always TLS index 0?
 	ProcessTIBSel = tibsel;
-	OS2Selector   = GetFS();	//0x150b
    }
    SetFS(tibsel);
@@ -108,7 +115,24 @@
 void DestroyTIB()
 {
+#ifdef WIN32_TIBSEL
+ SHORT  orgtibsel;
+ TEB   *winteb;
+ THDB  *thdb;
+
    dprintf(("DestroyTIB: FS = %x", GetFS()));
-#ifdef WIN32_TIBSEL
-   OS2FreeSel(ProcessTIBSel);
+
+   winteb = (TEB *)TIBFlatPtr;
+   if(winteb) {
+	thdb = (THDB *)(winteb+1);
+	orgtibsel = thdb->OrgTIBSel;
+
+	//Restore our original FS selector
+   	SetFS(orgtibsel);
+
+   	//And free our own
+   	OS2FreeSel(thdb->teb_sel);
+   }
+   else	DebugInt3();
+
    return;
 #endif
@@ -254,5 +278,5 @@
 #ifdef WIN32_TIBSEL
   //Restore original OS/2 TIB selector
-  SetFS(OS2Selector);
+  DestroyTIB();
 #endif
 
