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