Index: trunk/src/win32k/dev32/d32Win32kOpenClose.c
===================================================================
--- trunk/src/win32k/dev32/d32Win32kOpenClose.c	(revision 6295)
+++ trunk/src/win32k/dev32/d32Win32kOpenClose.c	(revision 6296)
@@ -1,3 +1,3 @@
-/* $Id: d32Win32kOpenClose.c,v 1.1 2001-07-08 02:53:18 bird Exp $
+/* $Id: d32Win32kOpenClose.c,v 1.2 2001-07-10 16:39:16 bird Exp $
  *
  * Open and Close handlers for the Win32k driver.
@@ -16,4 +16,6 @@
 #define INCL_NOPMAPI
 #define INCL_OS2KRNL_TK
+#define INCL_OS2KRNL_SEM
+#define INCL_OS2KRNL_LDR
 #define INCL_OS2KRNL_PTDA
 
@@ -52,9 +54,23 @@
 USHORT _loadds _Far32 _Pascal Win32kOpen(PRP32OPENCLOSE pRpOpen)
 {
-    PPTD pptd = GetTaskData(0);
+    APIRET  rc;
+    PPTD    pptd;
+
+    /*
+     * Take Loader semaphore as that currently protects everything in this driver...
+     */
+    rc = LDRRequestSem();
+    if (rc != NO_ERROR)
+    {
+        kprintf(("Win32kOpen: LDRRequestSem failed with rc = %d\n", rc));
+        //return rc;
+    }
+
+    pptd = GetTaskData(0, TRUE);
     if (pptd)
         pptd->cUsage++;
 
     pRpOpen = pRpOpen;
+    LDRClearSem();
     return STATUS_DONE;
 }
@@ -70,22 +86,28 @@
 USHORT _loadds _Far32 _Pascal Win32kClose(PRP32OPENCLOSE pRpClose)
 {
-    PPTD pptd = GetTaskData(0);
+    APIRET  rc;
+    PPTD    pptd;
+
+    /*
+     * Take Loader semaphore as that currently protects everything in this driver...
+     */
+    rc = LDRRequestSem();
+    if (rc != NO_ERROR)
+    {
+        kprintf(("Win32kClose: LDRRequestSem failed with rc = %d\n", rc));
+        //return rc;
+    }
+
+    pptd = GetTaskData(0, FALSE);
     if (pptd)
     {
         if (pptd->cUsage > 0)
-        {
-            APIRET  rc;
-            if (    pptd->pszzOdin32Env != NULL
-                && (rc = D32Hlp_VMUnLock(&pptd->lockOdin32Env)) != NO_ERROR
-                )
-            {
-                kprintf(("Win32kClose: VMUnLock failed with rc=%d\n", rc));
-            }
             pptd->cUsage--;
-        }
         if (pptd->cUsage == 0)
             RemoveTaskData(0);
     }
     pRpClose = pRpClose;
+
+    LDRClearSem();
     return STATUS_DONE;
 }
Index: trunk/src/win32k/include/PerTaskW32kData.h
===================================================================
--- trunk/src/win32k/include/PerTaskW32kData.h	(revision 6295)
+++ trunk/src/win32k/include/PerTaskW32kData.h	(revision 6296)
@@ -1,3 +1,3 @@
-/* $Id: PerTaskW32kData.h,v 1.1 2001-07-08 02:58:36 bird Exp $
+/* $Id: PerTaskW32kData.h,v 1.2 2001-07-10 16:41:10 bird Exp $
  *
  * Per Task (Win32k) Data.
@@ -20,5 +20,5 @@
     AVLNODECORE     core;
     ULONG           cUsage;             /* Usage counter. */
-    PID             pid;                /* Also in core.Key */
+    PPTDA           pPTDA;              /* Also in core.Key */
     PSZ             pszzOdin32Env;      /* Pointer to environment block. */
     LOCKHANDLE      lockOdin32Env;      /* VMLock handle for the environment block. */
@@ -26,6 +26,6 @@
 
 
-PPTD _Optlink   GetTaskData(PID pid);
-void _Optlink   RemoveTaskData(PID pid);
+PPTD _Optlink   GetTaskData(PPTDA pPTDA, BOOL fCreate);
+void _Optlink   RemoveTaskData(PPTDA pPTDA);
 
 #endif
Index: trunk/src/win32k/k32/k32SetEnvironment.cpp
===================================================================
--- trunk/src/win32k/k32/k32SetEnvironment.cpp	(revision 6295)
+++ trunk/src/win32k/k32/k32SetEnvironment.cpp	(revision 6296)
@@ -1,3 +1,3 @@
-/* $Id: k32SetEnvironment.cpp,v 1.1 2001-07-08 02:52:11 bird Exp $
+/* $Id: k32SetEnvironment.cpp,v 1.2 2001-07-10 16:39:17 bird Exp $
  *
  * k32SetEnvironment - Sets the Odin32 environment for a process.
@@ -15,4 +15,5 @@
 #define INCL_DOSERRORS
 #define INCL_OS2KRNL_TK
+#define INCL_OS2KRNL_PTDA
 #define INCL_OS2KRNL_SEM
 #define INCL_OS2KRNL_LDR
@@ -24,4 +25,5 @@
 *******************************************************************************/
 #include <os2.h>
+#include <memory.h>
 #include "devSegDf.h"                   /* Win32k segment definitions. */
 #include "OS2Krnl.h"
@@ -56,5 +58,4 @@
     PPTD    pptd;
 
-
     /*
      * Validate pid a little bit...
@@ -63,4 +64,10 @@
     {
         kprintf(("k32SetEnvironment: invalid pid=%x\n", pid));
+        return ERROR_INVALID_PARAMETER;
+    }
+
+    if (pid != 0)
+    {
+        kprintf(("k32SetEnvironment: currently only supported for current pid. pid=%x\n", pid));
         return ERROR_INVALID_PARAMETER;
     }
@@ -81,23 +88,15 @@
      * Get Per Task Data and try set next environment pointer.
      */
-    pptd = GetTaskData(pid);
+    pptd = GetTaskData(ptdaGetCur(), TRUE);
     if (pptd)
     {
-        if (pptd->cUsage != 0 && pptd->pszzOdin32Env)
+        if (*(PULONG)&pptd->lockOdin32Env)
+        {
             D32Hlp_VMUnLock(&pptd->lockOdin32Env);
+            memset(&pptd->lockOdin32Env, 0, sizeof(pptd->lockOdin32Env));
+        }
         pptd->pszzOdin32Env = NULL;
         if (pptd->cUsage != 0 && pszzEnvironment != NULL && cchEnvironment != 0)
         {
-            /*
-             * Currently we're limited to the current process.
-             */
-            if (pid != 0 || GetTaskData(0) != pptd)
-            {
-                kprintf(("k32SetEnvironment: not current process (pid=%d)\n", pid));
-                LDRClearSem();
-                return ERROR_INVALID_PARAMETER;
-            }
-
-
             /*
              * Lock the memory (so we don't block or trap during environment searchs).
@@ -110,4 +109,5 @@
             {
                 kprintf(("k32SetEnvironment: VMLock2 failed with rc=%d\n", rc));
+                memset(&pptd->lockOdin32Env, 0, sizeof(pptd->lockOdin32Env));
             }
         }
Index: trunk/src/win32k/misc/PerTaskW32kData.c
===================================================================
--- trunk/src/win32k/misc/PerTaskW32kData.c	(revision 6295)
+++ trunk/src/win32k/misc/PerTaskW32kData.c	(revision 6296)
@@ -1,3 +1,3 @@
-/* $Id: PerTaskW32kData.c,v 1.1 2001-07-08 03:09:03 bird Exp $
+/* $Id: PerTaskW32kData.c,v 1.2 2001-07-10 16:39:19 bird Exp $
  *
  * Per Task (Win32k) Data.
@@ -17,4 +17,5 @@
 #define INCL_OS2KRNL_VM                 /* OS2KRNL: Virtual Memory Management */
 #define INCL_OS2KRNL_TK                 /* OS2KRNL: Task Stuff */
+#define INCL_OS2KRNL_PTDA
 
 /*******************************************************************************
@@ -52,16 +53,18 @@
  * If it doesn't exist we'll allocate a new one.
  * @returns Pointer to task data structure.
- * @param   pid     Process id. 0 means current pid.
+ *          Might be NULL (see fCreate).
+ * @param   pPTDA   Pointer to PTDA for the task.
+ * @param   fCreate TRUE:  Create structure if not found.
+ *                  FALSE: Return NULL if not found.
  * @status  completely implemented.
  * @author  knut st. osmundsen (kosmunds@csc.no)
- * @remark  Do we need a Get function which doesn't make a node if no data was found?
  */
-PPTD    GetTaskData(PID pid)
+PPTD    GetTaskData(PPTDA pPTDA, BOOL fCreate)
 {
     PPTD    pptd;
-    if (pid == 0)
-        pid = InternalGetPid();
-    pptd = (PPTD)(void*)AVLGet(&pPTDTree, pid);
-    if (!pptd)
+    if (pPTDA == 0)
+        pPTDA = ptdaGetCur();
+    pptd = (PPTD)(void*)AVLGet(&pPTDTree, (AVLKEY)pPTDA);
+    if (!pptd && fCreate)
     {
         pptd = rmalloc(sizeof(*pptd));
@@ -72,5 +75,5 @@
         }
         memset(pptd, 0, sizeof(*pptd));
-        pptd->core.Key = pid;
+        pptd->core.Key = (AVLKEY)pPTDA;
         AVLInsert(&pPTDTree, &pptd->core);
     }
@@ -81,17 +84,23 @@
 /**
  * Remove the give process id.
- * @param   pid     Process id to remove. 0 means current pid.
+ * @param   pPTDA   Pointer to PTDA for the task.
  * @status  completely implemented.
  * @author  knut st. osmundsen (kosmunds@csc.no)
  */
-void    RemoveTaskData(PID pid)
+void    RemoveTaskData(PPTDA pPTDA)
 {
     PPTD  pptd;
-    if (pid == 0)
-        pid = InternalGetPid();
-    pptd = (PPTD)(void*)AVLRemove(&pPTDTree, pid);
+    if (pPTDA == 0)
+        pPTDA = ptdaGetCur();
+    pptd = (PPTD)(void*)AVLRemove(&pPTDTree, (AVLKEY)pPTDA);
     if (pptd)
     {
         /* perhaps free data here... */
+        if (*(PULONG)(void*)&pptd->lockOdin32Env)
+        {
+            D32Hlp_VMUnLock(&pptd->lockOdin32Env);
+            memset(&pptd->lockOdin32Env, 0, sizeof(pptd->lockOdin32Env));
+        }
+        pptd->pszzOdin32Env = NULL;
         rfree(pptd);
     }
Index: trunk/src/win32k/pe2lx/pe2lx.cpp
===================================================================
--- trunk/src/win32k/pe2lx/pe2lx.cpp	(revision 6295)
+++ trunk/src/win32k/pe2lx/pe2lx.cpp	(revision 6296)
@@ -1,3 +1,3 @@
-/* $Id: pe2lx.cpp,v 1.30 2001-07-09 12:56:53 bird Exp $
+/* $Id: pe2lx.cpp,v 1.31 2001-07-10 16:39:18 bird Exp $
  *
  * Pe2Lx class implementation. Ring 0 and Ring 3
@@ -1766,5 +1766,5 @@
             case FINDDLL_PATH:
             {
-                PPTD    pptd = GetTaskData(0);
+                PPTD    pptd = GetTaskData(NULL, FALSE);
                 pszPath = NULL;
                 if (pptd)
