1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/***************************************************************************
* 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 Codex (gpt 5.4).
* 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
**************************************************************************/
// Some portions generated by Codex (gpt 5.5).
#define TX_SOURCE_CODE
#define TX_THREAD_SMP_SOURCE_CODE
#include "tx_api.h"
#include "tx_thread.h"
#include "tx_timer.h"
static VOID _tx_win32_thread_cleanup(TX_THREAD *thread_ptr);
VOID _tx_thread_schedule(VOID)
{
UINT core;
UINT pending_work;
TX_THREAD *current_thread;
TX_THREAD *execute_thread;
UCHAR preempt_retry;
DWORD wait_status;
LONG start_sequence;
preempt_retry = TX_FALSE;
while (1)
{
_tx_win32_critical_section_obtain(&_tx_win32_critical_section);
if (_tx_win32_global_int_disabled_flag != TX_FALSE)
{
_tx_win32_system_error++;
}
_tx_win32_debug_entry_insert("SCHEDULE-wake_up", __FILE__, __LINE__);
for (core = 0U; core < TX_THREAD_SMP_MAX_CORES; core++)
{
current_thread = _tx_thread_current_ptr[core];
if ((current_thread != TX_NULL) && (current_thread -> tx_thread_win32_deferred_preempt != TX_FALSE))
{
if (_tx_thread_preempt_disable != 0U)
{
preempt_retry = TX_TRUE;
continue;
}
if (current_thread -> tx_thread_win32_mutex_access != TX_FALSE)
{
preempt_retry = TX_TRUE;
continue;
}
if (current_thread -> tx_thread_state != TX_TERMINATED)
{
_tx_win32_thread_suspend(current_thread -> tx_thread_win32_thread_handle);
current_thread -> tx_thread_win32_deferred_preempt = TX_FALSE;
current_thread -> tx_thread_win32_suspension_type = 1U;
if (_tx_timer_time_slice[core] != 0U)
{
current_thread -> tx_thread_time_slice = _tx_timer_time_slice[core];
_tx_timer_time_slice[core] = 0U;
}
}
_tx_thread_current_ptr[core] = TX_NULL;
_tx_win32_virtual_cores[core].tx_thread_smp_core_mapping_thread = TX_NULL;
_tx_win32_virtual_cores[core].tx_thread_smp_core_mapping_thread_handle = NULL;
_tx_win32_virtual_cores[core].tx_thread_smp_core_mapping_thread_id = 0U;
current_thread -> tx_thread_smp_core_control = 1U;
_tx_win32_debug_entry_insert("SCHEDULE-core_preempt_complete", __FILE__, __LINE__);
}
if (_tx_thread_current_ptr[core] == TX_NULL)
{
execute_thread = _tx_thread_execute_ptr[core];
if ((execute_thread != TX_NULL) && (execute_thread -> tx_thread_smp_core_control != 0U))
{
_tx_thread_current_ptr[core] = execute_thread;
execute_thread -> tx_thread_win32_virtual_core = core;
execute_thread -> tx_thread_smp_core_mapped = core;
_tx_win32_virtual_cores[core].tx_thread_smp_core_mapping_thread = execute_thread;
_tx_win32_virtual_cores[core].tx_thread_smp_core_mapping_thread_handle = execute_thread -> tx_thread_win32_thread_handle;
_tx_win32_virtual_cores[core].tx_thread_smp_core_mapping_thread_id = execute_thread -> tx_thread_win32_thread_id;
execute_thread -> tx_thread_smp_core_control = 0U;
execute_thread -> tx_thread_run_count++;
_tx_timer_time_slice[core] = execute_thread -> tx_thread_time_slice;
MemoryBarrier();
if (execute_thread -> tx_thread_win32_suspension_type == 1U)
{
execute_thread -> tx_thread_win32_suspension_type = 0U;
_tx_win32_debug_entry_insert("SCHEDULE-resume_thread", __FILE__, __LINE__);
_tx_win32_thread_resume(execute_thread -> tx_thread_win32_thread_handle);
}
else if (execute_thread -> tx_thread_win32_suspension_type == 2U)
{
execute_thread -> tx_thread_win32_suspension_type = 0U;
_tx_win32_debug_entry_insert("SCHEDULE-release_sem", __FILE__, __LINE__);
#ifdef TX_WIN32_PROFILE_ENABLE
_tx_win32_profile_mark_run_signal(execute_thread);
#endif
#if (TX_WIN32_USE_ADDRESS_WAIT == 0)
_tx_win32_semaphore_reset(execute_thread -> tx_thread_win32_thread_start_semaphore);
_tx_win32_semaphore_reset(execute_thread -> tx_thread_win32_thread_run_semaphore);
#endif
start_sequence = _tx_win32_thread_start_sequence_get(execute_thread);
_tx_win32_thread_run_signal(execute_thread);
/* Wait for the execute_thread to signal start_ack before proceeding.
* Holding the CS here means any thread that calls TX_DISABLE will
* spin with mutex_access=TRUE and preempt_disable≠0, creating the
* window the ISR needs to observe preempt_disable!=0 for resonance
* tests such as wait_abort_and_isr. */
wait_status = _tx_win32_wait_for_thread_start_ack(execute_thread, start_sequence);
if (wait_status != WAIT_OBJECT_0)
{
_tx_win32_system_error++;
}
}
else
{
_tx_win32_system_error++;
}
}
}
}
/* Signal context_restore's rendezvous wait now that all execute_threads have
* been resumed and their start_ack received. */
if (_tx_win32_timer_waiting != 0U)
{
ReleaseSemaphore(_tx_win32_isr_semaphore, 1, NULL);
}
if (preempt_retry != TX_FALSE)
{
_tx_win32_critical_section_release_all(&_tx_win32_critical_section);
_tx_win32_thread_yield();
preempt_retry = TX_FALSE;
continue;
}
_tx_win32_debug_entry_insert("SCHEDULE-self_suspend_sem", __FILE__, __LINE__);
pending_work = TX_FALSE;
for (core = 0U; core < TX_THREAD_SMP_MAX_CORES; core++)
{
current_thread = _tx_thread_current_ptr[core];
execute_thread = _tx_thread_execute_ptr[core];
if ((current_thread != TX_NULL) && (current_thread -> tx_thread_win32_deferred_preempt != TX_FALSE))
{
pending_work = TX_TRUE;
break;
}
if ((_tx_thread_current_ptr[core] == TX_NULL) &&
(execute_thread != TX_NULL) &&
(execute_thread -> tx_thread_smp_core_control != 0U))
{
pending_work = TX_TRUE;
break;
}
}
_tx_win32_critical_section_release_all(&_tx_win32_critical_section);
if (pending_work != TX_FALSE)
{
continue;
}
/* Coalesce stale wakeups so the scheduler yields to worker threads
until there is a new ready-state transition to process. */
(void) _tx_win32_wait_for_scheduler_event();
}
}
static VOID _tx_win32_thread_cleanup(TX_THREAD *thread_ptr)
{
DWORD exit_code;
ULONG wait_count;
if (thread_ptr -> tx_thread_win32_thread_handle != NULL)
{
wait_count = 0U;
do
{
if ((GetExitCodeThread(thread_ptr -> tx_thread_win32_thread_handle, &exit_code) != 0) &&
(exit_code != STILL_ACTIVE))
{
break;
}
_tx_win32_thread_resume(thread_ptr -> tx_thread_win32_thread_handle);
_tx_win32_thread_run_signal(thread_ptr);
_tx_win32_thread_sleep(1U);
wait_count++;
} while (wait_count < 100U);
if ((GetExitCodeThread(thread_ptr -> tx_thread_win32_thread_handle, &exit_code) != 0) &&
(exit_code == STILL_ACTIVE))
{
(void) TerminateThread(thread_ptr -> tx_thread_win32_thread_handle, 0U);
(void) WaitForSingleObject(thread_ptr -> tx_thread_win32_thread_handle, INFINITE);
}
CloseHandle(thread_ptr -> tx_thread_win32_thread_handle);
thread_ptr -> tx_thread_win32_thread_handle = NULL;
}
if (thread_ptr -> tx_thread_win32_thread_run_semaphore != NULL)
{
CloseHandle(thread_ptr -> tx_thread_win32_thread_run_semaphore);
thread_ptr -> tx_thread_win32_thread_run_semaphore = NULL;
}
if (thread_ptr -> tx_thread_win32_thread_start_semaphore != NULL)
{
CloseHandle(thread_ptr -> tx_thread_win32_thread_start_semaphore);
thread_ptr -> tx_thread_win32_thread_start_semaphore = NULL;
}
}
void _tx_thread_delete_port_completion(TX_THREAD *thread_ptr, UINT tx_interrupt_save)
{
_tx_thread_smp_unprotect(tx_interrupt_save);
_tx_win32_thread_cleanup(thread_ptr);
tx_interrupt_save = _tx_thread_smp_protect();
TX_PARAMETER_NOT_USED(tx_interrupt_save);
}
void _tx_thread_reset_port_completion(TX_THREAD *thread_ptr, UINT tx_interrupt_save)
{
_tx_thread_delete_port_completion(thread_ptr, tx_interrupt_save);
}
|