]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Nt32Pkg/TimerDxe/Timer.c
Nt32Pkg: Use the merged Variable driver
[mirror_edk2.git] / Nt32Pkg / TimerDxe / Timer.c
index 363f1047d18cd09bf813c74693bdb221a5340c5d..c35de7a29a01db4dd9fa7a2b73ca986c31c7f6f7 100644 (file)
@@ -1,7 +1,7 @@
-/*++\r
+/**@file\r
 \r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
+Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials                          \r
 are licensed and made available under the terms and conditions of the BSD License         \r
 which accompanies this distribution.  The full text of the license may be found at        \r
 http://opensource.org/licenses/bsd-license.php                                            \r
@@ -21,12 +21,7 @@ Abstract:
   timer service.  In the future, the Thread creation should possibly be \r
   abstracted by the CPU architectural protocol\r
 \r
---*/\r
-\r
-//\r
-// Include common header file for this module.\r
-//\r
-#include "CommonHeader.h"\r
+**/\r
 \r
 #include "Timer.h"\r
 \r
@@ -120,9 +115,12 @@ Returns:
   if (!mCancelTimerThread) {\r
   \r
     //\r
-    //  Suspend the main thread until we are done\r
+    // Suspend the main thread until we are done.\r
+    // Enter the critical section before suspending\r
+    // and leave the critical section after resuming\r
+    // to avoid deadlock between main and timer thread.\r
     //\r
-\r
+    gWinNt->EnterCriticalSection (&mNtCriticalSection);\r
     gWinNt->SuspendThread (mNtMainThreadHandle);\r
 \r
     //\r
@@ -132,6 +130,7 @@ Returns:
     //\r
     if (mCancelTimerThread) {\r
       gWinNt->ResumeThread (mNtMainThreadHandle);\r
+      gWinNt->LeaveCriticalSection (&mNtCriticalSection);\r
       gWinNt->timeKillEvent (wTimerID);\r
       mMMTimerThreadID = 0;\r
       return ;\r
@@ -143,19 +142,21 @@ Returns:
       //  Resume the main thread\r
       //\r
       gWinNt->ResumeThread (mNtMainThreadHandle);\r
+      gWinNt->LeaveCriticalSection (&mNtCriticalSection);\r
 \r
       //\r
       //  Wait for interrupts to be enabled.\r
       //\r
       mCpu->GetInterruptState (mCpu, &InterruptState);\r
       while (!InterruptState) {\r
-        gWinNt->Sleep (0);\r
+        gWinNt->Sleep (1);\r
         mCpu->GetInterruptState (mCpu, &InterruptState);\r
       }\r
        \r
       //\r
       //  Suspend the main thread until we are done\r
       //\r
+      gWinNt->EnterCriticalSection (&mNtCriticalSection);\r
       gWinNt->SuspendThread (mNtMainThreadHandle);\r
       mCpu->GetInterruptState (mCpu, &InterruptState);\r
     }\r
@@ -179,9 +180,7 @@ Returns:
       //  expired since the last call is 10,000 times the number\r
       //  of ms.  (or 100ns units)\r
       //\r
-      gWinNt->EnterCriticalSection (&mNtCriticalSection);\r
       CallbackFunction = mTimerNotifyFunction;\r
-      gWinNt->LeaveCriticalSection (&mNtCriticalSection);\r
 \r
       //\r
       // Only invoke the callback function if a Non-NULL handler has been\r
@@ -199,6 +198,7 @@ Returns:
     //  Resume the main thread\r
     //\r
     gWinNt->ResumeThread (mNtMainThreadHandle);\r
+    gWinNt->LeaveCriticalSection (&mNtCriticalSection);\r
   } else {\r
     gWinNt->timeKillEvent (wTimerID);\r
     mMMTimerThreadID = 0;\r
@@ -536,7 +536,9 @@ Returns:
   EFI_STATUS  Status;\r
   UINTN       Result;\r
   EFI_HANDLE  Handle;\r
-\r
+  EFI_HANDLE  hSourceProcessHandle;\r
+  EFI_HANDLE  hSourceHandle;\r
+  EFI_HANDLE  hTargetProcessHandle;\r
   //\r
   // Make sure the Timer Architectural Protocol is not already installed in the system\r
   //\r
@@ -545,16 +547,19 @@ Returns:
   //\r
   // Get the CPU Architectural Protocol instance\r
   //\r
-  Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, &mCpu);\r
+  Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID**)&mCpu);\r
   ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
   //  Get our handle so the timer tick thread can suspend\r
   //\r
+  hSourceProcessHandle = gWinNt->GetCurrentProcess ();\r
+  hSourceHandle        = gWinNt->GetCurrentThread ();\r
+  hTargetProcessHandle = gWinNt->GetCurrentProcess ();\r
   Result = gWinNt->DuplicateHandle (\r
-                    gWinNt->GetCurrentProcess (),\r
-                    gWinNt->GetCurrentThread (),\r
-                    gWinNt->GetCurrentProcess (),\r
+                    hSourceProcessHandle,\r
+                    hSourceHandle,\r
+                    hTargetProcessHandle,\r
                     &mNtMainThreadHandle,\r
                     0,\r
                     FALSE,\r