Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define TX_SOURCE_CODE
#include "tx_api.h"
#include "tx_timer.h"
#include "tx_thread.h"
...
...
_tx_timer_thread_entry(ULONG)
Files
loading...
CodeScopeSTM32 Libraries and Samplesthreadxcommon/src/tx_timer_thread_entry.c
 
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** ThreadX Component */ /** */ /** Timer */ /** */... /**************************************************************************/ /**************************************************************************/ #define TX_SOURCE_CODE /* Include necessary system files. */ #include "tx_api.h" #include "tx_timer.h" #include "tx_thread.h" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _tx_timer_thread_entry PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function manages thread and application timer expirations. */ /* Actually, from this thread's point of view, there is no difference. */ /* */ /* INPUT */ /* */ /* timer_thread_input Used just for verification */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* Timer Expiration Function */ /* _tx_thread_system_suspend Thread suspension */ /* _tx_thread_system_ni_suspend Non-interruptable suspend thread */ /* _tx_timer_system_activate Timer reactivate processing */ /* */ /* CALLED BY */ /* */ /* ThreadX Scheduler */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 William E. Lamie Initial Version 6.0 */ /* 09-30-2020 Yuxin Zhou Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ #ifndef TX_TIMER_PROCESS_IN_ISR VOID _tx_timer_thread_entry(ULONG timer_thread_input) { TX_INTERRUPT_SAVE_AREA TX_TIMER_INTERNAL *expired_timers; TX_TIMER_INTERNAL *reactivate_timer; TX_TIMER_INTERNAL *next_timer; TX_TIMER_INTERNAL *previous_timer; TX_TIMER_INTERNAL *current_timer; VOID (*timeout_function)(ULONG id); ULONG timeout_param = ((ULONG) 0); TX_THREAD *thread_ptr; #ifdef TX_REACTIVATE_INLINE TX_TIMER_INTERNAL **timer_list; /* Timer list pointer */ UINT expiration_time; /* Value used for pointer offset*/ ULONG delta;/* ... */ #endif #ifdef TX_TIMER_ENABLE_PERFORMANCE_INFO TX_TIMER *timer_ptr; #endif /* Make sure the timer input is correct. This also gets rid of the silly compiler warnings. *//* ... */ if (timer_thread_input == TX_TIMER_ID) { /* Yes, valid thread entry, proceed... */ /* Now go into an infinite loop to process timer expirations. */ while (TX_LOOP_FOREVER) { /* First, move the current list pointer and clear the timer expired value. This allows the interrupt handling portion to continue looking for timer expirations. *//* ... */ TX_DISABLE /* Save the current timer expiration list pointer. */ expired_timers = *_tx_timer_current_ptr; /* Modify the head pointer in the first timer in the list, if there is one! *//* ... */ if (expired_timers != TX_NULL) { expired_timers -> tx_timer_internal_list_head = &expired_timers; }if (expired_timers != TX_NULL) { ... } /* Set the current list pointer to NULL. */ *_tx_timer_current_ptr = TX_NULL; /* Move the current pointer up one timer entry wrap if we get to the end of the list. *//* ... */ _tx_timer_current_ptr = TX_TIMER_POINTER_ADD(_tx_timer_current_ptr, 1); if (_tx_timer_current_ptr == _tx_timer_list_end) { _tx_timer_current_ptr = _tx_timer_list_start; }if (_tx_timer_current_ptr == _tx_timer_list_end) { ... } /* Clear the expired flag. */ _tx_timer_expired = TX_FALSE; /* Restore interrupts temporarily. */ TX_RESTORE /* Disable interrupts again. */ TX_DISABLE /* Next, process the expiration of the associated timers at this time slot. *//* ... */ while (expired_timers != TX_NULL) { /* Something is on the list. Remove it and process the expiration. */ current_timer = expired_timers; /* Pickup the next timer. */ next_timer = expired_timers -> tx_timer_internal_active_next; /* Set the reactivate_timer to NULL. */ reactivate_timer = TX_NULL; /* Determine if this is the only timer. */ if (current_timer == next_timer) { /* Yes, this is the only timer in the list. */ /* Set the head pointer to NULL. */ expired_timers = TX_NULL; }if (current_timer == next_timer) { ... } else { /* No, not the only expired timer. */ /* Remove this timer from the expired list. */ previous_timer = current_timer -> tx_timer_internal_active_previous; next_timer -> tx_timer_internal_active_previous = previous_timer; previous_timer -> tx_timer_internal_active_next = next_timer; /* Modify the next timer's list head to point at the current list head. */ next_timer -> tx_timer_internal_list_head = &expired_timers; /* Set the list head pointer. */ expired_timers = next_timer; }else { ... } /* In any case, the timer is now off of the expired list. */ /* Determine if the timer has expired or if it is just a really big timer that needs to be placed in the list again. *//* ... */ if (current_timer -> tx_timer_internal_remaining_ticks > TX_TIMER_ENTRIES) { /* Timer is bigger than the timer entries and must be rescheduled. *//* ... */ #ifdef TX_TIMER_ENABLE_PERFORMANCE_INFO /* Increment the total expiration adjustments counter. */ _tx_timer_performance__expiration_adjust_count++; /* Determine if this is an application timer. */ if (current_timer -> tx_timer_internal_timeout_function != &_tx_thread_timeout) { /* Derive the application timer pointer. */ /* Pickup the application timer pointer. */ TX_USER_TIMER_POINTER_GET(current_timer, timer_ptr) /* Increment the number of expiration adjustments on this timer. */ if (timer_ptr -> tx_timer_id == TX_TIMER_ID) { timer_ptr -> tx_timer_performance__expiration_adjust_count++; }if (timer_ptr -> tx_timer_id == TX_TIMER_ID) { ... } }if (current_timer -> tx_timer_internal_timeout_function != &_tx_thread_timeout) { ... } /* ... */#endif /* Decrement the remaining ticks of the timer. */ current_timer -> tx_timer_internal_remaining_ticks = current_timer -> tx_timer_internal_remaining_ticks - TX_TIMER_ENTRIES; /* Set the timeout function to NULL in order to bypass the expiration. *//* ... */ timeout_function = TX_NULL; /* Make the timer appear that it is still active while interrupts are enabled. This will permit proper processing of a timer deactivate from an ISR. *//* ... */ current_timer -> tx_timer_internal_list_head = &reactivate_timer; current_timer -> tx_timer_internal_active_next = current_timer; /* Setup the temporary timer list head pointer. */ reactivate_timer = current_timer; }if (current_timer -> tx_timer_internal_remaining_ticks > TX_TIMER_ENTRIES) { ... } else { /* Timer did expire. */ #ifdef TX_TIMER_ENABLE_PERFORMANCE_INFO /* Increment the total expirations counter. */ _tx_timer_performance_expiration_count++; /* Determine if this is an application timer. */ if (current_timer -> tx_timer_internal_timeout_function != &_tx_thread_timeout) { /* Derive the application timer pointer. */ /* Pickup the application timer pointer. */ TX_USER_TIMER_POINTER_GET(current_timer, timer_ptr) /* Increment the number of expirations on this timer. */ if (timer_ptr -> tx_timer_id == TX_TIMER_ID) { timer_ptr -> tx_timer_performance_expiration_count++; }if (timer_ptr -> tx_timer_id == TX_TIMER_ID) { ... } }if (current_timer -> tx_timer_internal_timeout_function != &_tx_thread_timeout) { ... } /* ... */#endif /* Copy the calling function and ID into local variables before interrupts are re-enabled. *//* ... */ timeout_function = current_timer -> tx_timer_internal_timeout_function; timeout_param = current_timer -> tx_timer_internal_timeout_param; /* Copy the reinitialize ticks into the remaining ticks. */ current_timer -> tx_timer_internal_remaining_ticks = current_timer -> tx_timer_internal_re_initialize_ticks; /* Determine if the timer should be reactivated. */ if (current_timer -> tx_timer_internal_remaining_ticks != ((ULONG) 0)) { /* Make the timer appear that it is still active while processing the expiration routine and with interrupts enabled. This will permit proper processing of a timer deactivate from both the expiration routine and an ISR. *//* ... */ current_timer -> tx_timer_internal_list_head = &reactivate_timer; current_timer -> tx_timer_internal_active_next = current_timer; /* Setup the temporary timer list head pointer. */ reactivate_timer = current_timer; }if (current_timer -> tx_timer_internal_remaining_ticks != ((ULONG) 0)) { ... } else { /* Set the list pointer of this timer to NULL. This is used to indicate the timer is no longer active. *//* ... */ current_timer -> tx_timer_internal_list_head = TX_NULL; }else { ... } }else { ... } /* Set pointer to indicate the expired timer that is currently being processed. */ _tx_timer_expired_timer_ptr = current_timer; /* Restore interrupts for timer expiration call. */ TX_RESTORE /* Call the timer-expiration function, if non-NULL. */ if (timeout_function != TX_NULL) { (timeout_function) (timeout_param); }if (timeout_function != TX_NULL) { ... } /* Lockout interrupts again. */ TX_DISABLE /* Clear expired timer pointer. */ _tx_timer_expired_timer_ptr = TX_NULL; /* Determine if the timer needs to be reactivated. */ if (reactivate_timer == current_timer) { /* Reactivate the timer. */ #ifdef TX_TIMER_ENABLE_PERFORMANCE_INFO /* Determine if this timer expired. */ if (timeout_function != TX_NULL) { /* Increment the total reactivations counter. */ _tx_timer_performance_reactivate_count++; /* Determine if this is an application timer. */ if (current_timer -> tx_timer_internal_timeout_function != &_tx_thread_timeout) { /* Derive the application timer pointer. */ /* Pickup the application timer pointer. */ TX_USER_TIMER_POINTER_GET(current_timer, timer_ptr) /* Increment the number of expirations on this timer. */ if (timer_ptr -> tx_timer_id == TX_TIMER_ID) { timer_ptr -> tx_timer_performance_reactivate_count++; }if (timer_ptr -> tx_timer_id == TX_TIMER_ID) { ... } }if (current_timer -> tx_timer_internal_timeout_function != &_tx_thread_timeout) { ... } }if (timeout_function != TX_NULL) { ... } /* ... */#endif #ifdef TX_REACTIVATE_INLINE /* Calculate the amount of time remaining for the timer. */ if (current_timer -> tx_timer_internal_remaining_ticks > TX_TIMER_ENTRIES) { /* Set expiration time to the maximum number of entries. */ expiration_time = TX_TIMER_ENTRIES - ((UINT) 1); }if (current_timer -> tx_timer_internal_remaining_ticks > TX_TIMER_ENTRIES) { ... } else { /* Timer value fits in the timer entries. */ /* Set the expiration time. */ expiration_time = ((UINT) current_timer -> tx_timer_internal_remaining_ticks) - ((UINT) 1); }else { ... } /* At this point, we are ready to put the timer back on one of the timer lists. *//* ... */ /* Calculate the proper place for the timer. */ timer_list = TX_TIMER_POINTER_ADD(_tx_timer_current_ptr, expiration_time); if (TX_TIMER_INDIRECT_TO_VOID_POINTER_CONVERT(timer_list) >= TX_TIMER_INDIRECT_TO_VOID_POINTER_CONVERT(_tx_timer_list_end)) { /* Wrap from the beginning of the list. */ delta = TX_TIMER_POINTER_DIF(timer_list, _tx_timer_list_end); timer_list = TX_TIMER_POINTER_ADD(_tx_timer_list_start, delta); }if (TX_TIMER_INDIRECT_TO_VOID_POINTER_CONVERT(timer_list) >= TX_TIMER_INDIRECT_TO_VOID_POINTER_CONVERT(_tx_timer_list_end)) { ... } /* Now put the timer on this list. */ if ((*timer_list) == TX_NULL) { /* This list is NULL, just put the new timer on it. */ /* Setup the links in this timer. */ current_timer -> tx_timer_internal_active_next = current_timer; current_timer -> tx_timer_internal_active_previous = current_timer; /* Setup the list head pointer. */ *timer_list = current_timer; }if ((*timer_list) == TX_NULL) { ... } else { /* This list is not NULL, add current timer to the end. */ next_timer = *timer_list; previous_timer = next_timer -> tx_timer_internal_active_previous; previous_timer -> tx_timer_internal_active_next = current_timer; next_timer -> tx_timer_internal_active_previous = current_timer; current_timer -> tx_timer_internal_active_next = next_timer; current_timer -> tx_timer_internal_active_previous = previous_timer; }else { ... } /* Setup list head pointer. */ current_timer -> tx_timer_internal_list_head = timer_list;/* ... */ #else /* Reactivate through the timer activate function. */ /* Clear the list head for the timer activate call. */ current_timer -> tx_timer_internal_list_head = TX_NULL; /* Activate the current timer. */ _tx_timer_system_activate(current_timer);/* ... */ #endif }if (reactivate_timer == current_timer) { ... } /* Restore interrupts. */ TX_RESTORE /* Lockout interrupts again. */ TX_DISABLE }while (expired_timers != TX_NULL) { ... } /* Finally, suspend this thread and wait for the next expiration. */ /* Determine if another expiration took place while we were in this thread. If so, process another expiration. *//* ... */ if (_tx_timer_expired == TX_FALSE) { /* Otherwise, no timer expiration, so suspend the thread. */ /* Build pointer to the timer thread. */ thread_ptr = &_tx_timer_thread; /* Set the status to suspending, in order to indicate the suspension is in progress. *//* ... */ thread_ptr -> tx_thread_state = TX_SUSPENDED; #ifdef TX_NOT_INTERRUPTABLE /* Call actual non-interruptable thread suspension routine. */ _tx_thread_system_ni_suspend(thread_ptr, ((ULONG) 0)); /* Restore interrupts. */ TX_RESTORE/* ... */ #else /* Set the suspending flag. */ thread_ptr -> tx_thread_suspending = TX_TRUE; /* Increment the preempt disable count prior to suspending. */ _tx_thread_preempt_disable++; /* Restore interrupts. */ TX_RESTORE /* Call actual thread suspension routine. */ _tx_thread_system_suspend(thread_ptr);/* ... */ #endif }if (_tx_timer_expired == TX_FALSE) { ... } else { /* Restore interrupts. */ TX_RESTORE }else { ... } }while (TX_LOOP_FOREVER) { ... } }if (timer_thread_input == TX_TIMER_ID) { ... } #ifdef TX_SAFETY_CRITICAL /* If we ever get here, raise safety critical exception. */ TX_SAFETY_CRITICAL_EXCEPTION(__FILE__, __LINE__, 0);/* ... */ #endif }{ ... } /* ... */#endif...