Driver: Shaper: Refactor timer handling

This commit is contained in:
Nodir Temirkhodjaev 2023-01-09 14:15:05 +03:00
parent 2935dcbb36
commit 35cb45a2a1
3 changed files with 18 additions and 33 deletions

View File

@ -468,18 +468,14 @@ static void fort_shaper_free_queues(PFORT_SHAPER shaper)
}
}
static void fort_shaper_timer_start(PFORT_SHAPER shaper, BOOL force)
static void fort_shaper_timer_start(PFORT_SHAPER shaper)
{
const ULONG active_io_bits = fort_shaper_io_bits(&shaper->active_io_bits);
if (active_io_bits == 0)
return;
PFORT_TIMER timer = &shaper->timer;
timer->period = force ? 0 : 1; /* milliseconds */
fort_timer_set_running(timer, /*run=*/TRUE);
fort_timer_set_running(&shaper->timer, /*run=*/TRUE);
}
static void NTAPI fort_shaper_timer_process(void)
@ -509,7 +505,7 @@ static void NTAPI fort_shaper_timer_process(void)
if (new_active_io_bits != 0) {
fort_shaper_io_bits_set(&shaper->active_io_bits, new_active_io_bits, TRUE);
fort_shaper_timer_start(shaper, /*force=*/FALSE);
fort_shaper_timer_start(shaper);
}
}
@ -719,7 +715,7 @@ static NTSTATUS fort_shaper_packet_queue(PFORT_SHAPER shaper,
fort_shaper_io_bits_set(&shaper->active_io_bits, queue_bit, TRUE);
/* Start the Timer */
fort_shaper_timer_start(shaper, /*force=*/TRUE);
fort_shaper_timer_start(shaper);
return STATUS_SUCCESS;
}

View File

@ -40,17 +40,11 @@ FORT_API void fort_timer_open(
{
timer->period = period;
timer->callback = callback;
timer->flags = flags;
KeInitializeDpc(&timer->dpc,
FORT_CALLBACK(FORT_TIMER_CALLBACK, PKDEFERRED_ROUTINE, &fort_timer_callback), timer);
KeInitializeTimer(&timer->id);
if ((flags & FORT_TIMER_RUNNING) == 0) {
timer->flags = flags;
} else {
timer->flags = 0;
fort_timer_update(timer, flags);
}
}
FORT_API void fort_timer_close(PFORT_TIMER timer)
@ -63,30 +57,30 @@ FORT_API void fort_timer_close(PFORT_TIMER timer)
KeFlushQueuedDpcs();
}
FORT_API void fort_timer_update(PFORT_TIMER timer, UCHAR flags)
FORT_API BOOL fort_timer_is_running(PFORT_TIMER timer)
{
const UCHAR old_flags = fort_timer_flags_exchange(timer, flags);
const UCHAR flags = fort_timer_flags(timer);
return (flags & FORT_TIMER_RUNNING) != 0;
}
const UCHAR run = (flags & FORT_TIMER_RUNNING);
if ((old_flags & FORT_TIMER_RUNNING) == run)
void fort_timer_set_running(PFORT_TIMER timer, BOOL run)
{
const UCHAR flags = fort_timer_flags_set(timer, FORT_TIMER_RUNNING, run);
const BOOL was_run = (flags & FORT_TIMER_RUNNING) != 0;
if (run == was_run)
return;
if (run != 0) {
if (run) {
const ULONG period = timer->period;
const ULONG interval = (flags & FORT_TIMER_ONESHOT) != 0 ? 0 : period;
const ULONG delay = (flags & FORT_TIMER_COALESCABLE) != 0 ? 500 : 0;
LARGE_INTEGER due;
due.QuadPart = period * -10000; /* ms -> us */
due.QuadPart = (INT64) period * -10000LL /* ms -> us */;
KeSetCoalescableTimer(&timer->id, due, interval, delay, &timer->dpc);
} else {
KeCancelTimer(&timer->id);
}
}
FORT_API BOOL fort_timer_is_running(PFORT_TIMER timer)
{
const UCHAR flags = fort_timer_flags(timer);
return (flags & FORT_TIMER_RUNNING) != 0;
}

View File

@ -30,14 +30,9 @@ FORT_API void fort_timer_open(
FORT_API void fort_timer_close(PFORT_TIMER timer);
FORT_API void fort_timer_update(PFORT_TIMER timer, UCHAR flags);
FORT_API BOOL fort_timer_is_running(PFORT_TIMER timer);
inline static void fort_timer_set_running(PFORT_TIMER timer, BOOL run)
{
fort_timer_update(timer, (run ? FORT_TIMER_RUNNING : 0));
}
FORT_API void fort_timer_set_running(PFORT_TIMER timer, BOOL run);
#ifdef __cplusplus
} // extern "C"