From 73e69a62d3983bc273ee7bf6dd6a2139dc9b7855 Mon Sep 17 00:00:00 2001 From: Alex Mendoza <182437269+MuerteSeguraZ@users.noreply.github.com> Date: Tue, 28 Jul 2026 12:37:25 +0200 Subject: [PATCH] [KERNEL32] Make creation of a default timer queue thread safe --- dll/win32/kernel32/client/timerqueue.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/dll/win32/kernel32/client/timerqueue.c b/dll/win32/kernel32/client/timerqueue.c index 5a24a258b9f..83656880ba4 100644 --- a/dll/win32/kernel32/client/timerqueue.c +++ b/dll/win32/kernel32/client/timerqueue.c @@ -19,15 +19,19 @@ HANDLE BasepDefaultTimerQueue = NULL; /* PRIVATE FUNCTIONS **********************************************************/ -/* FIXME - make this thread safe */ BOOL WINAPI BasepCreateDefaultTimerQueue(VOID) { NTSTATUS Status; + HANDLE TimerQueue; + + /* Already initialized */ + if (BasepDefaultTimerQueue != NULL) + return TRUE; /* Create the timer queue */ - Status = RtlCreateTimerQueue(&BasepDefaultTimerQueue); + Status = RtlCreateTimerQueue(&TimerQueue); if (!NT_SUCCESS(Status)) { BaseSetLastNTError(Status); @@ -35,6 +39,14 @@ BasepCreateDefaultTimerQueue(VOID) return FALSE; } + /* Try to publish it. If another thread already did, throw ours away */ + if (InterlockedCompareExchangePointer((PVOID *)&BasepDefaultTimerQueue, + TimerQueue, + NULL) != NULL) + { + RtlDeleteTimerQueueEx(TimerQueue, NULL); + } + return TRUE; }