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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2026-present 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.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** ThreadX Component */
/** */
/** Thread */
/** */
/**************************************************************************/
/**************************************************************************/
#define TX_SOURCE_CODE
/* Include necessary system files. */
#include "tx_api.h"
#include "tx_thread.h"
#include "tx_timer.h"
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _tx_thread_schedule Win32/Visual */
/* 6.1 */
/* AUTHOR */
/* */
/* William E. Lamie, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function waits for a thread control block pointer to appear in */
/* the _tx_thread_execute_ptr variable. Once a thread pointer appears */
/* in the variable, the corresponding thread is resumed. */
/* */
/* INPUT */
/* */
/* None */
/* */
/* OUTPUT */
/* */
/* None */
/* */
/* CALLS */
/* */
/* ReleaseSemaphore Win32 release semaphore */
/* ResumeThread Win32 resume thread */
/* Sleep Win32 thread sleep */
/* WaitForSingleObject Win32 wait on a semaphore */
/* _tx_win32_critical_section_obtain Obtain critical section */
/* _tx_win32_critical_section_release Release critical section */
/* */
/* CALLED BY */
/* */
/* _tx_initialize_kernel_enter ThreadX entry function */
/* */
/**************************************************************************/
VOID _tx_thread_schedule(VOID)
{
/* Loop forever. */
while(1)
{
/* Wait for a thread to execute and all ISRs to complete. */
while(1)
{
/* Enter Win32 critical section. */
_tx_win32_critical_section_obtain(&_tx_win32_critical_section);
/* Debug entry. */
_tx_win32_debug_entry_insert("SCHEDULE-wake_up", __FILE__, __LINE__);
/* Determine if there is a thread ready to execute AND all ISRs
are complete. */
if ((_tx_thread_execute_ptr != TX_NULL) && (_tx_thread_system_state == 0))
{
/* Get out of this loop and schedule the thread! */
break;
}
else
{
/* Leave the critical section. */
_tx_win32_critical_section_release(&_tx_win32_critical_section);
/* Now sleep so we don't block forever. */
Sleep(2);
}
}
/* Yes! We have a thread to execute. Note that the critical section is already
active from the scheduling loop above. */
/* Setup the current thread pointer. */
_tx_thread_current_ptr = _tx_thread_execute_ptr;
/* Increment the run count for this thread. */
_tx_thread_current_ptr -> tx_thread_run_count++;
/* Setup time-slice, if present. */
_tx_timer_time_slice = _tx_thread_current_ptr -> tx_thread_time_slice;
/* Determine how the thread was suspended. */
if (_tx_thread_current_ptr -> tx_thread_win32_suspension_type)
{
/* Debug entry. */
_tx_win32_debug_entry_insert("SCHEDULE-resume_thread", __FILE__, __LINE__);
/* Pseudo interrupt suspension. The thread is not waiting on
its run semaphore. */
ResumeThread(_tx_thread_current_ptr -> tx_thread_win32_thread_handle);
}
else
{
/* Debug entry. */
_tx_win32_debug_entry_insert("SCHEDULE-release_sem", __FILE__, __LINE__);
/* Let the thread run again by releasing its run semaphore. */
ReleaseSemaphore(_tx_thread_current_ptr -> tx_thread_win32_thread_run_semaphore, 1, NULL);
}
/* Debug entry. */
_tx_win32_debug_entry_insert("SCHEDULE-self_suspend_sem", __FILE__, __LINE__);
/* Exit Win32 critical section. */
_tx_win32_critical_section_release(&_tx_win32_critical_section);
/* Now suspend the main thread so the application thread can run. */
WaitForSingleObject(_tx_win32_scheduler_semaphore, INFINITE);
}
}
/* Define the ThreadX Win32 critical section get, release, and release all functions. */
void _tx_win32_critical_section_obtain(TX_WIN32_CRITICAL_SECTION *critical_section)
{
TX_THREAD *thread_ptr;
/* Is the protection owned? */
if (critical_section -> tx_win32_critical_section_owner == GetCurrentThreadId())
{
/* Simply increment the nested counter. */
critical_section -> tx_win32_critical_section_nested_count++;
}
else
{
/* Pickup the current thread pointer. */
thread_ptr = _tx_thread_current_ptr;
/* Get the Win32 critical section. */
while (WaitForSingleObject(critical_section -> tx_win32_critical_section_mutex_handle, 3) != WAIT_OBJECT_0)
{
}
/* At this point we have the mutex. */
/* Increment the nesting counter. */
critical_section -> tx_win32_critical_section_nested_count = 1;
/* Remember the owner. */
critical_section -> tx_win32_critical_section_owner = GetCurrentThreadId();
}
}
void _tx_win32_critical_section_release(TX_WIN32_CRITICAL_SECTION *critical_section)
{
/* Ensure the caller is the mutex owner. */
if (critical_section -> tx_win32_critical_section_owner == GetCurrentThreadId())
{
/* Determine if there is protection. */
if (critical_section -> tx_win32_critical_section_nested_count)
{
/* Decrement the nesting counter. */
critical_section -> tx_win32_critical_section_nested_count--;
/* Determine if the critical section is now being released. */
if (critical_section -> tx_win32_critical_section_nested_count == 0)
{
/* Yes, it is being released clear the owner. */
critical_section -> tx_win32_critical_section_owner = 0;
/* Finally, release the mutex. */
if (ReleaseMutex(critical_section -> tx_win32_critical_section_mutex_handle) != TX_TRUE)
{
/* Increment the system error counter. */
_tx_win32_system_error++;
}
/* Just in case, make sure there the mutex is not owned. */
while (ReleaseMutex(critical_section -> tx_win32_critical_section_mutex_handle) == TX_TRUE)
{
/* Increment the system error counter. */
_tx_win32_system_error++;
}
/* Sleep for 0, just to relinquish to other ready threads. */
Sleep(0);
}
}
}
else
{
/* Increment the system error counter. */
_tx_win32_system_error++;
}
}
void _tx_win32_critical_section_release_all(TX_WIN32_CRITICAL_SECTION *critical_section)
{
/* Ensure the caller is the mutex owner. */
if (critical_section -> tx_win32_critical_section_owner == GetCurrentThreadId())
{
/* Determine if there is protection. */
if (critical_section -> tx_win32_critical_section_nested_count)
{
/* Clear the nesting counter. */
critical_section -> tx_win32_critical_section_nested_count = 0;
/* Yes, it is being release clear the owner. */
critical_section -> tx_win32_critical_section_owner = 0;
/* Finally, release the mutex. */
if (ReleaseMutex(critical_section -> tx_win32_critical_section_mutex_handle) != TX_TRUE)
{
/* Increment the system error counter. */
_tx_win32_system_error++;
}
/* Just in case, make sure there the mutex is not owned. */
while (ReleaseMutex(critical_section -> tx_win32_critical_section_mutex_handle) == TX_TRUE)
{
/* Increment the system error counter. */
_tx_win32_system_error++;
}
}
}
else
{
/* Increment the system error counter. */
_tx_win32_system_error++;
}
}
|