1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Thread Queue Operations |
---|
5 | * @ingroup ScoreThreadQ |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2014. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/score/threadqimpl.h> |
---|
22 | #include <rtems/score/assert.h> |
---|
23 | #include <rtems/score/threaddispatch.h> |
---|
24 | #include <rtems/score/threadimpl.h> |
---|
25 | #include <rtems/score/status.h> |
---|
26 | #include <rtems/score/watchdogimpl.h> |
---|
27 | |
---|
28 | #define THREAD_QUEUE_INTEND_TO_BLOCK \ |
---|
29 | (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_INTEND_TO_BLOCK) |
---|
30 | |
---|
31 | #define THREAD_QUEUE_BLOCKED \ |
---|
32 | (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_BLOCKED) |
---|
33 | |
---|
34 | #define THREAD_QUEUE_READY_AGAIN \ |
---|
35 | (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_READY_AGAIN) |
---|
36 | |
---|
37 | void _Thread_queue_Enqueue_critical( |
---|
38 | Thread_queue_Queue *queue, |
---|
39 | const Thread_queue_Operations *operations, |
---|
40 | Thread_Control *the_thread, |
---|
41 | States_Control state, |
---|
42 | Watchdog_Interval timeout, |
---|
43 | ISR_lock_Context *lock_context |
---|
44 | ) |
---|
45 | { |
---|
46 | Per_CPU_Control *cpu_self; |
---|
47 | bool success; |
---|
48 | |
---|
49 | #if defined(RTEMS_MULTIPROCESSING) |
---|
50 | if ( _Thread_MP_Is_receive( the_thread ) && the_thread->receive_packet ) { |
---|
51 | the_thread = _Thread_MP_Allocate_proxy( state ); |
---|
52 | } |
---|
53 | #endif |
---|
54 | |
---|
55 | _Thread_Lock_set( the_thread, &queue->Lock ); |
---|
56 | |
---|
57 | the_thread->Wait.return_code = STATUS_SUCCESSFUL; |
---|
58 | _Thread_Wait_set_queue( the_thread, queue ); |
---|
59 | _Thread_Wait_set_operations( the_thread, operations ); |
---|
60 | |
---|
61 | ( *operations->enqueue )( queue, the_thread ); |
---|
62 | |
---|
63 | _Thread_Wait_flags_set( the_thread, THREAD_QUEUE_INTEND_TO_BLOCK ); |
---|
64 | cpu_self = _Thread_Dispatch_disable_critical( lock_context ); |
---|
65 | _Thread_queue_Queue_release( queue, lock_context ); |
---|
66 | |
---|
67 | /* |
---|
68 | * Set the blocking state for this thread queue in the thread. |
---|
69 | */ |
---|
70 | _Thread_Set_state( the_thread, state ); |
---|
71 | |
---|
72 | /* |
---|
73 | * If the thread wants to timeout, then schedule its timer. |
---|
74 | */ |
---|
75 | if ( timeout != WATCHDOG_NO_TIMEOUT ) { |
---|
76 | _Thread_Timer_insert_relative( |
---|
77 | the_thread, |
---|
78 | cpu_self, |
---|
79 | _Thread_Timeout, |
---|
80 | timeout |
---|
81 | ); |
---|
82 | } |
---|
83 | |
---|
84 | success = _Thread_Wait_flags_try_change( |
---|
85 | the_thread, |
---|
86 | THREAD_QUEUE_INTEND_TO_BLOCK, |
---|
87 | THREAD_QUEUE_BLOCKED |
---|
88 | ); |
---|
89 | if ( !success ) { |
---|
90 | _Thread_Remove_timer_and_unblock( the_thread, queue ); |
---|
91 | } |
---|
92 | |
---|
93 | _Thread_Dispatch_enable( cpu_self ); |
---|
94 | } |
---|
95 | |
---|
96 | bool _Thread_queue_Do_extract_locked( |
---|
97 | Thread_queue_Queue *queue, |
---|
98 | const Thread_queue_Operations *operations, |
---|
99 | Thread_Control *the_thread |
---|
100 | #if defined(RTEMS_MULTIPROCESSING) |
---|
101 | , |
---|
102 | const Thread_queue_Context *queue_context |
---|
103 | #endif |
---|
104 | ) |
---|
105 | { |
---|
106 | bool success; |
---|
107 | bool unblock; |
---|
108 | |
---|
109 | #if defined(RTEMS_MULTIPROCESSING) |
---|
110 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) { |
---|
111 | Thread_Proxy_control *the_proxy; |
---|
112 | Thread_queue_MP_callout mp_callout; |
---|
113 | |
---|
114 | the_proxy = (Thread_Proxy_control *) the_thread; |
---|
115 | mp_callout = queue_context->mp_callout; |
---|
116 | _Assert( mp_callout != NULL ); |
---|
117 | the_proxy->thread_queue_callout = queue_context->mp_callout; |
---|
118 | } |
---|
119 | #endif |
---|
120 | |
---|
121 | ( *operations->extract )( queue, the_thread ); |
---|
122 | |
---|
123 | /* |
---|
124 | * We must update the wait flags under protection of the current thread lock, |
---|
125 | * otherwise a _Thread_Timeout() running on another processor may interfere. |
---|
126 | */ |
---|
127 | success = _Thread_Wait_flags_try_change_critical( |
---|
128 | the_thread, |
---|
129 | THREAD_QUEUE_INTEND_TO_BLOCK, |
---|
130 | THREAD_QUEUE_READY_AGAIN |
---|
131 | ); |
---|
132 | if ( success ) { |
---|
133 | unblock = false; |
---|
134 | } else { |
---|
135 | _Assert( _Thread_Wait_flags_get( the_thread ) == THREAD_QUEUE_BLOCKED ); |
---|
136 | _Thread_Wait_flags_set( the_thread, THREAD_QUEUE_READY_AGAIN ); |
---|
137 | unblock = true; |
---|
138 | } |
---|
139 | |
---|
140 | _Thread_Wait_set_queue( the_thread, NULL ); |
---|
141 | _Thread_Wait_restore_default_operations( the_thread ); |
---|
142 | _Thread_Lock_restore_default( the_thread ); |
---|
143 | |
---|
144 | return unblock; |
---|
145 | } |
---|
146 | |
---|
147 | void _Thread_queue_Unblock_critical( |
---|
148 | bool unblock, |
---|
149 | Thread_queue_Queue *queue, |
---|
150 | Thread_Control *the_thread, |
---|
151 | ISR_lock_Context *lock_context |
---|
152 | ) |
---|
153 | { |
---|
154 | if ( unblock ) { |
---|
155 | Per_CPU_Control *cpu_self; |
---|
156 | |
---|
157 | cpu_self = _Thread_Dispatch_disable_critical( lock_context ); |
---|
158 | _Thread_queue_Queue_release( queue, lock_context ); |
---|
159 | |
---|
160 | _Thread_Remove_timer_and_unblock( the_thread, queue ); |
---|
161 | |
---|
162 | _Thread_Dispatch_enable( cpu_self ); |
---|
163 | } else { |
---|
164 | _Thread_queue_Queue_release( queue, lock_context ); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | void _Thread_queue_Extract_critical( |
---|
169 | Thread_queue_Queue *queue, |
---|
170 | const Thread_queue_Operations *operations, |
---|
171 | Thread_Control *the_thread, |
---|
172 | Thread_queue_Context *queue_context |
---|
173 | ) |
---|
174 | { |
---|
175 | bool unblock; |
---|
176 | |
---|
177 | unblock = _Thread_queue_Extract_locked( |
---|
178 | queue, |
---|
179 | operations, |
---|
180 | the_thread, |
---|
181 | queue_context |
---|
182 | ); |
---|
183 | |
---|
184 | _Thread_queue_Unblock_critical( |
---|
185 | unblock, |
---|
186 | queue, |
---|
187 | the_thread, |
---|
188 | &queue_context->Lock_context |
---|
189 | ); |
---|
190 | } |
---|
191 | |
---|
192 | void _Thread_queue_Extract( Thread_Control *the_thread ) |
---|
193 | { |
---|
194 | Thread_queue_Context queue_context; |
---|
195 | void *lock; |
---|
196 | Thread_queue_Queue *queue; |
---|
197 | |
---|
198 | _Thread_queue_Context_initialize( &queue_context ); |
---|
199 | lock = _Thread_Lock_acquire( the_thread, &queue_context.Lock_context ); |
---|
200 | |
---|
201 | queue = the_thread->Wait.queue; |
---|
202 | |
---|
203 | if ( queue != NULL ) { |
---|
204 | _SMP_Assert( lock == &queue->Lock ); |
---|
205 | |
---|
206 | _Thread_queue_Context_set_MP_callout( |
---|
207 | &queue_context, |
---|
208 | _Thread_queue_MP_callout_do_nothing |
---|
209 | ); |
---|
210 | _Thread_queue_Extract_critical( |
---|
211 | queue, |
---|
212 | the_thread->Wait.operations, |
---|
213 | the_thread, |
---|
214 | &queue_context |
---|
215 | ); |
---|
216 | } else { |
---|
217 | _Thread_Lock_release( lock, &queue_context.Lock_context ); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | Thread_Control *_Thread_queue_Do_dequeue( |
---|
222 | Thread_queue_Control *the_thread_queue, |
---|
223 | const Thread_queue_Operations *operations |
---|
224 | #if defined(RTEMS_MULTIPROCESSING) |
---|
225 | , |
---|
226 | Thread_queue_MP_callout mp_callout |
---|
227 | #endif |
---|
228 | ) |
---|
229 | { |
---|
230 | Thread_queue_Context queue_context; |
---|
231 | Thread_Control *the_thread; |
---|
232 | |
---|
233 | _Thread_queue_Context_initialize( &queue_context ); |
---|
234 | _Thread_queue_Context_set_MP_callout( &queue_context, mp_callout ); |
---|
235 | _Thread_queue_Acquire( the_thread_queue, &queue_context.Lock_context ); |
---|
236 | |
---|
237 | the_thread = _Thread_queue_First_locked( the_thread_queue, operations ); |
---|
238 | |
---|
239 | if ( the_thread != NULL ) { |
---|
240 | _SMP_Assert( the_thread->Lock.current == &the_thread_queue->Queue.Lock ); |
---|
241 | |
---|
242 | _Thread_queue_Extract_critical( |
---|
243 | &the_thread_queue->Queue, |
---|
244 | operations, |
---|
245 | the_thread, |
---|
246 | &queue_context |
---|
247 | ); |
---|
248 | } else { |
---|
249 | _Thread_queue_Release( the_thread_queue, &queue_context.Lock_context ); |
---|
250 | } |
---|
251 | |
---|
252 | return the_thread; |
---|
253 | } |
---|
254 | |
---|
255 | #if defined(RTEMS_MULTIPROCESSING) |
---|
256 | void _Thread_queue_Unblock_proxy( |
---|
257 | Thread_queue_Queue *queue, |
---|
258 | Thread_Control *the_thread |
---|
259 | ) |
---|
260 | { |
---|
261 | const Thread_queue_Object *the_queue_object; |
---|
262 | Thread_Proxy_control *the_proxy; |
---|
263 | Thread_queue_MP_callout mp_callout; |
---|
264 | |
---|
265 | the_queue_object = THREAD_QUEUE_QUEUE_TO_OBJECT( queue ); |
---|
266 | the_proxy = (Thread_Proxy_control *) the_thread; |
---|
267 | mp_callout = the_proxy->thread_queue_callout; |
---|
268 | ( *mp_callout )( the_thread, the_queue_object->Object.id ); |
---|
269 | |
---|
270 | _Thread_MP_Free_proxy( the_thread ); |
---|
271 | } |
---|
272 | #endif |
---|