1 | /* |
---|
2 | * Thread Queue Handler - Enqueue By Priority |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2009. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | */ |
---|
11 | |
---|
12 | #if HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <rtems/system.h> |
---|
17 | #include <rtems/score/chain.h> |
---|
18 | #include <rtems/score/isr.h> |
---|
19 | #include <rtems/score/object.h> |
---|
20 | #include <rtems/score/states.h> |
---|
21 | #include <rtems/score/thread.h> |
---|
22 | #include <rtems/score/threadq.h> |
---|
23 | #include <rtems/score/tqdata.h> |
---|
24 | |
---|
25 | /* |
---|
26 | * Support the user forcing the unrolling to be disabled. |
---|
27 | */ |
---|
28 | #if __RTEMS_DO_NOT_UNROLL_THREADQ_ENQUEUE_PRIORITY__ |
---|
29 | #undef CPU_UNROLL_ENQUEUE_PRIORITY |
---|
30 | #define CPU_UNROLL_ENQUEUE_PRIORITY FALSE |
---|
31 | #endif |
---|
32 | |
---|
33 | /* |
---|
34 | * _Thread_queue_Enqueue_priority |
---|
35 | * |
---|
36 | * This routine places a blocked thread on a priority thread queue. |
---|
37 | * |
---|
38 | * Input parameters: |
---|
39 | * the_thread_queue - pointer to threadq |
---|
40 | * thread - thread to insert |
---|
41 | * |
---|
42 | * Output parameters: NONE |
---|
43 | * |
---|
44 | * INTERRUPT LATENCY: |
---|
45 | * forward less than |
---|
46 | * forward equal |
---|
47 | */ |
---|
48 | |
---|
49 | Thread_blocking_operation_States _Thread_queue_Enqueue_priority ( |
---|
50 | Thread_queue_Control *the_thread_queue, |
---|
51 | Thread_Control *the_thread, |
---|
52 | ISR_Level *level_p |
---|
53 | ) |
---|
54 | { |
---|
55 | Priority_Control search_priority; |
---|
56 | Thread_Control *search_thread; |
---|
57 | ISR_Level level; |
---|
58 | Chain_Control *header; |
---|
59 | uint32_t header_index; |
---|
60 | Chain_Node *the_node; |
---|
61 | Chain_Node *next_node; |
---|
62 | Chain_Node *previous_node; |
---|
63 | Chain_Node *search_node; |
---|
64 | Priority_Control priority; |
---|
65 | States_Control block_state; |
---|
66 | |
---|
67 | _Chain_Initialize_empty( &the_thread->Wait.Block2n ); |
---|
68 | |
---|
69 | priority = the_thread->current_priority; |
---|
70 | header_index = _Thread_queue_Header_number( priority ); |
---|
71 | header = &the_thread_queue->Queues.Priority[ header_index ]; |
---|
72 | block_state = the_thread_queue->state; |
---|
73 | |
---|
74 | if ( _Thread_queue_Is_reverse_search( priority ) ) |
---|
75 | goto restart_reverse_search; |
---|
76 | |
---|
77 | restart_forward_search: |
---|
78 | search_priority = PRIORITY_MINIMUM - 1; |
---|
79 | _ISR_Disable( level ); |
---|
80 | search_thread = (Thread_Control *) _Chain_First( header ); |
---|
81 | while ( !_Chain_Is_tail( header, (Chain_Node *)search_thread ) ) { |
---|
82 | search_priority = search_thread->current_priority; |
---|
83 | if ( priority <= search_priority ) |
---|
84 | break; |
---|
85 | |
---|
86 | #if ( CPU_UNROLL_ENQUEUE_PRIORITY == TRUE ) |
---|
87 | search_thread = (Thread_Control *) search_thread->Object.Node.next; |
---|
88 | if ( _Chain_Is_tail( header, (Chain_Node *)search_thread ) ) |
---|
89 | break; |
---|
90 | search_priority = search_thread->current_priority; |
---|
91 | if ( priority <= search_priority ) |
---|
92 | break; |
---|
93 | #endif |
---|
94 | _ISR_Flash( level ); |
---|
95 | if ( !_States_Are_set( search_thread->current_state, block_state) ) { |
---|
96 | _ISR_Enable( level ); |
---|
97 | goto restart_forward_search; |
---|
98 | } |
---|
99 | search_thread = |
---|
100 | (Thread_Control *)search_thread->Object.Node.next; |
---|
101 | } |
---|
102 | |
---|
103 | if ( the_thread_queue->sync_state != |
---|
104 | THREAD_BLOCKING_OPERATION_NOTHING_HAPPENED ) |
---|
105 | goto synchronize; |
---|
106 | |
---|
107 | the_thread_queue->sync_state = THREAD_BLOCKING_OPERATION_SYNCHRONIZED; |
---|
108 | |
---|
109 | if ( priority == search_priority ) |
---|
110 | goto equal_priority; |
---|
111 | |
---|
112 | search_node = (Chain_Node *) search_thread; |
---|
113 | previous_node = search_node->previous; |
---|
114 | the_node = (Chain_Node *) the_thread; |
---|
115 | |
---|
116 | the_node->next = search_node; |
---|
117 | the_node->previous = previous_node; |
---|
118 | previous_node->next = the_node; |
---|
119 | search_node->previous = the_node; |
---|
120 | the_thread->Wait.queue = the_thread_queue; |
---|
121 | _ISR_Enable( level ); |
---|
122 | return THREAD_BLOCKING_OPERATION_NOTHING_HAPPENED; |
---|
123 | |
---|
124 | restart_reverse_search: |
---|
125 | search_priority = PRIORITY_MAXIMUM + 1; |
---|
126 | |
---|
127 | _ISR_Disable( level ); |
---|
128 | search_thread = (Thread_Control *) _Chain_Last( header ); |
---|
129 | while ( !_Chain_Is_head( header, (Chain_Node *)search_thread ) ) { |
---|
130 | search_priority = search_thread->current_priority; |
---|
131 | if ( priority >= search_priority ) |
---|
132 | break; |
---|
133 | #if ( CPU_UNROLL_ENQUEUE_PRIORITY == TRUE ) |
---|
134 | search_thread = (Thread_Control *) search_thread->Object.Node.previous; |
---|
135 | if ( _Chain_Is_head( header, (Chain_Node *)search_thread ) ) |
---|
136 | break; |
---|
137 | search_priority = search_thread->current_priority; |
---|
138 | if ( priority >= search_priority ) |
---|
139 | break; |
---|
140 | #endif |
---|
141 | _ISR_Flash( level ); |
---|
142 | if ( !_States_Are_set( search_thread->current_state, block_state) ) { |
---|
143 | _ISR_Enable( level ); |
---|
144 | goto restart_reverse_search; |
---|
145 | } |
---|
146 | search_thread = (Thread_Control *) |
---|
147 | search_thread->Object.Node.previous; |
---|
148 | } |
---|
149 | |
---|
150 | if ( the_thread_queue->sync_state != |
---|
151 | THREAD_BLOCKING_OPERATION_NOTHING_HAPPENED ) |
---|
152 | goto synchronize; |
---|
153 | |
---|
154 | the_thread_queue->sync_state = THREAD_BLOCKING_OPERATION_SYNCHRONIZED; |
---|
155 | |
---|
156 | if ( priority == search_priority ) |
---|
157 | goto equal_priority; |
---|
158 | |
---|
159 | search_node = (Chain_Node *) search_thread; |
---|
160 | next_node = search_node->next; |
---|
161 | the_node = (Chain_Node *) the_thread; |
---|
162 | |
---|
163 | the_node->next = next_node; |
---|
164 | the_node->previous = search_node; |
---|
165 | search_node->next = the_node; |
---|
166 | next_node->previous = the_node; |
---|
167 | the_thread->Wait.queue = the_thread_queue; |
---|
168 | _ISR_Enable( level ); |
---|
169 | return THREAD_BLOCKING_OPERATION_NOTHING_HAPPENED; |
---|
170 | |
---|
171 | equal_priority: /* add at end of priority group */ |
---|
172 | search_node = _Chain_Tail( &search_thread->Wait.Block2n ); |
---|
173 | previous_node = search_node->previous; |
---|
174 | the_node = (Chain_Node *) the_thread; |
---|
175 | |
---|
176 | the_node->next = search_node; |
---|
177 | the_node->previous = previous_node; |
---|
178 | previous_node->next = the_node; |
---|
179 | search_node->previous = the_node; |
---|
180 | the_thread->Wait.queue = the_thread_queue; |
---|
181 | _ISR_Enable( level ); |
---|
182 | return THREAD_BLOCKING_OPERATION_NOTHING_HAPPENED; |
---|
183 | |
---|
184 | synchronize: |
---|
185 | /* |
---|
186 | * An interrupt completed the thread's blocking request. |
---|
187 | * For example, the blocking thread could have been given |
---|
188 | * the mutex by an ISR or timed out. |
---|
189 | * |
---|
190 | * WARNING! Returning with interrupts disabled! |
---|
191 | */ |
---|
192 | *level_p = level; |
---|
193 | return the_thread_queue->sync_state; |
---|
194 | } |
---|