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 /**************************************************************************/
14 /**                                                                       */
15 /** ThreadX Component                                                     */
16 /**                                                                       */
17 /**   Thread                                                              */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define TX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "tx_api.h"
28 #include "tx_thread.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _tx_thread_system_preempt_check                     PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    William E. Lamie, Microsoft Corporation                             */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function checks for preemption that could have occurred as a   */
44 /*    result scheduling activities occurring while the preempt disable    */
45 /*    flag was set.                                                       */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    None                                                                */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _tx_thread_system_return              Return to the system          */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    Other ThreadX Components                                            */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
68 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
69 /*                                            resulting in version 6.1    */
70 /*                                                                        */
71 /**************************************************************************/
_tx_thread_system_preempt_check(VOID)72 VOID  _tx_thread_system_preempt_check(VOID)
73 {
74 
75 ULONG           combined_flags;
76 TX_THREAD       *current_thread;
77 TX_THREAD       *thread_ptr;
78 
79 
80     /* Combine the system state and preempt disable flags into one for comparison.  */
81     TX_THREAD_SYSTEM_RETURN_CHECK(combined_flags)
82 
83     /* Determine if we are in a system state (ISR or Initialization) or internal preemption is disabled.  */
84     if (combined_flags == ((ULONG) 0))
85     {
86 
87         /* No, at thread execution level so continue checking for preemption.  */
88 
89         /* Pickup thread pointer.  */
90         TX_THREAD_GET_CURRENT(current_thread)
91 
92         /* Pickup the next execute pointer.  */
93         thread_ptr =  _tx_thread_execute_ptr;
94 
95         /* Determine if preemption should take place.  */
96         if (current_thread != thread_ptr)
97         {
98 
99 #ifdef TX_ENABLE_STACK_CHECKING
100 
101             /* Check this thread's stack.  */
102             TX_THREAD_STACK_CHECK(thread_ptr)
103 #endif
104 
105 
106 #ifdef TX_THREAD_ENABLE_PERFORMANCE_INFO
107 
108             /* Determine if an idle system return is present.  */
109             if (thread_ptr == TX_NULL)
110             {
111 
112                 /* Yes, increment the return to idle return count.  */
113                 _tx_thread_performance_idle_return_count++;
114             }
115             else
116             {
117 
118                 /* No, there is another thread ready to run and will be scheduled upon return.  */
119                 _tx_thread_performance_non_idle_return_count++;
120             }
121 #endif
122 
123             /* Return to the system so the higher priority thread can be scheduled.  */
124             _tx_thread_system_return();
125         }
126     }
127 }
128 
129