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
|
/***************************************************************************
* 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"
#include <stdio.h>
#include <errno.h>
extern sem_t _tx_linux_timer_semaphore;
extern sem_t _tx_linux_isr_semaphore;
extern UINT _tx_linux_timer_waiting;
extern pthread_t _tx_linux_timer_id;
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _tx_thread_schedule Linux/GNU */
/* 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 */
/* */
/* tx_linux_mutex_lock */
/* tx_linux_mutex_unlock */
/* _tx_linux_debug_entry_insert */
/* _tx_linux_thread_resume */
/* tx_linux_sem_post */
/* sem_trywait */
/* tx_linux_sem_wait */
/* */
/* CALLED BY */
/* */
/* _tx_initialize_kernel_enter ThreadX entry function */
/* */
/**************************************************************************/
VOID _tx_thread_schedule(VOID)
{
struct timespec ts;
/* Set timer. */
ts.tv_sec = 0;
ts.tv_nsec = 200000;
/* Loop forever. */
while(1)
{
/* Wait for a thread to execute and all ISRs to complete. */
while(1)
{
/* Lock Linux mutex. */
tx_linux_mutex_lock(_tx_linux_mutex);
/* 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
{
/* Unlock linux mutex. */
tx_linux_mutex_unlock(_tx_linux_mutex);
/* Don't waste all the processor time here in the master thread... */
#ifdef TX_LINUX_NO_IDLE_ENABLE
while(!sem_trywait(&_tx_linux_timer_semaphore));
tx_linux_sem_post(&_tx_linux_timer_semaphore);
/*nanosleep(&ts, &ts);*/
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_nsec += 200000;
if (ts.tv_nsec > 1000000000)
{
ts.tv_nsec -= 1000000000;
ts.tv_sec++;
}
sem_timedwait(&_tx_linux_semaphore_no_idle, &ts);
#else
nanosleep(&ts, &ts);
#endif /* TX_LINUX_NO_IDLE_ENABLE */
}
}
/* 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_linux_suspension_type)
{
/* Debug entry. */
_tx_linux_debug_entry_insert("SCHEDULE-resume_thread", __FILE__, __LINE__);
/* Pseudo interrupt suspension. The thread is not waiting on
its run semaphore. */
_tx_linux_thread_resume(_tx_thread_current_ptr -> tx_thread_linux_thread_id);
}
else
{
/* Debug entry. */
_tx_linux_debug_entry_insert("SCHEDULE-release_sem", __FILE__, __LINE__);
/* Make sure semaphore is 0. */
while(!sem_trywait(&_tx_thread_current_ptr -> tx_thread_linux_thread_run_semaphore));
/* Let the thread run again by releasing its run semaphore. */
tx_linux_sem_post(&_tx_thread_current_ptr -> tx_thread_linux_thread_run_semaphore);
/* Block timer ISR. */
if(_tx_linux_timer_waiting)
{
/* It is woken up by timer ISR. */
/* Let ThreadX thread wake up first. */
tx_linux_sem_wait(&_tx_linux_semaphore);
/* Wake up timer ISR. */
tx_linux_sem_post_nolock(&_tx_linux_isr_semaphore);
}
else
{
/* It is woken up by TX_THREAD. */
/* Suspend timer thread and let ThreadX thread wake up first. */
_tx_linux_thread_suspend(_tx_linux_timer_id);
tx_linux_sem_wait(&_tx_linux_semaphore);
_tx_linux_thread_resume(_tx_linux_timer_id);
}
}
/* Unlock linux mutex. */
tx_linux_mutex_unlock(_tx_linux_mutex);
/* Debug entry. */
_tx_linux_debug_entry_insert("SCHEDULE-self_suspend_sem", __FILE__, __LINE__);
/* Now suspend the main thread so the application thread can run. */
tx_linux_sem_wait(&_tx_linux_semaphore);
/* Debug entry. */
_tx_linux_debug_entry_insert("SCHEDULE-wake_up", __FILE__, __LINE__);
}
}
void _tx_thread_delete_port_completion(TX_THREAD *thread_ptr, UINT tx_saved_posture)
{
INT linux_status;
sem_t *threadrunsemaphore;
pthread_t thread_id;
struct timespec ts;
thread_id = thread_ptr -> tx_thread_linux_thread_id;
threadrunsemaphore = &(thread_ptr -> tx_thread_linux_thread_run_semaphore);
ts.tv_sec = 0;
ts.tv_nsec = 1000000;
TX_RESTORE
do
{
linux_status = pthread_cancel(thread_id);
if(linux_status != EAGAIN)
{
break;
}
_tx_linux_thread_resume(thread_id);
tx_linux_sem_post(threadrunsemaphore);
nanosleep(&ts, &ts);
} while (1);
pthread_join(thread_id, NULL);
sem_destroy(threadrunsemaphore);
TX_DISABLE
}
void _tx_thread_reset_port_completion(TX_THREAD *thread_ptr, UINT tx_saved_posture)
{
INT linux_status;
sem_t *threadrunsemaphore;
pthread_t thread_id;
struct timespec ts;
thread_id = thread_ptr -> tx_thread_linux_thread_id;
threadrunsemaphore = &(thread_ptr -> tx_thread_linux_thread_run_semaphore);
ts.tv_sec = 0;
ts.tv_nsec = 1000000;
TX_RESTORE
do
{
linux_status = pthread_cancel(thread_id);
if(linux_status != EAGAIN)
{
break;
}
_tx_linux_thread_resume(thread_id);
tx_linux_sem_post(threadrunsemaphore);
nanosleep(&ts, &ts);
} while (1);
pthread_join(thread_id, NULL);
sem_destroy(threadrunsemaphore);
TX_DISABLE
}
|