Skip to content

Commit 169f459

Browse files
committed
ExtendedTools: Revert df2c1ba (Remove unused lock)
1 parent a88c3ae commit 169f459

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

plugins/ExtendedTools/etwstat.c

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* ETW statistics collection
44
*
55
* Copyright (C) 2010-2011 wj32
6-
* Copyright (C) 2019-2020 dmex
6+
* Copyright (C) 2019-2022 dmex
77
*
88
* This file is part of Process Hacker.
99
*
@@ -34,10 +34,17 @@ VOID NTAPI EtEtwNetworkItemsUpdatedCallback(
3434
_In_opt_ PVOID Context
3535
);
3636

37+
VOID EtpUpdateProcessInformation(
38+
VOID
39+
);
40+
3741
BOOLEAN EtDiskExtEnabled = FALSE;
3842
static PH_CALLBACK_REGISTRATION EtpProcessesUpdatedCallbackRegistration;
3943
static PH_CALLBACK_REGISTRATION EtpNetworkItemsUpdatedCallbackRegistration;
4044

45+
static PVOID EtpProcessInformation = NULL;
46+
static PH_QUEUED_LOCK EtpProcessInformationLock = PH_QUEUED_LOCK_INIT;
47+
4148
ULONG EtpDiskReadRaw;
4249
ULONG EtpDiskWriteRaw;
4350
ULONG EtpNetworkReceiveRaw;
@@ -396,6 +403,8 @@ VOID NTAPI EtEtwProcessesUpdatedCallback(
396403
// Since Windows 8, we no longer get the correct process/thread IDs in the
397404
// event headers for disk events. We need to update our process information since
398405
// etwmon uses our EtThreadIdToProcessId function. (wj32)
406+
if (PhWindowsVersion >= WINDOWS_8)
407+
EtpUpdateProcessInformation();
399408

400409
// ETW is extremely lazy when it comes to flushing buffers, so we must do it manually. (wj32)
401410
//EtFlushEtwSession();
@@ -548,43 +557,56 @@ VOID NTAPI EtEtwNetworkItemsUpdatedCallback(
548557
}
549558
}
550559

560+
VOID EtpUpdateProcessInformation(
561+
VOID
562+
)
563+
{
564+
PhAcquireQueuedLockExclusive(&EtpProcessInformationLock);
565+
566+
if (EtpProcessInformation)
567+
{
568+
PhFree(EtpProcessInformation);
569+
EtpProcessInformation = NULL;
570+
}
571+
572+
PhEnumProcesses(&EtpProcessInformation);
573+
574+
PhReleaseQueuedLockExclusive(&EtpProcessInformationLock);
575+
}
576+
551577
HANDLE EtThreadIdToProcessId(
552578
_In_ HANDLE ThreadId
553579
)
554580
{
555-
// Note: no lock is needed because we use the list on the same thread (EtpEtwMonitorThreadStart). (dmex)
556-
static PVOID processInfo = NULL;
557-
static ULONG64 lastTickTotal = 0;
558581
PSYSTEM_PROCESS_INFORMATION process;
559-
ULONG64 tickCount;
582+
ULONG i;
583+
HANDLE processId;
560584

561-
tickCount = NtGetTickCount64();
585+
PhAcquireQueuedLockShared(&EtpProcessInformationLock);
562586

563-
if (tickCount - lastTickTotal >= 2 * CLOCKS_PER_SEC)
587+
if (!EtpProcessInformation)
564588
{
565-
lastTickTotal = tickCount;
566-
567-
if (processInfo)
568-
{
569-
PhFree(processInfo);
570-
processInfo = NULL;
571-
}
572-
573-
PhEnumProcesses(&processInfo);
589+
PhReleaseQueuedLockShared(&EtpProcessInformationLock);
590+
return SYSTEM_PROCESS_ID;
574591
}
575592

576-
process = PH_FIRST_PROCESS(processInfo);
593+
process = PH_FIRST_PROCESS(EtpProcessInformation);
577594

578595
do
579596
{
580-
for (ULONG i = 0; i < process->NumberOfThreads; i++)
597+
for (i = 0; i < process->NumberOfThreads; i++)
581598
{
582599
if (process->Threads[i].ClientId.UniqueThread == ThreadId)
583600
{
584-
return process->UniqueProcessId;
601+
processId = process->UniqueProcessId;
602+
PhReleaseQueuedLockShared(&EtpProcessInformationLock);
603+
604+
return processId;
585605
}
586606
}
587607
} while (process = PH_NEXT_PROCESS(process));
588608

609+
PhReleaseQueuedLockShared(&EtpProcessInformationLock);
610+
589611
return SYSTEM_PROCESS_ID;
590612
}

0 commit comments

Comments
 (0)