From 88f2b62b87e81ebefa28c1df52c1fcbb60fb840a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Desbiens?= Date: Wed, 4 Nov 2026 21:55:11 +0100 Subject: [PATCH 1/2] Fixed Windows simulator stalls and restored host-clock tick rate Fixes #832 The Windows simulator rework in 6.5.2.202603 changed the default tick rate and the way a simulated interrupt hands over to the thread it wakes. The tick rate went from 1 tick per 10 ms to 5 ticks per 1 ms, so simulated time ran 5 times faster than TX_TIMER_PERIODIC implied, or 50 times at the defaults. The handover had a single waiter. When a second host thread simulates interrupts, as the GUIX Win32 driver does, both threads wait and spin in SwitchToThread at a priority above the ThreadX threads. The woken thread never runs, and the tick count advances while no thread is scheduled. The win32 and win64 ports now default to TX_TIMER_PERIODIC 10 and TX_WIN32_TICKS_PER_INTERRUPT 1. The regression suite sets the previous fast values itself. The scheduler counts dispatches. A waiting interrupt spins briefly and then blocks in WaitOnAddress until that count changes or the thread it woke is no longer next to run. Any number of interrupt threads can therefore wait at once. threadx_windows_isr_handoff_test, which adds a second interrupt thread, passes 0/5 on dev and 20/20 with the fix, on both win32 and win64. With TX_TIMER_PERIODIC 5, simulated time runs at 962 ticks/s on dev and 191 with the fix. The regression suite passes in all ten configurations, apart from threadx_thread_wait_abort_and_isr_test. That test also times out on dev, where it passes 10/40 standalone against 8/40 with the fix. Assisted-by: Claude Code (Opus 5.5) --- ports/win32/vs_2019/inc/tx_port.h | 14 +- .../vs_2019/src/tx_initialize_low_level.c | 10 +- .../vs_2019/src/tx_thread_context_restore.c | 74 +++++--- ports/win32/vs_2019/src/tx_thread_schedule.c | 25 ++- ports/win64/vs_2022/inc/tx_port.h | 23 ++- .../vs_2022/src/tx_initialize_low_level.c | 12 +- .../vs_2022/src/tx_thread_context_restore.c | 73 +++++--- ports/win64/vs_2022/src/tx_thread_schedule.c | 24 ++- test/tx/cmake/CMakeLists.txt | 11 ++ test/tx/cmake/windows_port/CMakeLists.txt | 16 ++ .../threadx_windows_isr_handoff_test.c | 176 ++++++++++++++++++ 11 files changed, 343 insertions(+), 115 deletions(-) create mode 100644 test/tx/cmake/windows_port/CMakeLists.txt create mode 100644 test/tx/windows_port/threadx_windows_isr_handoff_test.c diff --git a/ports/win32/vs_2019/inc/tx_port.h b/ports/win32/vs_2019/inc/tx_port.h index 113936aa8..47ae32735 100644 --- a/ports/win32/vs_2019/inc/tx_port.h +++ b/ports/win32/vs_2019/inc/tx_port.h @@ -500,9 +500,9 @@ extern LARGE_INTEGER _tx_win32_time_stamp; extern ULONG _tx_win32_system_error; extern HANDLE _tx_win32_timer_handle; extern HANDLE _tx_win32_timer_thread_handle; -extern HANDLE _tx_win32_isr_semaphore; extern UINT _tx_win32_timer_id; -extern UINT _tx_win32_timer_waiting; +extern UINT _tx_win32_isr_waiting_count; +extern volatile LONG _tx_win32_dispatch_count; #ifdef TX_WIN32_NO_IDLE_ENABLE extern HANDLE _tx_win32_timer_kick_event; #endif @@ -522,12 +522,20 @@ VOID _tx_win32_scheduler_wake(VOID); #define TX_WIN32_MEMORY_SIZE 256000 #endif +/* Host milliseconds between simulated timer interrupts. The default keeps + simulated time on the host clock at the default 100 ticks per second. */ #ifndef TX_TIMER_PERIODIC #ifdef TX_WIN32_SLOW_TIMER #define TX_TIMER_PERIODIC TX_WIN32_SLOW_TIMER #else -#define TX_TIMER_PERIODIC 1 +#define TX_TIMER_PERIODIC 10 #endif #endif +/* ThreadX ticks processed per simulated timer interrupt. Values above one + run simulated time that many times faster than the host clock. */ +#ifndef TX_WIN32_TICKS_PER_INTERRUPT +#define TX_WIN32_TICKS_PER_INTERRUPT 1 +#endif + #endif diff --git a/ports/win32/vs_2019/src/tx_initialize_low_level.c b/ports/win32/vs_2019/src/tx_initialize_low_level.c index 82bee7426..10898a28c 100644 --- a/ports/win32/vs_2019/src/tx_initialize_low_level.c +++ b/ports/win32/vs_2019/src/tx_initialize_low_level.c @@ -44,8 +44,8 @@ LARGE_INTEGER _tx_win32_time_stamp; ULONG _tx_win32_system_error; HANDLE _tx_win32_timer_handle; HANDLE _tx_win32_timer_thread_handle; -HANDLE _tx_win32_isr_semaphore; -UINT _tx_win32_timer_waiting; +UINT _tx_win32_isr_waiting_count; +volatile LONG _tx_win32_dispatch_count; extern TX_THREAD *_tx_thread_current_ptr; #ifdef TX_WIN32_NO_IDLE_ENABLE @@ -259,7 +259,6 @@ VOID _tx_initialize_low_level(VOID) /* Create the semaphore that regulates when the scheduler executes. */ _tx_win32_scheduler_semaphore = CreateSemaphore(NULL, 0, 1, NULL); - _tx_win32_isr_semaphore = CreateSemaphore(NULL, 0, 1, NULL); /* Create the event that wakes the scheduler whenever the ready state changes. */ _tx_win32_scheduler_wake_event = CreateEvent(NULL, FALSE, FALSE, NULL); @@ -287,7 +286,7 @@ VOID _tx_initialize_low_level(VOID) /* Initialize the global interrupt disabled flag. */ _tx_win32_global_int_disabled_flag = TX_FALSE; - _tx_win32_timer_waiting = TX_FALSE; + _tx_win32_isr_waiting_count = 0; /* Done, return to caller. */ } @@ -422,9 +421,6 @@ VOID CALLBACK _tx_win32_timer_interrupt(UINT wTimerID, UINT msg, DWORD_PTR dwUse relative ordering of thread wakeups is preserved because each call to _tx_timer_interrupt() advances the tick counter by exactly one step and processes the timers that expire at that step. */ -#ifndef TX_WIN32_TICKS_PER_INTERRUPT -#define TX_WIN32_TICKS_PER_INTERRUPT 5 -#endif { UINT _tick_i; for (_tick_i = 0; _tick_i < TX_WIN32_TICKS_PER_INTERRUPT; _tick_i++) diff --git a/ports/win32/vs_2019/src/tx_thread_context_restore.c b/ports/win32/vs_2019/src/tx_thread_context_restore.c index 215489c9e..af51029ae 100644 --- a/ports/win32/vs_2019/src/tx_thread_context_restore.c +++ b/ports/win32/vs_2019/src/tx_thread_context_restore.c @@ -31,6 +31,10 @@ #include "tx_api.h" #include "tx_thread.h" #include "tx_timer.h" +#pragma comment (lib, "Synchronization.lib") + + +static VOID _tx_win32_dispatch_wait(TX_THREAD *execute_thread); /**************************************************************************/ @@ -115,32 +119,18 @@ TX_THREAD *execute_thread; /* Clear the current thread pointer. */ _tx_thread_current_ptr = TX_NULL; - /* Block the timer ISR until the resumed thread has observed the wakeup. */ - _tx_win32_timer_waiting = TX_TRUE; - /* Wakeup the system thread by setting the system semaphore. */ ReleaseSemaphore(_tx_win32_scheduler_semaphore, 1, NULL); _tx_win32_scheduler_wake(); - /* If the timer made a solicited wakeup ready, let that thread run before - the host timer ISR continues. */ + /* If this interrupt made a solicited wakeup ready, let that thread run + before the interrupt returns. */ if ((execute_thread != TX_NULL) && (execute_thread -> tx_thread_win32_suspension_type == 0)) { - /* Spin-poll for the scheduler to complete the solicited wakeup - before the timer ISR proceeds. */ - _tx_win32_critical_section_release_all(&_tx_win32_critical_section); - while (WaitForSingleObject(_tx_win32_isr_semaphore, 0) != WAIT_OBJECT_0) - SwitchToThread(); - _tx_win32_critical_section_obtain(&_tx_win32_critical_section); - while (WaitForSingleObject(_tx_win32_isr_semaphore, 0) == WAIT_OBJECT_0) - { - } + _tx_win32_dispatch_wait(execute_thread); } - - /* The timer ISR no longer needs to hold off future ticks. */ - _tx_win32_timer_waiting = TX_FALSE; } else { @@ -152,28 +142,50 @@ TX_THREAD *execute_thread; else if ((!_tx_thread_system_state) && (execute_thread != TX_NULL)) { - /* The timer made a thread ready while the scheduler was idle. Keep the - timer ISR blocked until the solicited wakeup has started running. */ - _tx_win32_timer_waiting = TX_TRUE; + /* This interrupt made a thread ready while the scheduler was idle. Hold + the interrupt until a solicited wakeup has started running. */ _tx_win32_scheduler_wake(); if (execute_thread -> tx_thread_win32_suspension_type == 0) { - /* Spin-poll for the scheduler to hand off to the next thread and - acknowledge via the ISR semaphore. Keeps timer-path latency low. */ - _tx_win32_critical_section_release_all(&_tx_win32_critical_section); - while (WaitForSingleObject(_tx_win32_isr_semaphore, 0) != WAIT_OBJECT_0) - SwitchToThread(); - _tx_win32_critical_section_obtain(&_tx_win32_critical_section); - while (WaitForSingleObject(_tx_win32_isr_semaphore, 0) == WAIT_OBJECT_0) - { - } + _tx_win32_dispatch_wait(execute_thread); } - - _tx_win32_timer_waiting = TX_FALSE; } /* Leave Win32 critical section. */ _tx_win32_critical_section_release_all(&_tx_win32_critical_section); } + + +/* Waits, with the ThreadX lock released, until the scheduler dispatches a thread + or the thread this interrupt made ready is no longer the next to run. */ +static VOID _tx_win32_dispatch_wait(TX_THREAD *execute_thread) +{ +LONG dispatch_count; +ULONG spin_count; + + + _tx_win32_isr_waiting_count++; + dispatch_count = _tx_win32_dispatch_count; + _tx_win32_critical_section_release_all(&_tx_win32_critical_section); + + /* Spin briefly, then block, so that several waiting interrupt threads cannot + keep the lower-priority host thread being started off the processor. */ + spin_count = TX_WIN32_HANDOFF_SPIN_COUNT; + while ((_tx_win32_dispatch_count == dispatch_count) && (_tx_thread_execute_ptr == execute_thread)) + { + if (spin_count != 0U) + { + spin_count--; + (VOID) SwitchToThread(); + } + else + { + (VOID) WaitOnAddress(&_tx_win32_dispatch_count, &dispatch_count, sizeof(LONG), 1U); + } + } + + _tx_win32_critical_section_obtain(&_tx_win32_critical_section); + _tx_win32_isr_waiting_count--; +} diff --git a/ports/win32/vs_2019/src/tx_thread_schedule.c b/ports/win32/vs_2019/src/tx_thread_schedule.c index e9dc43d77..8b2b779b7 100644 --- a/ports/win32/vs_2019/src/tx_thread_schedule.c +++ b/ports/win32/vs_2019/src/tx_thread_schedule.c @@ -75,8 +75,6 @@ static VOID _tx_win32_semaphore_reset(HANDLE semaphore_handle); /**************************************************************************/ VOID _tx_thread_schedule(VOID) { -DWORD wait_status; - /* Loop forever. */ @@ -173,20 +171,12 @@ DWORD wait_status; _tx_win32_system_error++; } - /* Let the solicited wakeup reach ThreadX before the timer ISR advances again. */ - if (_tx_win32_timer_waiting) + /* Let the solicited wakeup reach ThreadX before a waiting interrupt returns. */ + if (_tx_win32_isr_waiting_count != 0U) { - /* Wait for the thread to acknowledge the wakeup and then release the ISR. */ - wait_status = WaitForSingleObject(_tx_thread_current_ptr -> tx_thread_win32_thread_start_semaphore, INFINITE); - if (ReleaseSemaphore(_tx_win32_isr_semaphore, 1, NULL) == 0) - { - - /* Increment the system error counter. */ - _tx_win32_system_error++; - } - - if (wait_status != WAIT_OBJECT_0) + /* Wait for the thread to acknowledge the wakeup. */ + if (WaitForSingleObject(_tx_thread_current_ptr -> tx_thread_win32_thread_start_semaphore, INFINITE) != WAIT_OBJECT_0) { /* Increment the system error counter. */ @@ -195,6 +185,13 @@ DWORD wait_status; } } + /* Record the dispatch and release any interrupt waiting for it. */ + _tx_win32_dispatch_count++; + if (_tx_win32_isr_waiting_count != 0U) + { + WakeByAddressAll((PVOID) &_tx_win32_dispatch_count); + } + /* Debug entry. */ _tx_win32_debug_entry_insert("SCHEDULE-self_suspend_sem", __FILE__, __LINE__); diff --git a/ports/win64/vs_2022/inc/tx_port.h b/ports/win64/vs_2022/inc/tx_port.h index e080a74d3..b2c2592ba 100644 --- a/ports/win64/vs_2022/inc/tx_port.h +++ b/ports/win64/vs_2022/inc/tx_port.h @@ -566,8 +566,8 @@ extern LARGE_INTEGER _tx_win32_time_stamp; extern ULONG _tx_win32_system_error; extern HANDLE _tx_win32_timer_handle; extern HANDLE _tx_win32_timer_thread_handle; -extern HANDLE _tx_win32_isr_semaphore; -extern UINT _tx_win32_timer_waiting; +extern UINT _tx_win32_isr_waiting_count; +extern volatile LONG _tx_win32_dispatch_count; #ifdef TX_WIN32_NO_IDLE_ENABLE extern HANDLE _tx_win32_timer_kick_event; #endif @@ -581,19 +581,26 @@ extern LARGE_INTEGER _tx_win32_time_stamp; VOID _tx_win32_scheduler_wake(VOID); -/* This simulation port is not for production use. Run at 1 ms per tick - (10x faster than wall clock at the default 100 ticks/second) so that - regression tests with protocol timeouts complete in a fraction of real - time without changing any tick-count-based test logic. The slower - TX_WIN32_SLOW_TIMER escape hatch is preserved for debugging. */ +#ifndef TX_WIN32_HANDOFF_SPIN_COUNT +#define TX_WIN32_HANDOFF_SPIN_COUNT 64 +#endif + +/* Host milliseconds between simulated timer interrupts. The default keeps + simulated time on the host clock at the default 100 ticks per second. */ #ifndef TX_TIMER_PERIODIC #ifdef TX_WIN32_SLOW_TIMER #define TX_TIMER_PERIODIC TX_WIN32_SLOW_TIMER #else -#define TX_TIMER_PERIODIC 1 +#define TX_TIMER_PERIODIC 10 #endif #endif +/* ThreadX ticks processed per simulated timer interrupt. Values above one + run simulated time that many times faster than the host clock. */ +#ifndef TX_WIN32_TICKS_PER_INTERRUPT +#define TX_WIN32_TICKS_PER_INTERRUPT 1 +#endif + #endif diff --git a/ports/win64/vs_2022/src/tx_initialize_low_level.c b/ports/win64/vs_2022/src/tx_initialize_low_level.c index 0bb1f3e3d..ff1e0b104 100644 --- a/ports/win64/vs_2022/src/tx_initialize_low_level.c +++ b/ports/win64/vs_2022/src/tx_initialize_low_level.c @@ -47,8 +47,8 @@ LARGE_INTEGER _tx_win32_time_stamp; ULONG _tx_win32_system_error; HANDLE _tx_win32_timer_handle; HANDLE _tx_win32_timer_thread_handle; -HANDLE _tx_win32_isr_semaphore; -UINT _tx_win32_timer_waiting; +UINT _tx_win32_isr_waiting_count; +volatile LONG _tx_win32_dispatch_count; extern TX_THREAD *_tx_thread_current_ptr; #ifdef TX_WIN32_NO_IDLE_ENABLE @@ -254,8 +254,7 @@ VOID _tx_initialize_low_level(VOID) /* Create the semaphore that regulates when the scheduler executes. */ _tx_win32_scheduler_semaphore = CreateSemaphore(NULL, 0, 1, NULL); - _tx_win32_isr_semaphore = CreateSemaphore(NULL, 0, 1, NULL); - if ((_tx_win32_scheduler_semaphore == NULL) || (_tx_win32_isr_semaphore == NULL)) + if (_tx_win32_scheduler_semaphore == NULL) { printf("ThreadX Win64 error creating semaphores!\n"); while(1) @@ -289,7 +288,7 @@ VOID _tx_initialize_low_level(VOID) /* Initialize the global interrupt disabled flag. */ _tx_win32_global_int_disabled_flag = TX_FALSE; - _tx_win32_timer_waiting = TX_FALSE; + _tx_win32_isr_waiting_count = 0; /* Done, return to caller. */ } @@ -380,9 +379,6 @@ VOID CALLBACK _tx_win32_timer_interrupt(UINT wTimerID, UINT msg, DWORD_PTR dwUse relative ordering of thread wakeups is preserved because each call to _tx_timer_interrupt() advances the tick counter by exactly one step and processes the timers that expire at that step. */ -#ifndef TX_WIN32_TICKS_PER_INTERRUPT -#define TX_WIN32_TICKS_PER_INTERRUPT 5 -#endif { UINT _tick_i; for (_tick_i = 0; _tick_i < TX_WIN32_TICKS_PER_INTERRUPT; _tick_i++) diff --git a/ports/win64/vs_2022/src/tx_thread_context_restore.c b/ports/win64/vs_2022/src/tx_thread_context_restore.c index ae265da1a..189f73daf 100644 --- a/ports/win64/vs_2022/src/tx_thread_context_restore.c +++ b/ports/win64/vs_2022/src/tx_thread_context_restore.c @@ -34,6 +34,10 @@ #include "tx_api.h" #include "tx_thread.h" #include "tx_timer.h" +#pragma comment (lib, "Synchronization.lib") + + +static VOID _tx_win32_dispatch_wait(TX_THREAD *execute_thread); /**************************************************************************/ @@ -118,32 +122,18 @@ TX_THREAD *execute_thread; /* Clear the current thread pointer. */ _tx_thread_current_ptr = TX_NULL; - /* Block the timer ISR until the resumed thread has observed the wakeup. */ - _tx_win32_timer_waiting = TX_TRUE; - /* Wakeup the system thread by setting the system semaphore. */ ReleaseSemaphore(_tx_win32_scheduler_semaphore, 1, NULL); _tx_win32_scheduler_wake(); - /* If the timer made a solicited wakeup ready, let that thread run before - the host timer ISR continues. */ + /* If this interrupt made a solicited wakeup ready, let that thread run + before the interrupt returns. */ if ((execute_thread != TX_NULL) && (execute_thread -> tx_thread_win32_suspension_type == 0)) { - /* Spin-poll for the scheduler to complete the solicited wakeup - before the timer ISR proceeds. */ - _tx_win32_critical_section_release_all(&_tx_win32_critical_section); - while (WaitForSingleObject(_tx_win32_isr_semaphore, 0) != WAIT_OBJECT_0) - SwitchToThread(); - _tx_win32_critical_section_obtain(&_tx_win32_critical_section); - while (WaitForSingleObject(_tx_win32_isr_semaphore, 0) == WAIT_OBJECT_0) - { - } + _tx_win32_dispatch_wait(execute_thread); } - - /* The timer ISR no longer needs to hold off future ticks. */ - _tx_win32_timer_waiting = TX_FALSE; } else { @@ -155,29 +145,50 @@ TX_THREAD *execute_thread; else if ((!_tx_thread_system_state) && (execute_thread != TX_NULL)) { - /* The timer made a thread ready while the scheduler was idle. Keep the - timer ISR blocked until the solicited wakeup has started running. */ - _tx_win32_timer_waiting = TX_TRUE; + /* This interrupt made a thread ready while the scheduler was idle. Hold + the interrupt until a solicited wakeup has started running. */ _tx_win32_scheduler_wake(); if (execute_thread -> tx_thread_win32_suspension_type == 0) { - /* Spin-poll for the scheduler to hand off to the next thread and - acknowledge via the ISR semaphore. Keeps timer-path latency low. */ - _tx_win32_critical_section_release_all(&_tx_win32_critical_section); - while (WaitForSingleObject(_tx_win32_isr_semaphore, 0) != WAIT_OBJECT_0) - SwitchToThread(); - _tx_win32_critical_section_obtain(&_tx_win32_critical_section); - while (WaitForSingleObject(_tx_win32_isr_semaphore, 0) == WAIT_OBJECT_0) - { - } + _tx_win32_dispatch_wait(execute_thread); } - - _tx_win32_timer_waiting = TX_FALSE; } /* Leave Win32 critical section. */ _tx_win32_critical_section_release_all(&_tx_win32_critical_section); } + +/* Waits, with the ThreadX lock released, until the scheduler dispatches a thread + or the thread this interrupt made ready is no longer the next to run. */ +static VOID _tx_win32_dispatch_wait(TX_THREAD *execute_thread) +{ +LONG dispatch_count; +ULONG spin_count; + + + _tx_win32_isr_waiting_count++; + dispatch_count = _tx_win32_dispatch_count; + _tx_win32_critical_section_release_all(&_tx_win32_critical_section); + + /* Spin briefly, then block, so that several waiting interrupt threads cannot + keep the lower-priority host thread being started off the processor. */ + spin_count = TX_WIN32_HANDOFF_SPIN_COUNT; + while ((_tx_win32_dispatch_count == dispatch_count) && (_tx_thread_execute_ptr == execute_thread)) + { + if (spin_count != 0U) + { + spin_count--; + (VOID) SwitchToThread(); + } + else + { + (VOID) WaitOnAddress(&_tx_win32_dispatch_count, &dispatch_count, sizeof(LONG), 1U); + } + } + + _tx_win32_critical_section_obtain(&_tx_win32_critical_section); + _tx_win32_isr_waiting_count--; +} diff --git a/ports/win64/vs_2022/src/tx_thread_schedule.c b/ports/win64/vs_2022/src/tx_thread_schedule.c index 204a1e90c..349f967bc 100644 --- a/ports/win64/vs_2022/src/tx_thread_schedule.c +++ b/ports/win64/vs_2022/src/tx_thread_schedule.c @@ -79,7 +79,6 @@ static VOID _tx_win32_semaphore_reset(HANDLE semaphore_handle); /**************************************************************************/ VOID _tx_thread_schedule(VOID) { -DWORD wait_status; /* Loop forever. */ @@ -176,20 +175,12 @@ DWORD wait_status; _tx_win32_system_error++; } - /* Let the solicited wakeup reach ThreadX before the timer ISR advances again. */ - if (_tx_win32_timer_waiting) + /* Let the solicited wakeup reach ThreadX before a waiting interrupt returns. */ + if (_tx_win32_isr_waiting_count != 0U) { - /* Wait for the thread to acknowledge the wakeup and then release the ISR. */ - wait_status = WaitForSingleObject(_tx_thread_current_ptr -> tx_thread_win32_thread_start_semaphore, INFINITE); - if (ReleaseSemaphore(_tx_win32_isr_semaphore, 1, NULL) == 0) - { - - /* Increment the system error counter. */ - _tx_win32_system_error++; - } - - if (wait_status != WAIT_OBJECT_0) + /* Wait for the thread to acknowledge the wakeup. */ + if (WaitForSingleObject(_tx_thread_current_ptr -> tx_thread_win32_thread_start_semaphore, INFINITE) != WAIT_OBJECT_0) { /* Increment the system error counter. */ @@ -198,6 +189,13 @@ DWORD wait_status; } } + /* Record the dispatch and release any interrupt waiting for it. */ + _tx_win32_dispatch_count++; + if (_tx_win32_isr_waiting_count != 0U) + { + WakeByAddressAll((PVOID) &_tx_win32_dispatch_count); + } + /* Debug entry. */ _tx_win32_debug_entry_insert("SCHEDULE-self_suspend_sem", __FILE__, __LINE__); diff --git a/test/tx/cmake/CMakeLists.txt b/test/tx/cmake/CMakeLists.txt index 7ed620d9c..567ffbd15 100644 --- a/test/tx/cmake/CMakeLists.txt +++ b/test/tx/cmake/CMakeLists.txt @@ -69,6 +69,12 @@ add_compile_definitions( TEST_STACK_SIZE_PRINTF=4096 ${${CMAKE_BUILD_TYPE}}) +# The Windows simulators keep simulated time on the host clock by default. The +# suite runs them at five ticks per millisecond so long timeouts finish quickly. +if((THREADX_ARCH STREQUAL "win32") OR (THREADX_ARCH STREQUAL "win64")) + add_compile_definitions(TX_TIMER_PERIODIC=1 TX_WIN32_TICKS_PER_INTERRUPT=5) +endif() + if(MSVC) add_compile_options(/W3 /Zi) add_link_options(/DEBUG /INCREMENTAL:NO) @@ -167,3 +173,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND THREADX_ARCH STREQUAL "linux" AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|amd64|AMD64)$") add_subdirectory(thread_stack_alignment) endif() + +# Port-level tests for the Windows simulators. +if((THREADX_ARCH STREQUAL "win32") OR (THREADX_ARCH STREQUAL "win64")) + add_subdirectory(windows_port) +endif() diff --git a/test/tx/cmake/windows_port/CMakeLists.txt b/test/tx/cmake/windows_port/CMakeLists.txt new file mode 100644 index 000000000..0891a3a3b --- /dev/null +++ b/test/tx/cmake/windows_port/CMakeLists.txt @@ -0,0 +1,16 @@ +# 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}/../../windows_port) + +# Port-level tests for the Windows simulators. They link the library the regression +# suite builds, and fake.c supplies the test hooks that TX_REGRESSION_TEST compiles +# into it. +add_executable(tx_win_isr_handoff + ${SOURCE_DIR}/threadx_windows_isr_handoff_test.c + ${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) diff --git a/test/tx/windows_port/threadx_windows_isr_handoff_test.c b/test/tx/windows_port/threadx_windows_isr_handoff_test.c new file mode 100644 index 000000000..be019aafb --- /dev/null +++ b/test/tx/windows_port/threadx_windows_isr_handoff_test.c @@ -0,0 +1,176 @@ +/*************************************************************************** + * 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 + **************************************************************************/ + +/* Runs a second simulated interrupt source beside the port's timer, the way the + GUIX Win32 driver and application-defined interrupt threads do. Each source + makes a solicited thread ready from _tx_thread_context_restore, so both wait + for the scheduler's handoff at the same time. The test fails if either + source stops waking its thread before both reach their target counts. */ + +#include +#include +#include "tx_api.h" + + +#define TEST_STACK_SIZE 4096 +#define TEST_ISR_ITERATIONS 1000 +#define TEST_TICK_WAKEUPS 500 +#define TEST_STALL_MS 3000 + + +static TX_THREAD isr_wake_thread; +static TX_THREAD tick_wake_thread; +static TX_THREAD busy_thread; +static TX_SEMAPHORE isr_semaphore; + +static ULONG isr_wake_stack[TEST_STACK_SIZE / sizeof(ULONG)]; +static ULONG tick_wake_stack[TEST_STACK_SIZE / sizeof(ULONG)]; +static ULONG busy_stack[TEST_STACK_SIZE / sizeof(ULONG)]; + +static volatile LONG isr_count; +static volatile LONG isr_wake_count; +static volatile LONG tick_wake_count; +static volatile ULONG busy_count; + +static HANDLE isr_start_event; + +/* Read directly: a stalled port may hold the lock that tx_time_get takes. */ +extern volatile ULONG _tx_timer_system_clock; + + +/* Simulated interrupt: wakes isr_wake_thread from outside ThreadX. */ +static DWORD WINAPI isr_source_entry(LPVOID parameter) +{ + (VOID) parameter; + + (VOID) WaitForSingleObject(isr_start_event, INFINITE); + + while (isr_count < TEST_ISR_ITERATIONS) + { + _tx_thread_context_save(); + (VOID) tx_semaphore_put(&isr_semaphore); + _tx_thread_context_restore(); + + (VOID) InterlockedIncrement(&isr_count); + Sleep(1); + } + + return 0; +} + + +/* Fails the test with the progress counters if either source stalls. */ +static DWORD WINAPI watchdog_entry(LPVOID parameter) +{ +LONG last_isr_wake_count; +LONG last_tick_wake_count; + + (VOID) parameter; + + do + { + last_isr_wake_count = isr_wake_count; + last_tick_wake_count = tick_wake_count; + Sleep(TEST_STALL_MS); + } while (((isr_wake_count != last_isr_wake_count) || (isr_wake_count >= TEST_ISR_ITERATIONS)) && + (tick_wake_count != last_tick_wake_count)); + + printf("Running Windows simulator ISR handoff test.......................... ERROR!\n"); + printf(" ticks=%lu isr=%ld isr_wakeups=%ld tick_wakeups=%ld\n", + _tx_timer_system_clock, isr_count, isr_wake_count, tick_wake_count); + (VOID) fflush(stdout); + (VOID) TerminateProcess(GetCurrentProcess(), 1); + return 0; +} + + +static VOID isr_wake_entry(ULONG input) +{ + (VOID) input; + + while (1) + { + if (tx_semaphore_get(&isr_semaphore, TX_WAIT_FOREVER) == TX_SUCCESS) + { + (VOID) InterlockedIncrement(&isr_wake_count); + } + } +} + + +/* Woken by the timer on every tick; ends the test once both sources are done. */ +static VOID tick_wake_entry(ULONG input) +{ + (VOID) input; + + (VOID) SetEvent(isr_start_event); + + while (1) + { + tx_thread_sleep(1); + (VOID) InterlockedIncrement(&tick_wake_count); + + if ((tick_wake_count >= TEST_TICK_WAKEUPS) && + (isr_count >= TEST_ISR_ITERATIONS) && + (isr_wake_count >= TEST_ISR_ITERATIONS)) + { + printf("Running Windows simulator ISR handoff test.......................... SUCCESS!\n"); + exit(0); + } + } +} + + +/* Keeps a thread running so the timer also takes the preemption path. */ +static VOID busy_entry(ULONG input) +{ + (VOID) input; + + while (1) + { + busy_count++; + } +} + + +VOID tx_application_define(VOID *first_unused_memory) +{ + (VOID) first_unused_memory; + + isr_start_event = CreateEvent(NULL, TRUE, FALSE, NULL); + + (VOID) tx_semaphore_create(&isr_semaphore, "isr semaphore", 0); + (VOID) tx_thread_create(&isr_wake_thread, "isr wake", isr_wake_entry, 0, + isr_wake_stack, sizeof(isr_wake_stack), 5, 5, TX_NO_TIME_SLICE, TX_AUTO_START); + (VOID) tx_thread_create(&tick_wake_thread, "tick wake", tick_wake_entry, 0, + tick_wake_stack, sizeof(tick_wake_stack), 6, 6, TX_NO_TIME_SLICE, TX_AUTO_START); + (VOID) tx_thread_create(&busy_thread, "busy", busy_entry, 0, + busy_stack, sizeof(busy_stack), 20, 20, TX_NO_TIME_SLICE, TX_AUTO_START); + + if ((isr_start_event == NULL) || + (CreateThread(NULL, 0, isr_source_entry, NULL, 0, NULL) == NULL) || + (CreateThread(NULL, 0, watchdog_entry, NULL, 0, NULL) == NULL)) + { + printf("Running Windows simulator ISR handoff test.......................... ERROR!\n"); + exit(1); + } +} + + +int main(void) +{ + tx_kernel_enter(); + return 0; +} From c3084e558464eb218837b7ff8eefc05e9154f4d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Desbiens?= Date: Wed, 4 Nov 2026 23:04:21 +0100 Subject: [PATCH 2/2] Stopped Windows simulator time falling behind the host clock The Win32, Win64 and SMP Win64 timer threads re-armed a one-shot host timer relative to the end of each simulated interrupt, so every interrupt's latency was added to the period. Simulated time ran 37% slow at the regression suite's 1 ms period, 4.5% slow with TX_TIMER_PERIODIC 5, and 4.3% slow at the SMP port's default 10 ms. Each re-arm now advances a deadline, measured with QueryPerformanceCounter, by one period and waits for that deadline. A deadline more than a period late, or more than a period ahead after an idle kick, is reset to one period from now, so missed interrupts are dropped rather than replayed in a burst. This builds on #833, which moved TX_WIN32_TICKS_PER_INTERRUPT into tx_port.h where the new test reads it. threadx_windows_tick_rate_test, with a 2% tolerance, measured 3,163 and 3,184 ticks/s against 5,000 on win32 and win64 before the fix, and 95.7 against 100 on SMP Win64. With the fix it measures 4,999.4 to 4,999.9 and 100.0. With TX_TIMER_PERIODIC 5, both single-core ports now run at 200.0 ticks/s. SMP Win64 passes 119/119 in all five configurations. win32 and win64 pass 106/106 in each configuration except where threadx_thread_wait_abort_and_isr_test times out, which it also does on dev. Assisted-by: Claude Code (Opus 5.5) --- .../vs_2019/src/tx_initialize_low_level.c | 45 ++++++++- .../vs_2022/src/tx_initialize_low_level.c | 45 ++++++++- .../vs_2022/src/tx_initialize_low_level.c | 44 ++++++++- test/smp/cmake/CMakeLists.txt | 5 + test/smp/cmake/windows_port/CMakeLists.txt | 14 +++ test/tx/cmake/windows_port/CMakeLists.txt | 6 ++ .../threadx_windows_tick_rate_test.c | 93 +++++++++++++++++++ 7 files changed, 247 insertions(+), 5 deletions(-) create mode 100644 test/smp/cmake/windows_port/CMakeLists.txt create mode 100644 test/tx/windows_port/threadx_windows_tick_rate_test.c diff --git a/ports/win32/vs_2019/src/tx_initialize_low_level.c b/ports/win32/vs_2019/src/tx_initialize_low_level.c index 10898a28c..f48f7736a 100644 --- a/ports/win32/vs_2019/src/tx_initialize_low_level.c +++ b/ports/win32/vs_2019/src/tx_initialize_low_level.c @@ -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); @@ -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 @@ -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; +} diff --git a/ports/win64/vs_2022/src/tx_initialize_low_level.c b/ports/win64/vs_2022/src/tx_initialize_low_level.c index ff1e0b104..d18d9a67d 100644 --- a/ports/win64/vs_2022/src/tx_initialize_low_level.c +++ b/ports/win64/vs_2022/src/tx_initialize_low_level.c @@ -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); @@ -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 @@ -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; +} diff --git a/ports_smp/win64/vs_2022/src/tx_initialize_low_level.c b/ports_smp/win64/vs_2022/src/tx_initialize_low_level.c index 9752ec3d8..183573b2d 100644 --- a/ports_smp/win64/vs_2022/src/tx_initialize_low_level.c +++ b/ports_smp/win64/vs_2022/src/tx_initialize_low_level.c @@ -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); @@ -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 @@ -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; +} diff --git a/test/smp/cmake/CMakeLists.txt b/test/smp/cmake/CMakeLists.txt index 3a0d9638a..28b1a2664 100644 --- a/test/smp/cmake/CMakeLists.txt +++ b/test/smp/cmake/CMakeLists.txt @@ -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() diff --git a/test/smp/cmake/windows_port/CMakeLists.txt b/test/smp/cmake/windows_port/CMakeLists.txt new file mode 100644 index 000000000..5c0d0a15e --- /dev/null +++ b/test/smp/cmake/windows_port/CMakeLists.txt @@ -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) diff --git a/test/tx/cmake/windows_port/CMakeLists.txt b/test/tx/cmake/windows_port/CMakeLists.txt index 0891a3a3b..6d0aa6606 100644 --- a/test/tx/cmake/windows_port/CMakeLists.txt +++ b/test/tx/cmake/windows_port/CMakeLists.txt @@ -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) diff --git a/test/tx/windows_port/threadx_windows_tick_rate_test.c b/test/tx/windows_port/threadx_windows_tick_rate_test.c new file mode 100644 index 000000000..7a656bdc6 --- /dev/null +++ b/test/tx/windows_port/threadx_windows_tick_rate_test.c @@ -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 +#include +#include +#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; +}