Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MrPowerGamerBR/0df28e30b21477363764f8fb84ec489c to your computer and use it in GitHub Desktop.

Select an option

Save MrPowerGamerBR/0df28e30b21477363764f8fb84ec489c to your computer and use it in GitHub Desktop.
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrPowerGamerBR <git@mrpowergamerbr.com>
Date: Thu, 15 Jan 2026 14:30:28 -0300
Subject: Implement SetThreadpoolTimerEx
You should NOT use this patch in production, this was generated by Claude Code (Sonnet 4.5) and is meant to see how far you can make Creative Cloud work with it.
---
dlls/kernel32/kernel32.spec | 1 +
dlls/kernelbase/kernelbase.spec | 2 +-
dlls/ntdll/ntdll.spec | 1 +
dlls/ntdll/threadpool.c | 85 +++++++++++++++++++++++++++++++++
include/threadpoolapiset.h | 1 +
include/winternl.h | 1 +
6 files changed, 90 insertions(+), 1 deletion(-)
diff --git a/dlls/kernel32/kernel32.spec b/dlls/kernel32/kernel32.spec
index 5c2501c3402..0fddfaab70d 100644
--- a/dlls/kernel32/kernel32.spec
+++ b/dlls/kernel32/kernel32.spec
@@ -1508,6 +1508,7 @@
@ stdcall SetThreadpoolThreadMaximum(ptr long) NTDLL.TpSetPoolMaxThreads
@ stdcall SetThreadpoolThreadMinimum(ptr long) NTDLL.TpSetPoolMinThreads
@ stdcall SetThreadpoolTimer(ptr ptr long long) NTDLL.TpSetTimer
+@ stdcall SetThreadpoolTimerEx(ptr ptr long long) NTDLL.TpSetTimerEx
@ stdcall SetThreadpoolWait(ptr long ptr) NTDLL.TpSetWait
@ stdcall -import SetTimeZoneInformation(ptr)
@ stub SetTimerQueueTimer
diff --git a/dlls/kernelbase/kernelbase.spec b/dlls/kernelbase/kernelbase.spec
index 28c5376e4cd..678b7bf14ea 100644
--- a/dlls/kernelbase/kernelbase.spec
+++ b/dlls/kernelbase/kernelbase.spec
@@ -1546,7 +1546,7 @@
@ stdcall SetThreadpoolThreadMaximum(ptr long) ntdll.TpSetPoolMaxThreads
@ stdcall SetThreadpoolThreadMinimum(ptr long) ntdll.TpSetPoolMinThreads
@ stdcall SetThreadpoolTimer(ptr ptr long long) ntdll.TpSetTimer
-# @ stub SetThreadpoolTimerEx
+@ stdcall SetThreadpoolTimerEx(ptr ptr long long) ntdll.TpSetTimerEx
@ stdcall SetThreadpoolWait(ptr long ptr) ntdll.TpSetWait
# @ stub SetThreadpoolWaitEx
@ stdcall SetTimeZoneInformation(ptr)
diff --git a/dlls/ntdll/ntdll.spec b/dlls/ntdll/ntdll.spec
index c8cd2d34595..207d340c3ae 100644
--- a/dlls/ntdll/ntdll.spec
+++ b/dlls/ntdll/ntdll.spec
@@ -1189,6 +1189,7 @@
@ stdcall TpSetPoolMinThreads(ptr long)
@ stdcall TpSetPoolStackInformation(ptr ptr)
@ stdcall TpSetTimer(ptr ptr long long)
+@ stdcall TpSetTimerEx(ptr ptr long long)
@ stdcall TpSetWait(ptr long ptr)
@ stdcall TpSimpleTryPost(ptr ptr ptr)
@ stdcall TpStartAsyncIoOperation(ptr)
diff --git a/dlls/ntdll/threadpool.c b/dlls/ntdll/threadpool.c
index 64a25694467..b000f37a258 100644
--- a/dlls/ntdll/threadpool.c
+++ b/dlls/ntdll/threadpool.c
@@ -3068,6 +3068,91 @@ VOID WINAPI TpSetTimer( TP_TIMER *timer, LARGE_INTEGER *timeout, LONG period, LO
tp_object_submit( this, FALSE );
}
+/***********************************************************************
+ * TpSetTimerEx (NTDLL.@)
+ */
+BOOL WINAPI TpSetTimerEx( TP_TIMER *timer, LARGE_INTEGER *timeout, LONG period, LONG window_length )
+{
+ struct threadpool_object *this = impl_from_TP_TIMER( timer );
+ struct threadpool_object *other_timer;
+ BOOL submit_timer = FALSE;
+ BOOL was_pending;
+ ULONGLONG timestamp;
+
+ TRACE( "%p %p %lu %lu\n", timer, timeout, period, window_length );
+
+ RtlEnterCriticalSection( &timerqueue.cs );
+
+ assert( this->u.timer.timer_initialized );
+
+ /* Capture the previous pending state before modifications */
+ was_pending = this->u.timer.timer_pending;
+
+ this->u.timer.timer_set = timeout != NULL;
+
+ /* Convert relative timeout to absolute timestamp and handle a timeout
+ * of zero, which means that the timer is submitted immediately. */
+ if (timeout)
+ {
+ timestamp = timeout->QuadPart;
+ if ((LONGLONG)timestamp < 0)
+ {
+ LARGE_INTEGER now;
+ NtQuerySystemTime( &now );
+ timestamp = now.QuadPart - timestamp;
+ }
+ else if (!timestamp)
+ {
+ if (!period)
+ timeout = NULL;
+ else
+ {
+ LARGE_INTEGER now;
+ NtQuerySystemTime( &now );
+ timestamp = now.QuadPart + (ULONGLONG)period * 10000;
+ }
+ submit_timer = TRUE;
+ }
+ }
+
+ /* First remove existing timeout. */
+ if (this->u.timer.timer_pending)
+ {
+ list_remove( &this->u.timer.timer_entry );
+ this->u.timer.timer_pending = FALSE;
+ }
+
+ /* If the timer was enabled, then add it back to the queue. */
+ if (timeout)
+ {
+ this->u.timer.timeout = timestamp;
+ this->u.timer.period = period;
+ this->u.timer.window_length = window_length;
+
+ LIST_FOR_EACH_ENTRY( other_timer, &timerqueue.pending_timers,
+ struct threadpool_object, u.timer.timer_entry )
+ {
+ assert( other_timer->type == TP_OBJECT_TYPE_TIMER );
+ if (this->u.timer.timeout < other_timer->u.timer.timeout)
+ break;
+ }
+ list_add_before( &other_timer->u.timer.timer_entry, &this->u.timer.timer_entry );
+
+ /* Wake up the timer thread when the timeout has to be updated. */
+ if (list_head( &timerqueue.pending_timers ) == &this->u.timer.timer_entry )
+ RtlWakeAllConditionVariable( &timerqueue.update_event );
+
+ this->u.timer.timer_pending = TRUE;
+ }
+
+ RtlLeaveCriticalSection( &timerqueue.cs );
+
+ if (submit_timer)
+ tp_object_submit( this, FALSE );
+
+ return was_pending;
+}
+
/***********************************************************************
* TpSetWait (NTDLL.@)
*/
diff --git a/include/threadpoolapiset.h b/include/threadpoolapiset.h
index 73203f413ee..0b6f1d27362 100644
--- a/include/threadpoolapiset.h
+++ b/include/threadpoolapiset.h
@@ -56,6 +56,7 @@ WINBASEAPI BOOL WINAPI SetThreadpoolStackInformation(PTP_POOL,PTP_POOL_ST
WINBASEAPI void WINAPI SetThreadpoolThreadMaximum(PTP_POOL,DWORD);
WINBASEAPI BOOL WINAPI SetThreadpoolThreadMinimum(PTP_POOL,DWORD);
WINBASEAPI void WINAPI SetThreadpoolTimer(PTP_TIMER,FILETIME*,DWORD,DWORD);
+WINBASEAPI BOOL WINAPI SetThreadpoolTimerEx(PTP_TIMER,FILETIME*,DWORD,DWORD);
WINBASEAPI void WINAPI SetThreadpoolWait(PTP_WAIT,HANDLE,FILETIME *);
WINBASEAPI void WINAPI StartThreadpoolIo(TP_IO*);
WINBASEAPI void WINAPI SubmitThreadpoolWork(PTP_WORK);
diff --git a/include/winternl.h b/include/winternl.h
index bdfaa0779fb..93986451d2b 100644
--- a/include/winternl.h
+++ b/include/winternl.h
@@ -5287,6 +5287,7 @@ NTSYSAPI void WINAPI TpSetPoolMaxThreads(TP_POOL *,DWORD);
NTSYSAPI BOOL WINAPI TpSetPoolMinThreads(TP_POOL *,DWORD);
NTSYSAPI NTSTATUS WINAPI TpSetPoolStackInformation(TP_POOL *, TP_POOL_STACK_INFORMATION *stack_info);
NTSYSAPI void WINAPI TpSetTimer(TP_TIMER *, LARGE_INTEGER *,LONG,LONG);
+NTSYSAPI BOOL WINAPI TpSetTimerEx(TP_TIMER *, LARGE_INTEGER *,LONG,LONG);
NTSYSAPI void WINAPI TpSetWait(TP_WAIT *,HANDLE,LARGE_INTEGER *);
NTSYSAPI NTSTATUS WINAPI TpSimpleTryPost(PTP_SIMPLE_CALLBACK,PVOID,TP_CALLBACK_ENVIRON *);
NTSYSAPI void WINAPI TpStartAsyncIoOperation(TP_IO *);
--
2.52.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment