Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions ports/win32/vs_2019/src/tx_initialize_low_level.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ volatile LONG _tx_win32_exiting = 0;
UINT _tx_win32_timer_id;
VOID CALLBACK _tx_win32_timer_interrupt(UINT wTimerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);
static VOID _tx_win32_timer_start(VOID);
static VOID _tx_win32_timer_due_set(LONGLONG period_ms, LARGE_INTEGER *due_time);
static LONGLONG _tx_win32_timer_deadline;
static LONGLONG _tx_win32_timer_frequency;
static DWORD WINAPI _tx_win32_timer_thread_entry(LPVOID thread_input);


Expand Down Expand Up @@ -486,8 +489,7 @@ static VOID _tx_win32_timer_start(VOID)
LARGE_INTEGER due_time;


/* Rearm the host timer relative to "now" to avoid burst catch-up ticks. */
due_time.QuadPart = -(((LONGLONG) TX_TIMER_PERIODIC) * 10000LL);
_tx_win32_timer_due_set((LONGLONG) TX_TIMER_PERIODIC, &due_time);
#if (TX_WIN32_USE_HIGH_RESOLUTION_TIMER != 0)
if (SetWaitableTimerEx(_tx_win32_timer_handle, &due_time, 0, NULL, NULL, NULL, 0) == 0)
#else
Expand All @@ -500,3 +502,42 @@ LARGE_INTEGER due_time;
}
}
}


/* Advances the timer deadline by one period and returns the relative due time to
it, so that interrupt latency does not accumulate into the period. */
static VOID _tx_win32_timer_due_set(LONGLONG period_ms, LARGE_INTEGER *due_time)
{

LARGE_INTEGER now;
LONGLONG period;
LONGLONG remaining;


if (_tx_win32_timer_frequency == 0LL)
{
QueryPerformanceFrequency(&now);
_tx_win32_timer_frequency = now.QuadPart;
}

QueryPerformanceCounter(&now);
period = (period_ms * _tx_win32_timer_frequency) / 1000LL;
_tx_win32_timer_deadline += period;

/* Resynchronize instead of bursting when more than a period late, and when an
early interrupt has left the deadline more than a period ahead. */
if ((_tx_win32_timer_deadline <= (now.QuadPart - period)) ||
(_tx_win32_timer_deadline > (now.QuadPart + period)))
{
_tx_win32_timer_deadline = now.QuadPart + period;
}

/* Convert to a negative, relative due time in 100 ns units; a deadline
already reached fires at once. */
remaining = ((_tx_win32_timer_deadline - now.QuadPart) * 10000000LL) / _tx_win32_timer_frequency;
if (remaining < 1LL)
{
remaining = 1LL;
}
due_time -> QuadPart = -remaining;
}
45 changes: 43 additions & 2 deletions ports/win64/vs_2022/src/tx_initialize_low_level.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ HANDLE _tx_win32_timer_kick_event;
UINT _tx_win32_timer_id;
VOID CALLBACK _tx_win32_timer_interrupt(UINT wTimerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);
static VOID _tx_win32_timer_start(VOID);
static VOID _tx_win32_timer_due_set(LONGLONG period_ms, LARGE_INTEGER *due_time);
static LONGLONG _tx_win32_timer_deadline;
static LONGLONG _tx_win32_timer_frequency;
static DWORD WINAPI _tx_win32_timer_thread_entry(LPVOID thread_input);


Expand Down Expand Up @@ -437,8 +440,7 @@ static VOID _tx_win32_timer_start(VOID)
LARGE_INTEGER due_time;


/* Rearm the host timer relative to "now" to avoid burst catch-up ticks. */
due_time.QuadPart = -(((LONGLONG) TX_TIMER_PERIODIC) * 10000LL);
_tx_win32_timer_due_set((LONGLONG) TX_TIMER_PERIODIC, &due_time);
#if (TX_WIN32_USE_HIGH_RESOLUTION_TIMER != 0)
if (SetWaitableTimerEx(_tx_win32_timer_handle, &due_time, 0, NULL, NULL, NULL, 0) == 0)
#else
Expand All @@ -451,3 +453,42 @@ LARGE_INTEGER due_time;
}
}
}


/* Advances the timer deadline by one period and returns the relative due time to
it, so that interrupt latency does not accumulate into the period. */
static VOID _tx_win32_timer_due_set(LONGLONG period_ms, LARGE_INTEGER *due_time)
{

LARGE_INTEGER now;
LONGLONG period;
LONGLONG remaining;


if (_tx_win32_timer_frequency == 0LL)
{
QueryPerformanceFrequency(&now);
_tx_win32_timer_frequency = now.QuadPart;
}

QueryPerformanceCounter(&now);
period = (period_ms * _tx_win32_timer_frequency) / 1000LL;
_tx_win32_timer_deadline += period;

/* Resynchronize instead of bursting when more than a period late, and when an
early interrupt has left the deadline more than a period ahead. */
if ((_tx_win32_timer_deadline <= (now.QuadPart - period)) ||
(_tx_win32_timer_deadline > (now.QuadPart + period)))
{
_tx_win32_timer_deadline = now.QuadPart + period;
}

/* Convert to a negative, relative due time in 100 ns units; a deadline
already reached fires at once. */
remaining = ((_tx_win32_timer_deadline - now.QuadPart) * 10000000LL) / _tx_win32_timer_frequency;
if (remaining < 1LL)
{
remaining = 1LL;
}
due_time -> QuadPart = -remaining;
}
44 changes: 43 additions & 1 deletion ports_smp/win64/vs_2022/src/tx_initialize_low_level.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ __declspec(thread) int _tx_win32_threadx_thread = 0;
__declspec(thread) UINT _tx_win32_current_virtual_core = 0U;

static VOID _tx_win32_timer_start(VOID);
static VOID _tx_win32_timer_due_set(LONGLONG period_ms, LARGE_INTEGER *due_time);
static LONGLONG _tx_win32_timer_deadline;
static LONGLONG _tx_win32_timer_frequency;
static DWORD WINAPI _tx_win32_timer_thread_entry(LPVOID thread_input);
static UINT _tx_win32_smp_current_core_get_internal(DWORD thread_id);
static UINT _tx_win32_smp_thread_core_get(DWORD thread_id);
Expand Down Expand Up @@ -1134,7 +1137,7 @@ LONGLONG timer_period;
timer_period = (LONGLONG) TX_TIMER_PERIODIC;
}

due_time.QuadPart = -(timer_period * 10000LL);
_tx_win32_timer_due_set(timer_period, &due_time);
#if (TX_WIN32_USE_HIGH_RESOLUTION_TIMER != 0)
if (SetWaitableTimerEx(_tx_win32_timer_handle, &due_time, 0, NULL, NULL, NULL, 0) == 0)
#else
Expand All @@ -1147,3 +1150,42 @@ LONGLONG timer_period;
}
}
}


/* Advances the timer deadline by one period and returns the relative due time to
it, so that interrupt latency does not accumulate into the period. */
static VOID _tx_win32_timer_due_set(LONGLONG period_ms, LARGE_INTEGER *due_time)
{

LARGE_INTEGER now;
LONGLONG period;
LONGLONG remaining;


if (_tx_win32_timer_frequency == 0LL)
{
QueryPerformanceFrequency(&now);
_tx_win32_timer_frequency = now.QuadPart;
}

QueryPerformanceCounter(&now);
period = (period_ms * _tx_win32_timer_frequency) / 1000LL;
_tx_win32_timer_deadline += period;

/* Resynchronize instead of bursting when more than a period late, and when an
early interrupt has left the deadline more than a period ahead. */
if ((_tx_win32_timer_deadline <= (now.QuadPart - period)) ||
(_tx_win32_timer_deadline > (now.QuadPart + period)))
{
_tx_win32_timer_deadline = now.QuadPart + period;
}

/* Convert to a negative, relative due time in 100 ns units; a deadline
already reached fires at once. */
remaining = ((_tx_win32_timer_deadline - now.QuadPart) * 10000000LL) / _tx_win32_timer_frequency;
if (remaining < 1LL)
{
remaining = 1LL;
}
due_time -> QuadPart = -remaining;
}
5 changes: 5 additions & 0 deletions test/smp/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,8 @@ else()
-Waggregate-return
-Wfloat-equal)
endif()

# Port-level tests for the Windows simulator.
if(THREADX_ARCH STREQUAL "win64")
add_subdirectory(windows_port)
endif()
14 changes: 14 additions & 0 deletions test/smp/cmake/windows_port/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Portions of this file were generated with AI assistance.

cmake_minimum_required(VERSION 3.13 FATAL_ERROR)

project(windows_port_test LANGUAGES C)

set(SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../tx/windows_port)

# Port-level tests for the SMP Windows simulator, shared with the single-core tree.
add_executable(tx_win_tick_rate
${SOURCE_DIR}/threadx_windows_tick_rate_test.c
${CMAKE_CURRENT_LIST_DIR}/../samples/fake.c)
target_link_libraries(tx_win_tick_rate PRIVATE azrtos::threadx_smp)
add_test(${CMAKE_BUILD_TYPE}::threadx_windows_tick_rate_test tx_win_tick_rate)
6 changes: 6 additions & 0 deletions test/tx/cmake/windows_port/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ add_executable(tx_win_isr_handoff
${CMAKE_CURRENT_LIST_DIR}/../samples/fake.c)
target_link_libraries(tx_win_isr_handoff PRIVATE azrtos::threadx)
add_test(${CMAKE_BUILD_TYPE}::threadx_windows_isr_handoff_test tx_win_isr_handoff)

add_executable(tx_win_tick_rate
${SOURCE_DIR}/threadx_windows_tick_rate_test.c
${CMAKE_CURRENT_LIST_DIR}/../samples/fake.c)
target_link_libraries(tx_win_tick_rate PRIVATE azrtos::threadx)
add_test(${CMAKE_BUILD_TYPE}::threadx_windows_tick_rate_test tx_win_tick_rate)
93 changes: 93 additions & 0 deletions test/tx/windows_port/threadx_windows_tick_rate_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/***************************************************************************
* Copyright (c) 2026 Eclipse ThreadX contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* AI Disclosure: This file was largely AI-generated by Claude Code (Opus 5.5).
* The AI-generated portions may be considered public domain (CC0-1.0)
* and not subject to the project's licence. The human contributor has
* reviewed and verified that the code is correct.
*
* SPDX-License-Identifier: MIT and CC0-1.0
**************************************************************************/

/* Checks that simulated time keeps pace with the host clock. Each simulated timer
interrupt is TX_TIMER_PERIODIC host milliseconds and TX_WIN32_TICKS_PER_INTERRUPT
ticks, so a sleep of two seconds' worth of ticks must take two host seconds.
Interrupt latency that accumulates into the period makes it take longer. */

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "tx_api.h"


/* The SMP port processes one tick per interrupt. */
#ifndef TX_WIN32_TICKS_PER_INTERRUPT
#define TX_WIN32_TICKS_PER_INTERRUPT 1
#endif

#define TEST_TICKS_PER_SECOND ((TX_WIN32_TICKS_PER_INTERRUPT * 1000UL) / TX_TIMER_PERIODIC)
#define TEST_SLEEP_TICKS (2UL * TEST_TICKS_PER_SECOND)
#define TEST_TOLERANCE_PERCENT 2U


static TX_THREAD test_thread;
static ULONG test_stack[4096 / sizeof(ULONG)];


static VOID test_entry(ULONG input)
{
LARGE_INTEGER frequency;
LARGE_INTEGER start;
LARGE_INTEGER end;
ULONG start_ticks;
ULONG ticks;
double ticks_per_second;
double expected;

(VOID) input;

/* Start on an interrupt boundary. */
tx_thread_sleep(1);

(VOID) QueryPerformanceFrequency(&frequency);
start_ticks = tx_time_get();
(VOID) QueryPerformanceCounter(&start);
tx_thread_sleep(TEST_SLEEP_TICKS);
(VOID) QueryPerformanceCounter(&end);
ticks = tx_time_get() - start_ticks;

ticks_per_second = ((double) ticks * (double) frequency.QuadPart) / (double) (end.QuadPart - start.QuadPart);
expected = (double) TEST_TICKS_PER_SECOND;

if ((ticks_per_second < ((expected * (100U - TEST_TOLERANCE_PERCENT)) / 100.0)) ||
(ticks_per_second > ((expected * (100U + TEST_TOLERANCE_PERCENT)) / 100.0)))
{
printf("Running Windows simulator tick rate test............................ ERROR!\n");
printf(" %.1f ticks/s, expected %.1f\n", ticks_per_second, expected);
exit(1);
}

printf("Running Windows simulator tick rate test............................ SUCCESS!\n");
printf(" %.1f ticks/s, expected %.1f\n", ticks_per_second, expected);
exit(0);
}


VOID tx_application_define(VOID *first_unused_memory)
{
(VOID) first_unused_memory;

(VOID) tx_thread_create(&test_thread, "tick rate", test_entry, 0,
test_stack, sizeof(test_stack), 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
}


int main(void)
{
tx_kernel_enter();
return 0;
}