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 /**   Semaphore                                                           */
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_trace.h"
29 #include "tx_thread.h"
30 #include "tx_semaphore.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _tx_semaphore_prioritize                            PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    William E. Lamie, Microsoft Corporation                             */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function places the highest priority suspended thread at the   */
46 /*    front of the suspension list.  All other threads remain in the same */
47 /*    FIFO suspension order.                                              */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    semaphore_ptr                     Pointer to semaphore control block*/
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                            Completion status                 */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _tx_thread_system_preempt_check   Check for preemption              */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application Code                                                    */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
70 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_tx_semaphore_prioritize(TX_SEMAPHORE * semaphore_ptr)74 UINT  _tx_semaphore_prioritize(TX_SEMAPHORE *semaphore_ptr)
75 {
76 
77 TX_INTERRUPT_SAVE_AREA
78 
79 TX_THREAD       *thread_ptr;
80 TX_THREAD       *priority_thread_ptr;
81 TX_THREAD       *head_ptr;
82 UINT            suspended_count;
83 TX_THREAD       *next_thread;
84 TX_THREAD       *previous_thread;
85 UINT            list_changed;
86 
87 
88     /* Disable interrupts.  */
89     TX_DISABLE
90 
91     /* If trace is enabled, insert this event into the trace buffer.  */
92     TX_TRACE_IN_LINE_INSERT(TX_TRACE_SEMAPHORE_PRIORITIZE, semaphore_ptr, semaphore_ptr -> tx_semaphore_suspended_count, TX_POINTER_TO_ULONG_CONVERT(&suspended_count), 0, TX_TRACE_SEMAPHORE_EVENTS)
93 
94     /* Log this kernel call.  */
95     TX_EL_SEMAPHORE_PRIORITIZE_INSERT
96 
97     /* Pickup the suspended count.  */
98     suspended_count =  semaphore_ptr -> tx_semaphore_suspended_count;
99 
100     /* Determine if there are fewer than 2 suspended threads.  */
101     if (suspended_count < ((UINT) 2))
102     {
103 
104         /* Restore interrupts.  */
105         TX_RESTORE
106     }
107 
108     /* Determine if there how many threads are suspended on this semaphore.  */
109     else if (suspended_count == ((UINT) 2))
110     {
111 
112         /* Pickup the head pointer and the next pointer.  */
113         head_ptr =  semaphore_ptr -> tx_semaphore_suspension_list;
114         next_thread =  head_ptr -> tx_thread_suspended_next;
115 
116         /* Determine if the next suspended thread has a higher priority.  */
117         if ((next_thread -> tx_thread_priority) < (head_ptr -> tx_thread_priority))
118         {
119 
120             /* Yes, move the list head to the next thread.  */
121             semaphore_ptr -> tx_semaphore_suspension_list =  next_thread;
122         }
123 
124         /* Restore interrupts.  */
125         TX_RESTORE
126     }
127     else
128     {
129 
130         /* Remember the suspension count and head pointer.  */
131         head_ptr =   semaphore_ptr -> tx_semaphore_suspension_list;
132 
133         /* Default the highest priority thread to the thread at the front of the list.  */
134         priority_thread_ptr =  head_ptr;
135 
136         /* Setup search pointer.  */
137         thread_ptr =  priority_thread_ptr -> tx_thread_suspended_next;
138 
139         /* Disable preemption.  */
140         _tx_thread_preempt_disable++;
141 
142         /* Set the list changed flag to false.  */
143         list_changed =  TX_FALSE;
144 
145         /* Search through the list to find the highest priority thread.  */
146         do
147         {
148 
149             /* Is the current thread higher priority?  */
150             if (thread_ptr -> tx_thread_priority < priority_thread_ptr -> tx_thread_priority)
151             {
152 
153                 /* Yes, remember that this thread is the highest priority.  */
154                 priority_thread_ptr =  thread_ptr;
155             }
156 
157             /* Restore interrupts temporarily.  */
158             TX_RESTORE
159 
160             /* Disable interrupts again.  */
161             TX_DISABLE
162 
163             /* Determine if any changes to the list have occurred while
164                interrupts were enabled.  */
165 
166             /* Is the list head the same?  */
167             if (head_ptr != semaphore_ptr -> tx_semaphore_suspension_list)
168             {
169 
170                 /* The list head has changed, set the list changed flag.  */
171                 list_changed =  TX_TRUE;
172             }
173             else
174             {
175 
176                 /* Is the suspended count the same?  */
177                 if (suspended_count != semaphore_ptr -> tx_semaphore_suspended_count)
178                 {
179 
180                     /* The list head has changed, set the list changed flag.  */
181                     list_changed =  TX_TRUE;
182                 }
183             }
184 
185             /* Determine if the list has changed.  */
186             if (list_changed == TX_FALSE)
187             {
188 
189                 /* Yes, everything is the same... move the thread pointer to the next thread.  */
190                 thread_ptr =  thread_ptr -> tx_thread_suspended_next;
191             }
192             else
193             {
194 
195                 /* No, the list is been modified so we need to start the search over.  */
196 
197                 /* Save the suspension count and head pointer.  */
198                 head_ptr =   semaphore_ptr -> tx_semaphore_suspension_list;
199                 suspended_count =  semaphore_ptr -> tx_semaphore_suspended_count;
200 
201                 /* Default the highest priority thread to the thread at the front of the list.  */
202                 priority_thread_ptr =  head_ptr;
203 
204                 /* Setup search pointer.  */
205                 thread_ptr =  priority_thread_ptr -> tx_thread_suspended_next;
206 
207                 /* Reset the list changed flag.  */
208                 list_changed =  TX_FALSE;
209             }
210 
211         } while (thread_ptr != head_ptr);
212 
213         /* Release preemption.  */
214         _tx_thread_preempt_disable--;
215 
216         /* Now determine if the highest priority thread is at the front
217            of the list.  */
218         if (priority_thread_ptr != head_ptr)
219         {
220 
221             /* No, we need to move the highest priority suspended thread to the
222                front of the list.  */
223 
224             /* First, remove the highest priority thread by updating the
225                adjacent suspended threads.  */
226             next_thread =                                  priority_thread_ptr -> tx_thread_suspended_next;
227             previous_thread =                              priority_thread_ptr -> tx_thread_suspended_previous;
228             next_thread -> tx_thread_suspended_previous =  previous_thread;
229             previous_thread -> tx_thread_suspended_next =  next_thread;
230 
231             /* Now, link the highest priority thread at the front of the list.  */
232             previous_thread =                                      head_ptr -> tx_thread_suspended_previous;
233             priority_thread_ptr -> tx_thread_suspended_next =      head_ptr;
234             priority_thread_ptr -> tx_thread_suspended_previous =  previous_thread;
235             previous_thread -> tx_thread_suspended_next =          priority_thread_ptr;
236             head_ptr -> tx_thread_suspended_previous =             priority_thread_ptr;
237 
238             /* Move the list head pointer to the highest priority suspended thread.  */
239             semaphore_ptr -> tx_semaphore_suspension_list =  priority_thread_ptr;
240         }
241 
242         /* Restore interrupts.  */
243         TX_RESTORE
244 
245         /* Check for preemption.  */
246         _tx_thread_system_preempt_check();
247     }
248 
249     /* Return completion status.  */
250     return(TX_SUCCESS);
251 }
252 
253