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
14 changes: 11 additions & 3 deletions ports/win32/vs_2019/inc/tx_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
10 changes: 3 additions & 7 deletions ports/win32/vs_2019/src/tx_initialize_low_level.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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. */
}
Expand Down Expand Up @@ -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++)
Expand Down
74 changes: 43 additions & 31 deletions ports/win32/vs_2019/src/tx_thread_context_restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);


/**************************************************************************/
Expand Down Expand Up @@ -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
{
Expand All @@ -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--;
}
25 changes: 11 additions & 14 deletions ports/win32/vs_2019/src/tx_thread_schedule.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ static VOID _tx_win32_semaphore_reset(HANDLE semaphore_handle);
/**************************************************************************/
VOID _tx_thread_schedule(VOID)
{
DWORD wait_status;



/* Loop forever. */
Expand Down Expand Up @@ -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. */
Expand All @@ -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__);

Expand Down
23 changes: 15 additions & 8 deletions ports/win64/vs_2022/inc/tx_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down
12 changes: 4 additions & 8 deletions ports/win64/vs_2022/src/tx_initialize_low_level.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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. */
}
Expand Down Expand Up @@ -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++)
Expand Down
Loading