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; +}