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_trace.h"
30 #include "tx_thread.h"
31 #ifndef TX_NO_TIMER
32 #include "tx_timer.h"
33 #endif
34 
35 
36 /**************************************************************************/
37 /*                                                                        */
38 /*  FUNCTION                                               RELEASE        */
39 /*                                                                        */
40 /*    _tx_thread_relinquish                               PORTABLE C      */
41 /*                                                           6.1          */
42 /*  AUTHOR                                                                */
43 /*                                                                        */
44 /*    William E. Lamie, Microsoft Corporation                             */
45 /*                                                                        */
46 /*  DESCRIPTION                                                           */
47 /*                                                                        */
48 /*    This function moves the currently executing thread to the end of    */
49 /*    the list of threads ready at the same priority. If no other threads */
50 /*    of the same or higher priority are ready, this function simply      */
51 /*    returns.                                                            */
52 /*                                                                        */
53 /*  INPUT                                                                 */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    None                                                                */
60 /*                                                                        */
61 /*  CALLS                                                                 */
62 /*                                                                        */
63 /*    _tx_thread_system_return              Return to the system          */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Application Code                                                    */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
74 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_tx_thread_relinquish(VOID)78 VOID  _tx_thread_relinquish(VOID)
79 {
80 
81 TX_INTERRUPT_SAVE_AREA
82 
83 UINT            priority;
84 TX_THREAD       *thread_ptr;
85 
86 
87     /* Pickup thread pointer.  */
88     TX_THREAD_GET_CURRENT(thread_ptr)
89 
90 #ifdef TX_ENABLE_STACK_CHECKING
91 
92     /* Check this thread's stack.  */
93     TX_THREAD_STACK_CHECK(thread_ptr)
94 #endif
95 
96     /* Disable interrupts.  */
97     TX_DISABLE
98 
99 #ifndef TX_NO_TIMER
100 
101     /* Reset time slice for current thread.  */
102     _tx_timer_time_slice =  thread_ptr -> tx_thread_new_time_slice;
103 #endif
104 
105     /* Pickup the thread's priority.  */
106     priority =  thread_ptr -> tx_thread_priority;
107 
108     /* Determine if there is another thread at the same priority.  */
109     if (thread_ptr -> tx_thread_ready_next != thread_ptr)
110     {
111 
112         /* Yes, there is another thread at this priority, make it the highest at
113            this priority level.  */
114         _tx_thread_priority_list[priority] =  thread_ptr -> tx_thread_ready_next;
115 
116         /* Mark the new thread as the one to execute.  */
117         _tx_thread_execute_ptr = thread_ptr -> tx_thread_ready_next;
118     }
119 
120     /* Determine if there is a higher-priority thread ready.  */
121     if (_tx_thread_highest_priority < priority)
122     {
123 
124         /* Yes, there is a higher priority thread ready to execute.  Make
125            it visible to the thread scheduler.  */
126         _tx_thread_execute_ptr =  _tx_thread_priority_list[_tx_thread_highest_priority];
127 
128         /* No need to clear the preempted bit in this case, since the currently running
129            thread must already have its preempted bit clear.  */
130     }
131 
132     /* If trace is enabled, insert this event into the trace buffer.  */
133     TX_TRACE_IN_LINE_INSERT(TX_TRACE_THREAD_RELINQUISH, &thread_ptr, TX_POINTER_TO_ULONG_CONVERT(_tx_thread_execute_ptr), 0, 0, TX_TRACE_THREAD_EVENTS)
134 
135     /* Log this kernel call.  */
136     TX_EL_THREAD_RELINQUISH_INSERT
137 
138     /* Restore previous interrupt posture.  */
139     TX_RESTORE
140 
141     /* Determine if this thread needs to return to the system.  */
142     if (_tx_thread_execute_ptr != thread_ptr)
143     {
144 
145 #ifdef TX_THREAD_ENABLE_PERFORMANCE_INFO
146 
147         /* Increment the number of thread relinquishes.  */
148         thread_ptr -> tx_thread_performance_relinquish_count++;
149 
150         /* Increment the total number of thread relinquish operations.  */
151         _tx_thread_performance_relinquish_count++;
152 
153         /* Increment the non-idle return count.  */
154         _tx_thread_performance_non_idle_return_count++;
155 #endif
156 
157 #ifdef TX_ENABLE_STACK_CHECKING
158 
159         /* Pickup the next execute pointer.  */
160         thread_ptr =  _tx_thread_execute_ptr;
161 
162         /* Check this thread's stack.  */
163         TX_THREAD_STACK_CHECK(thread_ptr)
164 #endif
165 
166         /* Transfer control to the system so the scheduler can execute
167            the next thread.  */
168         _tx_thread_system_return();
169     }
170 }
171 
172