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