tx_thread.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /***************************************************************************
  2. * Copyright (c) 2024 Microsoft Corporation
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the MIT License which is available at
  6. * https://opensource.org/licenses/MIT.
  7. *
  8. * SPDX-License-Identifier: MIT
  9. **************************************************************************/
  10. /**************************************************************************/
  11. /**************************************************************************/
  12. /** */
  13. /** ThreadX Component */
  14. /** */
  15. /** Thread */
  16. /** */
  17. /**************************************************************************/
  18. /**************************************************************************/
  19. /**************************************************************************/
  20. /* */
  21. /* COMPONENT DEFINITION RELEASE */
  22. /* */
  23. /* tx_thread.h PORTABLE C */
  24. /* 6.1.9 */
  25. /* AUTHOR */
  26. /* */
  27. /* William E. Lamie, Microsoft Corporation */
  28. /* */
  29. /* DESCRIPTION */
  30. /* */
  31. /* This file defines the ThreadX thread control component, including */
  32. /* data types and external references. It is assumed that tx_api.h */
  33. /* and tx_port.h have already been included. */
  34. /* */
  35. /* RELEASE HISTORY */
  36. /* */
  37. /* DATE NAME DESCRIPTION */
  38. /* */
  39. /* 05-19-2020 William E. Lamie Initial Version 6.0 */
  40. /* 09-30-2020 Yuxin Zhou Modified comment(s), */
  41. /* resulting in version 6.1 */
  42. /* 11-09-2020 Yuxin Zhou Modified comment(s), and */
  43. /* moved TX_THREAD_GET_SYSTEM_ */
  44. /* STATE to tx_api.h, */
  45. /* resulting in version 6.1.2 */
  46. /* 10-15-2021 Scott Larson Modified comment(s), improved */
  47. /* stack check error handling, */
  48. /* resulting in version 6.1.9 */
  49. /* */
  50. /**************************************************************************/
  51. #ifndef TX_THREAD_H
  52. #define TX_THREAD_H
  53. /* Define thread control specific data definitions. */
  54. #define TX_THREAD_ID ((ULONG) 0x54485244)
  55. #define TX_THREAD_PRIORITY_GROUP_MASK ((ULONG) 0xFF)
  56. #define TX_THREAD_EXECUTE_LOG_SIZE ((UINT) 8)
  57. /* Define the MOD32 bit set macro that is used to set/clear a priority bit within a specific
  58. priority group. */
  59. #if TX_MAX_PRIORITIES > 32
  60. #define MAP_INDEX (map_index)
  61. #ifndef TX_MOD32_BIT_SET
  62. #define TX_MOD32_BIT_SET(a,b) (b) = (((ULONG) 1) << ((a)%((UINT)32)));
  63. #endif
  64. #else
  65. #define MAP_INDEX (0)
  66. #ifndef TX_MOD32_BIT_SET
  67. #define TX_MOD32_BIT_SET(a,b) (b) = (((ULONG) 1) << ((a)));
  68. #endif
  69. #endif
  70. /* Define the DIV32 bit set macro that is used to set/clear a priority group bit and is
  71. only necessary when using priorities greater than 32. */
  72. #if TX_MAX_PRIORITIES > 32
  73. #ifndef TX_DIV32_BIT_SET
  74. #define TX_DIV32_BIT_SET(a,b) (b) = (((ULONG) 1) << ((a)/((UINT) 32)));
  75. #endif
  76. #endif
  77. /* Define state change macro that can be used by run-mode debug agents to keep track of thread
  78. state changes. By default, it is mapped to white space. */
  79. #ifndef TX_THREAD_STATE_CHANGE
  80. #define TX_THREAD_STATE_CHANGE(a, b)
  81. #endif
  82. /* Define the macro to get the current thread pointer. This is particularly useful in SMP
  83. versions of ThreadX to add additional processing. The default implementation is to simply
  84. access the global current thread pointer directly. */
  85. #ifndef TX_THREAD_GET_CURRENT
  86. #define TX_THREAD_GET_CURRENT(a) (a) = _tx_thread_current_ptr;
  87. #endif
  88. /* Define the macro to set the current thread pointer. This is particularly useful in SMP
  89. versions of ThreadX to add additional processing. The default implementation is to simply
  90. access the global current thread pointer directly. */
  91. #ifndef TX_THREAD_SET_CURRENT
  92. #define TX_THREAD_SET_CURRENT(a) _tx_thread_current_ptr = (a);
  93. #endif
  94. /* Define the get system state macro. By default, it simply maps to the variable _tx_thread_system_state. */
  95. /* This symbol is moved to tx_api.h. Therefore removed from this file.
  96. #ifndef TX_THREAD_GET_SYSTEM_STATE
  97. #define TX_THREAD_GET_SYSTEM_STATE() _tx_thread_system_state
  98. #endif
  99. */
  100. /* Define the check for whether or not to call the _tx_thread_system_return function. A non-zero value
  101. indicates that _tx_thread_system_return should not be called. */
  102. #ifndef TX_THREAD_SYSTEM_RETURN_CHECK
  103. #define TX_THREAD_SYSTEM_RETURN_CHECK(c) (c) = (ULONG) _tx_thread_preempt_disable; (c) = (c) | TX_THREAD_GET_SYSTEM_STATE();
  104. #endif
  105. /* Define the timeout setup macro used in _tx_thread_create. */
  106. #ifndef TX_THREAD_CREATE_TIMEOUT_SETUP
  107. #define TX_THREAD_CREATE_TIMEOUT_SETUP(t) (t) -> tx_thread_timer.tx_timer_internal_timeout_function = &(_tx_thread_timeout); \
  108. (t) -> tx_thread_timer.tx_timer_internal_timeout_param = TX_POINTER_TO_ULONG_CONVERT((t));
  109. #endif
  110. /* Define the thread timeout pointer setup macro used in _tx_thread_timeout. */
  111. #ifndef TX_THREAD_TIMEOUT_POINTER_SETUP
  112. #define TX_THREAD_TIMEOUT_POINTER_SETUP(t) (t) = TX_ULONG_TO_THREAD_POINTER_CONVERT(timeout_input);
  113. #endif
  114. /* Define the lowest bit set macro. Note, that this may be overridden
  115. by a port specific definition if there is supporting assembly language
  116. instructions in the architecture. */
  117. #ifndef TX_LOWEST_SET_BIT_CALCULATE
  118. #define TX_LOWEST_SET_BIT_CALCULATE(m, b) \
  119. (b) = ((ULONG) 0); \
  120. (m) = (m) & ((~(m)) + ((ULONG) 1)); \
  121. if ((m) < ((ULONG) 0x10)) \
  122. { \
  123. if ((m) >= ((ULONG) 4)) \
  124. { \
  125. (m) = (m) >> ((ULONG) 2); \
  126. (b) = (b) + ((ULONG) 2); \
  127. } \
  128. (b) = (b) + ((m) >> ((ULONG) 1)); \
  129. } \
  130. else if ((m) < ((ULONG) 0x100)) \
  131. { \
  132. (m) = (m) >> ((ULONG) 4); \
  133. (b) = (b) + ((ULONG) 4); \
  134. if ((m) >= ((ULONG) 4)) \
  135. { \
  136. (m) = (m) >> ((ULONG) 2); \
  137. (b) = (b) + ((ULONG) 2); \
  138. } \
  139. (b) = (b) + ((m) >> ((ULONG) 1)); \
  140. } \
  141. else if ((m) < ((ULONG) 0x10000)) \
  142. { \
  143. (m) = (m) >> ((ULONG) 8); \
  144. (b) = (b) + ((ULONG) 8); \
  145. if ((m) >= ((ULONG) 0x10)) \
  146. { \
  147. (m) = (m) >> ((ULONG) 4); \
  148. (b) = (b) + ((ULONG) 4); \
  149. } \
  150. if ((m) >= ((ULONG) 4)) \
  151. { \
  152. (m) = (m) >> ((ULONG) 2); \
  153. (b) = (b) + ((ULONG) 2); \
  154. } \
  155. (b) = (b) + ((m) >> ((ULONG) 1)); \
  156. } \
  157. else \
  158. { \
  159. (m) = (m) >> ((ULONG) 16); \
  160. (b) = (b) + ((ULONG) 16); \
  161. if ((m) >= ((ULONG) 0x100)) \
  162. { \
  163. (m) = (m) >> ((ULONG) 8); \
  164. (b) = (b) + ((ULONG) 8); \
  165. } \
  166. if ((m) >= ((ULONG) 16)) \
  167. { \
  168. (m) = (m) >> ((ULONG) 4); \
  169. (b) = (b) + ((ULONG) 4); \
  170. } \
  171. if ((m) >= ((ULONG) 4)) \
  172. { \
  173. (m) = (m) >> ((ULONG) 2); \
  174. (b) = (b) + ((ULONG) 2); \
  175. } \
  176. (b) = (b) + ((m) >> ((ULONG) 1)); \
  177. }
  178. #endif
  179. /* Define the default thread stack checking. This can be overridden by
  180. a particular port, which is necessary if the stack growth is from
  181. low address to high address (the default logic is for stacks that
  182. grow from high address to low address. */
  183. #ifndef TX_THREAD_STACK_CHECK
  184. #define TX_THREAD_STACK_CHECK(thread_ptr) \
  185. { \
  186. TX_INTERRUPT_SAVE_AREA \
  187. TX_DISABLE \
  188. if (((thread_ptr)) && ((thread_ptr) -> tx_thread_id == TX_THREAD_ID)) \
  189. { \
  190. if (((ULONG *) (thread_ptr) -> tx_thread_stack_ptr) < ((ULONG *) (thread_ptr) -> tx_thread_stack_highest_ptr)) \
  191. { \
  192. (thread_ptr) -> tx_thread_stack_highest_ptr = (thread_ptr) -> tx_thread_stack_ptr; \
  193. } \
  194. if ((*((ULONG *) (thread_ptr) -> tx_thread_stack_start) != TX_STACK_FILL) || \
  195. (*((ULONG *) (((UCHAR *) (thread_ptr) -> tx_thread_stack_end) + 1)) != TX_STACK_FILL) || \
  196. (((ULONG *) (thread_ptr) -> tx_thread_stack_highest_ptr) < ((ULONG *) (thread_ptr) -> tx_thread_stack_start))) \
  197. { \
  198. TX_RESTORE \
  199. _tx_thread_stack_error_handler((thread_ptr)); \
  200. TX_DISABLE \
  201. } \
  202. if (*(((ULONG *) (thread_ptr) -> tx_thread_stack_highest_ptr) - 1) != TX_STACK_FILL) \
  203. { \
  204. TX_RESTORE \
  205. _tx_thread_stack_analyze((thread_ptr)); \
  206. TX_DISABLE \
  207. } \
  208. } \
  209. TX_RESTORE \
  210. }
  211. #endif
  212. /* Define default post thread delete macro to whitespace, if it hasn't been defined previously (typically in tx_port.h). */
  213. #ifndef TX_THREAD_DELETE_PORT_COMPLETION
  214. #define TX_THREAD_DELETE_PORT_COMPLETION(t)
  215. #endif
  216. /* Define default post thread reset macro to whitespace, if it hasn't been defined previously (typically in tx_port.h). */
  217. #ifndef TX_THREAD_RESET_PORT_COMPLETION
  218. #define TX_THREAD_RESET_PORT_COMPLETION(t)
  219. #endif
  220. /* Define the thread create internal extension macro to whitespace, if it hasn't been defined previously (typically in tx_port.h). */
  221. #ifndef TX_THREAD_CREATE_INTERNAL_EXTENSION
  222. #define TX_THREAD_CREATE_INTERNAL_EXTENSION(t)
  223. #endif
  224. /* Define internal thread control function prototypes. */
  225. VOID _tx_thread_initialize(VOID);
  226. VOID _tx_thread_schedule(VOID);
  227. VOID _tx_thread_shell_entry(VOID);
  228. VOID _tx_thread_stack_analyze(TX_THREAD *thread_ptr);
  229. VOID _tx_thread_stack_build(TX_THREAD *thread_ptr, VOID (*function_ptr)(VOID));
  230. VOID _tx_thread_stack_error(TX_THREAD *thread_ptr);
  231. VOID _tx_thread_stack_error_handler(TX_THREAD *thread_ptr);
  232. VOID _tx_thread_system_preempt_check(VOID);
  233. VOID _tx_thread_system_resume(TX_THREAD *thread_ptr);
  234. VOID _tx_thread_system_ni_resume(TX_THREAD *thread_ptr);
  235. VOID _tx_thread_system_return(VOID);
  236. VOID _tx_thread_system_suspend(TX_THREAD *thread_ptr);
  237. VOID _tx_thread_system_ni_suspend(TX_THREAD *thread_ptr, ULONG wait_option);
  238. VOID _tx_thread_time_slice(VOID);
  239. VOID _tx_thread_timeout(ULONG timeout_input);
  240. /* Thread control component data declarations follow. */
  241. /* Determine if the initialization function of this component is including
  242. this file. If so, make the data definitions really happen. Otherwise,
  243. make them extern so other functions in the component can access them. */
  244. #define THREAD_DECLARE extern
  245. /* Define the pointer that contains the system stack pointer. This is
  246. utilized when control returns from a thread to the system to reset the
  247. current stack. This is setup in the low-level initialization function. */
  248. THREAD_DECLARE VOID * _tx_thread_system_stack_ptr;
  249. /* Define the current thread pointer. This variable points to the currently
  250. executing thread. If this variable is NULL, no thread is executing. */
  251. THREAD_DECLARE TX_THREAD * _tx_thread_current_ptr;
  252. /* Define the variable that holds the next thread to execute. It is important
  253. to remember that this is not necessarily equal to the current thread
  254. pointer. */
  255. THREAD_DECLARE TX_THREAD * _tx_thread_execute_ptr;
  256. /* Define the head pointer of the created thread list. */
  257. THREAD_DECLARE TX_THREAD * _tx_thread_created_ptr;
  258. /* Define the variable that holds the number of created threads. */
  259. THREAD_DECLARE ULONG _tx_thread_created_count;
  260. /* Define the current state variable. When this value is 0, a thread
  261. is executing or the system is idle. Other values indicate that
  262. interrupt or initialization processing is active. This variable is
  263. initialized to TX_INITIALIZE_IN_PROGRESS to indicate initialization is
  264. active. */
  265. THREAD_DECLARE volatile ULONG _tx_thread_system_state;
  266. /* Define the 32-bit priority bit-maps. There is one priority bit map for each
  267. 32 priority levels supported. If only 32 priorities are supported there is
  268. only one bit map. Each bit within a priority bit map represents that one
  269. or more threads at the associated thread priority are ready. */
  270. THREAD_DECLARE ULONG _tx_thread_priority_maps[TX_MAX_PRIORITIES/32];
  271. /* Define the priority map active bit map that specifies which of the previously
  272. defined priority maps have something set. This is only necessary if more than
  273. 32 priorities are supported. */
  274. #if TX_MAX_PRIORITIES > 32
  275. THREAD_DECLARE ULONG _tx_thread_priority_map_active;
  276. #endif
  277. #ifndef TX_DISABLE_PREEMPTION_THRESHOLD
  278. /* Define the 32-bit preempt priority bit maps. There is one preempt bit map
  279. for each 32 priority levels supported. If only 32 priorities are supported
  280. there is only one bit map. Each set set bit corresponds to a preempted priority
  281. level that had preemption-threshold active to protect against preemption of a
  282. range of relatively higher priority threads. */
  283. THREAD_DECLARE ULONG _tx_thread_preempted_maps[TX_MAX_PRIORITIES/32];
  284. /* Define the preempt map active bit map that specifies which of the previously
  285. defined preempt maps have something set. This is only necessary if more than
  286. 32 priorities are supported. */
  287. #if TX_MAX_PRIORITIES > 32
  288. THREAD_DECLARE ULONG _tx_thread_preempted_map_active;
  289. #endif
  290. #endif
  291. /* Define the variable that holds the highest priority group ready for
  292. execution. It is important to note that this is not necessarily the same
  293. as the priority of the thread pointed to by _tx_execute_thread. */
  294. THREAD_DECLARE UINT _tx_thread_highest_priority;
  295. /* Define the array of thread pointers. Each entry represents the threads that
  296. are ready at that priority group. For example, index 10 in this array
  297. represents the first thread ready at priority 10. If this entry is NULL,
  298. no threads are ready at that priority. */
  299. THREAD_DECLARE TX_THREAD * _tx_thread_priority_list[TX_MAX_PRIORITIES];
  300. /* Define the global preempt disable variable. If this is non-zero, preemption is
  301. disabled. It is used internally by ThreadX to prevent preemption of a thread in
  302. the middle of a service that is resuming or suspending another thread. */
  303. THREAD_DECLARE volatile UINT _tx_thread_preempt_disable;
  304. /* Define the global function pointer for mutex cleanup on thread completion or
  305. termination. This pointer is setup during mutex initialization. */
  306. THREAD_DECLARE VOID (*_tx_thread_mutex_release)(TX_THREAD *thread_ptr);
  307. /* Define the global build options variable. This contains a bit map representing
  308. how the ThreadX library was built. The following are the bit field definitions:
  309. Bit(s) Meaning
  310. 31 TX_NOT_INTERRUPTABLE defined
  311. 30 TX_INLINE_THREAD_RESUME_SUSPEND define
  312. 29-24 Priority groups 1 -> 32 priorities
  313. 2 -> 64 priorities
  314. 3 -> 96 priorities
  315. ...
  316. 32 -> 1024 priorities
  317. 23 TX_TIMER_PROCESS_IN_ISR defined
  318. 22 TX_REACTIVATE_INLINE defined
  319. 21 TX_DISABLE_STACK_FILLING defined
  320. 20 TX_ENABLE_STACK_CHECKING defined
  321. 19 TX_DISABLE_PREEMPTION_THRESHOLD defined
  322. 18 TX_DISABLE_REDUNDANT_CLEARING defined
  323. 17 TX_DISABLE_NOTIFY_CALLBACKS defined
  324. 16 TX_BLOCK_POOL_ENABLE_PERFORMANCE_INFO defined
  325. 15 TX_BYTE_POOL_ENABLE_PERFORMANCE_INFO defined
  326. 14 TX_EVENT_FLAGS_ENABLE_PERFORMANCE_INFO defined
  327. 13 TX_MUTEX_ENABLE_PERFORMANCE_INFO defined
  328. 12 TX_QUEUE_ENABLE_PERFORMANCE_INFO defined
  329. 11 TX_SEMAPHORE_ENABLE_PERFORMANCE_INFO defined
  330. 10 TX_THREAD_ENABLE_PERFORMANCE_INFO defined
  331. 9 TX_TIMER_ENABLE_PERFORMANCE_INFO defined
  332. 8 TX_ENABLE_EVENT_TRACE defined
  333. 7 TX_ENABLE_EXECUTION_CHANGE_NOTIFY defined
  334. 6-0 Port Specific */
  335. THREAD_DECLARE ULONG _tx_build_options;
  336. #if defined(TX_ENABLE_STACK_CHECKING) || defined(TX_PORT_THREAD_STACK_ERROR_HANDLING)
  337. /* Define the global function pointer for stack error handling. If a stack error is
  338. detected and the application has registered a stack error handler, it will be
  339. called via this function pointer. */
  340. THREAD_DECLARE VOID (*_tx_thread_application_stack_error_handler)(TX_THREAD *thread_ptr);
  341. #endif
  342. #ifdef TX_THREAD_ENABLE_PERFORMANCE_INFO
  343. /* Define the total number of thread resumptions. Each time a thread enters the
  344. ready state this variable is incremented. */
  345. THREAD_DECLARE ULONG _tx_thread_performance_resume_count;
  346. /* Define the total number of thread suspensions. Each time a thread enters a
  347. suspended state this variable is incremented. */
  348. THREAD_DECLARE ULONG _tx_thread_performance_suspend_count;
  349. /* Define the total number of solicited thread preemptions. Each time a thread is
  350. preempted by directly calling a ThreadX service, this variable is incremented. */
  351. THREAD_DECLARE ULONG _tx_thread_performance_solicited_preemption_count;
  352. /* Define the total number of interrupt thread preemptions. Each time a thread is
  353. preempted as a result of an ISR calling a ThreadX service, this variable is
  354. incremented. */
  355. THREAD_DECLARE ULONG _tx_thread_performance_interrupt_preemption_count;
  356. /* Define the total number of priority inversions. Each time a thread is blocked by
  357. a mutex owned by a lower-priority thread, this variable is incremented. */
  358. THREAD_DECLARE ULONG _tx_thread_performance_priority_inversion_count;
  359. /* Define the total number of time-slices. Each time a time-slice operation is
  360. actually performed (another thread is setup for running) this variable is
  361. incremented. */
  362. THREAD_DECLARE ULONG _tx_thread_performance_time_slice_count;
  363. /* Define the total number of thread relinquish operations. Each time a thread
  364. relinquish operation is actually performed (another thread is setup for running)
  365. this variable is incremented. */
  366. THREAD_DECLARE ULONG _tx_thread_performance_relinquish_count;
  367. /* Define the total number of thread timeouts. Each time a thread has a
  368. timeout this variable is incremented. */
  369. THREAD_DECLARE ULONG _tx_thread_performance_timeout_count;
  370. /* Define the total number of thread wait aborts. Each time a thread's suspension
  371. is lifted by the tx_thread_wait_abort call this variable is incremented. */
  372. THREAD_DECLARE ULONG _tx_thread_performance_wait_abort_count;
  373. /* Define the total number of idle system thread returns. Each time a thread returns to
  374. an idle system (no other thread is ready to run) this variable is incremented. */
  375. THREAD_DECLARE ULONG _tx_thread_performance_idle_return_count;
  376. /* Define the total number of non-idle system thread returns. Each time a thread returns to
  377. a non-idle system (another thread is ready to run) this variable is incremented. */
  378. THREAD_DECLARE ULONG _tx_thread_performance_non_idle_return_count;
  379. /* Define the last TX_THREAD_EXECUTE_LOG_SIZE threads scheduled in ThreadX. This
  380. is a circular list, where the index points to the oldest entry. */
  381. THREAD_DECLARE ULONG _tx_thread_performance__execute_log_index;
  382. THREAD_DECLARE TX_THREAD * _tx_thread_performance_execute_log[TX_THREAD_EXECUTE_LOG_SIZE];
  383. #endif
  384. #endif