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
|
/***************************************************************************
* 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.4).
/**************************************************************************/
/**************************************************************************/
/** */
/** ThreadX Component */
/** */
/** Thread */
/** */
/**************************************************************************/
/**************************************************************************/
#define TX_SOURCE_CODE
/* Include necessary system files. */
#include "tx_api.h"
#include "tx_thread.h"
#include <stdio.h>
/* Prototype for new thread entry function. */
DWORD WINAPI _tx_win32_thread_entry(LPVOID p);
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _tx_thread_stack_build Win64/MSVC */
/* 6.1 */
/* AUTHOR */
/* */
/* William E. Lamie, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function builds a stack frame on the supplied thread's stack. */
/* The stack frame results in a fake interrupt return to the supplied */
/* function pointer. */
/* */
/* INPUT */
/* */
/* thread_ptr Pointer to thread control blk */
/* function_ptr Pointer to return function */
/* */
/* OUTPUT */
/* */
/* None */
/* */
/* CALLS */
/* */
/* CreateThread Win32 create thread */
/* ResumeThread Win32 resume thread */
/* SetThreadPriority Win32 set thread priority */
/* */
/* CALLED BY */
/* */
/* _tx_thread_create Create thread service */
/* _tx_thread_reset Reset thread service */
/* */
/**************************************************************************/
VOID _tx_thread_stack_build(TX_THREAD *thread_ptr, VOID (*function_ptr)(VOID))
{
/* Create a Win32 thread for the application thread. */
thread_ptr -> tx_thread_win32_thread_handle =
CreateThread(NULL, 0, _tx_win32_thread_entry, (LPVOID) thread_ptr, CREATE_SUSPENDED,
&(thread_ptr -> tx_thread_win32_thread_id));
/* Check for a good thread create. */
if (!thread_ptr -> tx_thread_win32_thread_handle)
{
/* Display an error message. */
printf("ThreadX Win32 error creating thread!\n");
while(1)
{
}
}
/* Otherwise, we have a good thread create. Now set the priority to
a lower level. */
SetThreadPriority(thread_ptr -> tx_thread_win32_thread_handle, THREAD_PRIORITY_LOWEST);
/* Create the run semaphore for the thread. This will allow the scheduler
control over when the thread actually runs. */
thread_ptr -> tx_thread_win32_thread_run_semaphore = CreateSemaphore(NULL, 0, 1, NULL);
/* Determine if the run semaphore was created successfully. */
if (!thread_ptr -> tx_thread_win32_thread_run_semaphore)
{
/* Display an error message. */
printf("ThreadX Win32 error creating thread running semaphore!\n");
while(1)
{
}
}
/* Create the scheduler acknowledgement semaphore for this thread. */
thread_ptr -> tx_thread_win32_thread_start_semaphore = CreateSemaphore(NULL, 0, 1, NULL);
/* Determine if the start semaphore was created successfully. */
if (!thread_ptr -> tx_thread_win32_thread_start_semaphore)
{
/* Display an error message. */
printf("ThreadX Win32 error creating thread start semaphore!\n");
while(1)
{
}
}
/* Setup the thread suspension type to solicited thread suspension.
Pseudo interrupt handlers will suspend with this field set to 1. */
thread_ptr -> tx_thread_win32_suspension_type = 0;
/* Clear the disabled count that will keep track of the
tx_interrupt_control nesting. */
thread_ptr -> tx_thread_win32_int_disabled_flag = 0;
/* Setup a fake thread stack pointer. */
thread_ptr -> tx_thread_stack_ptr = (VOID *) (((CHAR *) thread_ptr -> tx_thread_stack_end) - 8);
/* Clear the first word of the stack. */
*(((ULONG *) thread_ptr -> tx_thread_stack_ptr) - 1) = 0;
/* Make the thread initially ready so it will run to the initial wait on
its run semaphore. */
ResumeThread(thread_ptr -> tx_thread_win32_thread_handle);
/* Wait until the host thread is parked at the controlled handoff point
before ThreadX can schedule it. */
if (WaitForSingleObject(thread_ptr -> tx_thread_win32_thread_start_semaphore, INFINITE) != WAIT_OBJECT_0)
{
/* Display an error message. */
printf("ThreadX Win32 error synchronizing thread startup!\n");
while(1)
{
}
}
}
DWORD WINAPI _tx_win32_thread_entry(LPVOID ptr)
{
TX_THREAD *thread_ptr;
TX_THREAD *current_thread_ptr;
HANDLE threadhandle;
int threadpriority;
DWORD threadid;
/* Pickup the current thread pointer. */
thread_ptr = (TX_THREAD *) ptr;
/* Tell the creator that this host thread has reached the controlled
handoff point and is ready to be scheduled. */
ReleaseSemaphore(thread_ptr -> tx_thread_win32_thread_start_semaphore, 1, NULL);
/* Now suspend the thread initially. If the thread has already
been scheduled, this will return immediately. */
WaitForSingleObject(thread_ptr -> tx_thread_win32_thread_run_semaphore, INFINITE);
/* Acknowledge that the host thread is now able to execute ThreadX code. */
ReleaseSemaphore(thread_ptr -> tx_thread_win32_thread_start_semaphore, 1, NULL);
/* A deleted host thread can be released only to let it exit. In notify-enabled
builds, the first TX_DISABLE in _tx_thread_shell_entry catches this path.
When callbacks are disabled, perform the same check before the shell calls
the stale ThreadX entry function. */
_tx_win32_critical_section_obtain(&_tx_win32_critical_section);
threadhandle = GetCurrentThread();
threadpriority = GetThreadPriority(threadhandle);
threadid = GetCurrentThreadId();
current_thread_ptr = _tx_thread_current_ptr;
if ((threadpriority == THREAD_PRIORITY_LOWEST) &&
((current_thread_ptr == TX_NULL) || (current_thread_ptr -> tx_thread_win32_thread_id != threadid)))
{
_tx_win32_critical_section_release_all(&_tx_win32_critical_section);
ExitThread(0);
}
_tx_win32_critical_section_release(&_tx_win32_critical_section);
/* Call ThreadX thread entry point. */
_tx_thread_shell_entry();
return EXIT_SUCCESS;
}
|