1 | /** |
---|
2 | * @file rtems/score/threadq.h |
---|
3 | * |
---|
4 | * Constants and Structures Associated with the Manipulation of Objects |
---|
5 | * |
---|
6 | * This include file contains all the constants and structures associated |
---|
7 | * with the manipulation of objects. |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * COPYRIGHT (c) 1989-2014. |
---|
12 | * On-Line Applications Research Corporation (OAR). |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.org/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef _RTEMS_SCORE_THREADQIMPL_H |
---|
20 | #define _RTEMS_SCORE_THREADQIMPL_H |
---|
21 | |
---|
22 | #include <rtems/score/threadq.h> |
---|
23 | #include <rtems/score/chainimpl.h> |
---|
24 | #include <rtems/score/rbtreeimpl.h> |
---|
25 | #include <rtems/score/scheduler.h> |
---|
26 | #include <rtems/score/thread.h> |
---|
27 | |
---|
28 | #ifdef __cplusplus |
---|
29 | extern "C" { |
---|
30 | #endif |
---|
31 | |
---|
32 | /** |
---|
33 | * @addtogroup ScoreThreadQueue |
---|
34 | */ |
---|
35 | /**@{*/ |
---|
36 | |
---|
37 | /** |
---|
38 | * @brief Thread queue with a layout compatible to struct _Thread_queue_Queue |
---|
39 | * defined in Newlib <sys/lock.h>. |
---|
40 | */ |
---|
41 | typedef struct { |
---|
42 | Thread_queue_Queue Queue; |
---|
43 | |
---|
44 | #if !defined(RTEMS_SMP) |
---|
45 | /* |
---|
46 | * The struct _Thread_queue_Queue definition is independent of the RTEMS |
---|
47 | * build configuration. Thus, the storage space for the SMP lock is always |
---|
48 | * present. In SMP configurations, the SMP lock is contained in the |
---|
49 | * Thread_queue_Queue. |
---|
50 | */ |
---|
51 | unsigned int reserved[2]; |
---|
52 | #endif |
---|
53 | } Thread_queue_Syslock_queue; |
---|
54 | |
---|
55 | RTEMS_INLINE_ROUTINE void _Thread_queue_Heads_initialize( |
---|
56 | Thread_queue_Heads *heads |
---|
57 | ) |
---|
58 | { |
---|
59 | #if defined(RTEMS_SMP) |
---|
60 | size_t i; |
---|
61 | |
---|
62 | for ( i = 0; i < _Scheduler_Count; ++i ) { |
---|
63 | _RBTree_Initialize_empty( &heads->Priority[ i ].Queue ); |
---|
64 | } |
---|
65 | #endif |
---|
66 | |
---|
67 | _Chain_Initialize_empty( &heads->Free_chain ); |
---|
68 | } |
---|
69 | |
---|
70 | RTEMS_INLINE_ROUTINE void _Thread_queue_Queue_initialize( |
---|
71 | Thread_queue_Queue *queue |
---|
72 | ) |
---|
73 | { |
---|
74 | queue->heads = NULL; |
---|
75 | #if defined(RTEMS_SMP) |
---|
76 | _SMP_ticket_lock_Initialize( &queue->Lock ); |
---|
77 | #endif |
---|
78 | } |
---|
79 | |
---|
80 | RTEMS_INLINE_ROUTINE void _Thread_queue_Queue_do_acquire_critical( |
---|
81 | Thread_queue_Queue *queue, |
---|
82 | #if defined(RTEMS_SMP) && defined(RTEMS_PROFILING) |
---|
83 | SMP_lock_Stats *lock_stats, |
---|
84 | #endif |
---|
85 | ISR_lock_Context *lock_context |
---|
86 | ) |
---|
87 | { |
---|
88 | #if defined(RTEMS_SMP) |
---|
89 | _SMP_ticket_lock_Acquire( |
---|
90 | &queue->Lock, |
---|
91 | lock_stats, |
---|
92 | &lock_context->Lock_context.Stats_context |
---|
93 | ); |
---|
94 | #else |
---|
95 | (void) queue; |
---|
96 | (void) lock_context; |
---|
97 | #endif |
---|
98 | } |
---|
99 | |
---|
100 | #if defined(RTEMS_SMP) && defined( RTEMS_PROFILING ) |
---|
101 | #define \ |
---|
102 | _Thread_queue_Queue_acquire_critical( queue, lock_stats, lock_context ) \ |
---|
103 | _Thread_queue_Queue_do_acquire_critical( queue, lock_stats, lock_context ) |
---|
104 | #else |
---|
105 | #define \ |
---|
106 | _Thread_queue_Queue_acquire_critical( queue, lock_stats, lock_context ) \ |
---|
107 | _Thread_queue_Queue_do_acquire_critical( queue, lock_context ) |
---|
108 | #endif |
---|
109 | |
---|
110 | RTEMS_INLINE_ROUTINE void _Thread_queue_Queue_release( |
---|
111 | Thread_queue_Queue *queue, |
---|
112 | ISR_lock_Context *lock_context |
---|
113 | ) |
---|
114 | { |
---|
115 | #if defined(RTEMS_SMP) |
---|
116 | _SMP_ticket_lock_Release( |
---|
117 | &queue->Lock, |
---|
118 | &lock_context->Lock_context.Stats_context |
---|
119 | ); |
---|
120 | #endif |
---|
121 | _ISR_lock_ISR_enable( lock_context ); |
---|
122 | } |
---|
123 | |
---|
124 | RTEMS_INLINE_ROUTINE void _Thread_queue_Acquire_critical( |
---|
125 | Thread_queue_Control *the_thread_queue, |
---|
126 | ISR_lock_Context *lock_context |
---|
127 | ) |
---|
128 | { |
---|
129 | _Thread_queue_Queue_acquire_critical( |
---|
130 | &the_thread_queue->Queue, |
---|
131 | &the_thread_queue->Lock_stats, |
---|
132 | lock_context |
---|
133 | ); |
---|
134 | } |
---|
135 | |
---|
136 | RTEMS_INLINE_ROUTINE void _Thread_queue_Acquire( |
---|
137 | Thread_queue_Control *the_thread_queue, |
---|
138 | ISR_lock_Context *lock_context |
---|
139 | ) |
---|
140 | { |
---|
141 | _ISR_lock_ISR_disable( lock_context ); |
---|
142 | _Thread_queue_Acquire_critical( the_thread_queue, lock_context ); |
---|
143 | } |
---|
144 | |
---|
145 | RTEMS_INLINE_ROUTINE void _Thread_queue_Release( |
---|
146 | Thread_queue_Control *the_thread_queue, |
---|
147 | ISR_lock_Context *lock_context |
---|
148 | ) |
---|
149 | { |
---|
150 | _Thread_queue_Queue_release( |
---|
151 | &the_thread_queue->Queue, |
---|
152 | lock_context |
---|
153 | ); |
---|
154 | } |
---|
155 | |
---|
156 | Thread_Control *_Thread_queue_Do_dequeue( |
---|
157 | Thread_queue_Control *the_thread_queue, |
---|
158 | const Thread_queue_Operations *operations |
---|
159 | #if defined(RTEMS_MULTIPROCESSING) |
---|
160 | , |
---|
161 | Thread_queue_MP_callout mp_callout, |
---|
162 | Objects_Id mp_id |
---|
163 | #endif |
---|
164 | ); |
---|
165 | |
---|
166 | /** |
---|
167 | * @brief Gets a pointer to a thread waiting on the_thread_queue. |
---|
168 | * |
---|
169 | * This function returns a pointer to a thread waiting on |
---|
170 | * the_thread_queue. The selection of this thread is based on |
---|
171 | * the discipline of the_thread_queue. If no threads are waiting |
---|
172 | * on the_thread_queue, then NULL is returned. |
---|
173 | * |
---|
174 | * - INTERRUPT LATENCY: |
---|
175 | * + single case |
---|
176 | */ |
---|
177 | #if defined(RTEMS_MULTIPROCESSING) |
---|
178 | #define _Thread_queue_Dequeue( \ |
---|
179 | the_thread_queue, \ |
---|
180 | operations, \ |
---|
181 | mp_callout, \ |
---|
182 | mp_id \ |
---|
183 | ) \ |
---|
184 | _Thread_queue_Do_dequeue( \ |
---|
185 | the_thread_queue, \ |
---|
186 | operations, \ |
---|
187 | mp_callout, \ |
---|
188 | mp_id \ |
---|
189 | ) |
---|
190 | #else |
---|
191 | #define _Thread_queue_Dequeue( \ |
---|
192 | the_thread_queue, \ |
---|
193 | operations, \ |
---|
194 | mp_callout, \ |
---|
195 | mp_id \ |
---|
196 | ) \ |
---|
197 | _Thread_queue_Do_dequeue( \ |
---|
198 | the_thread_queue, \ |
---|
199 | operations \ |
---|
200 | ) |
---|
201 | #endif |
---|
202 | |
---|
203 | /** |
---|
204 | * @brief Blocks the thread and places it on the thread queue. |
---|
205 | * |
---|
206 | * This enqueues the thread on the thread queue, blocks the thread, and |
---|
207 | * optionally starts the thread timer in case the timeout interval is not |
---|
208 | * WATCHDOG_NO_TIMEOUT. |
---|
209 | * |
---|
210 | * The caller must be the owner of the thread queue lock. This function will |
---|
211 | * release the thread queue lock and register it as the new thread lock. |
---|
212 | * Thread dispatching is disabled before the thread queue lock is released. |
---|
213 | * Thread dispatching is enabled once the sequence to block the thread is |
---|
214 | * complete. The operation to enqueue the thread on the queue is protected by |
---|
215 | * the thread queue lock. This makes it possible to use the thread queue lock |
---|
216 | * to protect the state of objects embedding the thread queue and directly |
---|
217 | * enter _Thread_queue_Enqueue_critical() in case the thread must block. |
---|
218 | * |
---|
219 | * @code |
---|
220 | * #include <rtems/score/threadqimpl.h> |
---|
221 | * #include <rtems/score/statesimpl.h> |
---|
222 | * |
---|
223 | * #define MUTEX_TQ_OPERATIONS &_Thread_queue_Operations_priority |
---|
224 | * |
---|
225 | * typedef struct { |
---|
226 | * Thread_queue_Control Queue; |
---|
227 | * Thread_Control *owner; |
---|
228 | * } Mutex; |
---|
229 | * |
---|
230 | * void _Mutex_Obtain( Mutex *mutex ) |
---|
231 | * { |
---|
232 | * ISR_lock_Context lock_context; |
---|
233 | * Thread_Control *executing; |
---|
234 | * |
---|
235 | * _Thread_queue_Acquire( &mutex->Queue, &lock_context ); |
---|
236 | * |
---|
237 | * executing = _Thread_Executing; |
---|
238 | * |
---|
239 | * if ( mutex->owner == NULL ) { |
---|
240 | * mutex->owner = executing; |
---|
241 | * _Thread_queue_Release( &mutex->Queue, &lock_context ); |
---|
242 | * } else { |
---|
243 | * _Thread_queue_Enqueue_critical( |
---|
244 | * &mutex->Queue.Queue, |
---|
245 | * MUTEX_TQ_OPERATIONS, |
---|
246 | * executing, |
---|
247 | * STATES_WAITING_FOR_MUTEX, |
---|
248 | * WATCHDOG_NO_TIMEOUT, |
---|
249 | * 0, |
---|
250 | * &lock_context |
---|
251 | * ); |
---|
252 | * } |
---|
253 | * } |
---|
254 | * @endcode |
---|
255 | * |
---|
256 | * @param[in] queue The actual thread queue. |
---|
257 | * @param[in] operations The thread queue operations. |
---|
258 | * @param[in] the_thread The thread to enqueue. |
---|
259 | * @param[in] state The new state of the thread. |
---|
260 | * @param[in] timeout Interval to wait. Use WATCHDOG_NO_TIMEOUT to block |
---|
261 | * potentially forever. |
---|
262 | * @param[in] timeout_code The return code in case a timeout occurs. |
---|
263 | * @param[in] lock_context The lock context of the lock acquire. |
---|
264 | */ |
---|
265 | void _Thread_queue_Enqueue_critical( |
---|
266 | Thread_queue_Queue *queue, |
---|
267 | const Thread_queue_Operations *operations, |
---|
268 | Thread_Control *the_thread, |
---|
269 | States_Control state, |
---|
270 | Watchdog_Interval timeout, |
---|
271 | uint32_t timeout_code, |
---|
272 | ISR_lock_Context *lock_context |
---|
273 | ); |
---|
274 | |
---|
275 | /** |
---|
276 | * @brief Acquires the thread queue lock and calls |
---|
277 | * _Thread_queue_Enqueue_critical(). |
---|
278 | */ |
---|
279 | RTEMS_INLINE_ROUTINE void _Thread_queue_Enqueue( |
---|
280 | Thread_queue_Control *the_thread_queue, |
---|
281 | const Thread_queue_Operations *operations, |
---|
282 | Thread_Control *the_thread, |
---|
283 | States_Control state, |
---|
284 | Watchdog_Interval timeout, |
---|
285 | uint32_t timeout_code |
---|
286 | ) |
---|
287 | { |
---|
288 | ISR_lock_Context lock_context; |
---|
289 | |
---|
290 | _Thread_queue_Acquire( the_thread_queue, &lock_context ); |
---|
291 | _Thread_queue_Enqueue_critical( |
---|
292 | &the_thread_queue->Queue, |
---|
293 | operations, |
---|
294 | the_thread, |
---|
295 | state, |
---|
296 | timeout, |
---|
297 | timeout_code, |
---|
298 | &lock_context |
---|
299 | ); |
---|
300 | } |
---|
301 | |
---|
302 | bool _Thread_queue_Do_extract_locked( |
---|
303 | Thread_queue_Queue *queue, |
---|
304 | const Thread_queue_Operations *operations, |
---|
305 | Thread_Control *the_thread |
---|
306 | #if defined(RTEMS_MULTIPROCESSING) |
---|
307 | , |
---|
308 | Thread_queue_MP_callout mp_callout, |
---|
309 | Objects_Id mp_id |
---|
310 | #endif |
---|
311 | ); |
---|
312 | |
---|
313 | /** |
---|
314 | * @brief Extracts the thread from the thread queue, restores the default wait |
---|
315 | * operations and restores the default thread lock. |
---|
316 | * |
---|
317 | * The caller must be the owner of the thread queue lock. The thread queue |
---|
318 | * lock is not released. |
---|
319 | * |
---|
320 | * @param[in] queue The actual thread queue. |
---|
321 | * @param[in] operations The thread queue operations. |
---|
322 | * @param[in] the_thread The thread to extract. |
---|
323 | * @param[in] mp_callout Callout to unblock the thread in case it is actually a |
---|
324 | * thread proxy. This parameter is only used on multiprocessing |
---|
325 | * configurations. |
---|
326 | * @param[in] mp_id Object identifier of the object containing the thread |
---|
327 | * queue. This parameter is only used on multiprocessing configurations. |
---|
328 | * |
---|
329 | * @return Returns the unblock indicator for _Thread_queue_Unblock_critical(). |
---|
330 | * True indicates, that this thread must be unblocked by the scheduler later in |
---|
331 | * _Thread_queue_Unblock_critical(), and false otherwise. In case false is |
---|
332 | * returned, then the thread queue enqueue procedure was interrupted. Thus it |
---|
333 | * will unblock itself and the thread wait information is no longer accessible, |
---|
334 | * since this thread may already block on another resource in an SMP |
---|
335 | * configuration. |
---|
336 | */ |
---|
337 | #if defined(RTEMS_MULTIPROCESSING) |
---|
338 | #define _Thread_queue_Extract_locked( \ |
---|
339 | unblock, \ |
---|
340 | queue, \ |
---|
341 | the_thread, \ |
---|
342 | mp_callout, \ |
---|
343 | mp_id \ |
---|
344 | ) \ |
---|
345 | _Thread_queue_Do_extract_locked( \ |
---|
346 | unblock, \ |
---|
347 | queue, \ |
---|
348 | the_thread, \ |
---|
349 | mp_callout, \ |
---|
350 | mp_id \ |
---|
351 | ) |
---|
352 | #else |
---|
353 | #define _Thread_queue_Extract_locked( \ |
---|
354 | unblock, \ |
---|
355 | queue, \ |
---|
356 | the_thread, \ |
---|
357 | mp_callout, \ |
---|
358 | mp_id \ |
---|
359 | ) \ |
---|
360 | _Thread_queue_Do_extract_locked( \ |
---|
361 | unblock, \ |
---|
362 | queue, \ |
---|
363 | the_thread \ |
---|
364 | ) |
---|
365 | #endif |
---|
366 | |
---|
367 | void _Thread_queue_Do_unblock_critical( |
---|
368 | bool unblock, |
---|
369 | Thread_queue_Queue *queue, |
---|
370 | Thread_Control *the_thread, |
---|
371 | #if defined(RTEMS_MULTIPROCESSING) |
---|
372 | Thread_queue_MP_callout mp_callout, |
---|
373 | Objects_Id mp_id, |
---|
374 | #endif |
---|
375 | ISR_lock_Context *lock_context |
---|
376 | ); |
---|
377 | |
---|
378 | /** |
---|
379 | * @brief Unblocks the thread which was on the thread queue before. |
---|
380 | * |
---|
381 | * The caller must be the owner of the thread queue lock. This function will |
---|
382 | * release the thread queue lock. Thread dispatching is disabled before the |
---|
383 | * thread queue lock is released and an unblock is necessary. Thread |
---|
384 | * dispatching is enabled once the sequence to unblock the thread is complete. |
---|
385 | * |
---|
386 | * @param[in] unblock The unblock indicator returned by |
---|
387 | * _Thread_queue_Extract_locked(). |
---|
388 | * @param[in] queue The actual thread queue. |
---|
389 | * @param[in] the_thread The thread to extract. |
---|
390 | * @param[in] mp_callout Callout to unblock the thread in case it is actually a |
---|
391 | * thread proxy. This parameter is only used on multiprocessing |
---|
392 | * configurations. |
---|
393 | * @param[in] mp_id Object identifier of the object containing the thread |
---|
394 | * queue. This parameter is only used on multiprocessing configurations. |
---|
395 | * @param[in] lock_context The lock context of the lock acquire. |
---|
396 | */ |
---|
397 | #if defined(RTEMS_MULTIPROCESSING) |
---|
398 | #define _Thread_queue_Unblock_critical( \ |
---|
399 | unblock, \ |
---|
400 | queue, \ |
---|
401 | the_thread, \ |
---|
402 | mp_callout, \ |
---|
403 | mp_id, \ |
---|
404 | lock_context \ |
---|
405 | ) \ |
---|
406 | _Thread_queue_Do_unblock_critical( \ |
---|
407 | unblock, \ |
---|
408 | queue, \ |
---|
409 | the_thread, \ |
---|
410 | mp_callout, \ |
---|
411 | mp_id, \ |
---|
412 | lock_context \ |
---|
413 | ) |
---|
414 | #else |
---|
415 | #define _Thread_queue_Unblock_critical( \ |
---|
416 | unblock, \ |
---|
417 | queue, \ |
---|
418 | the_thread, \ |
---|
419 | mp_callout, \ |
---|
420 | mp_id, \ |
---|
421 | lock_context \ |
---|
422 | ) \ |
---|
423 | _Thread_queue_Do_unblock_critical( \ |
---|
424 | unblock, \ |
---|
425 | queue, \ |
---|
426 | the_thread, \ |
---|
427 | lock_context \ |
---|
428 | ) |
---|
429 | #endif |
---|
430 | |
---|
431 | void _Thread_queue_Do_extract_critical( |
---|
432 | Thread_queue_Queue *queue, |
---|
433 | const Thread_queue_Operations *operations, |
---|
434 | Thread_Control *the_thread, |
---|
435 | #if defined(RTEMS_MULTIPROCESSING) |
---|
436 | Thread_queue_MP_callout mp_callout, |
---|
437 | Objects_Id mp_id, |
---|
438 | #endif |
---|
439 | ISR_lock_Context *lock_context |
---|
440 | ); |
---|
441 | |
---|
442 | /** |
---|
443 | * @brief Extracts the thread from the thread queue and unblocks it. |
---|
444 | * |
---|
445 | * The caller must be the owner of the thread queue lock. This function will |
---|
446 | * release the thread queue lock and restore the default thread lock. Thread |
---|
447 | * dispatching is disabled before the thread queue lock is released and an |
---|
448 | * unblock is necessary. Thread dispatching is enabled once the sequence to |
---|
449 | * unblock the thread is complete. This makes it possible to use the thread |
---|
450 | * queue lock to protect the state of objects embedding the thread queue and |
---|
451 | * directly enter _Thread_queue_Extract_critical() to finalize an operation in |
---|
452 | * case a waiting thread exists. |
---|
453 | * |
---|
454 | * @code |
---|
455 | * #include <rtems/score/threadqimpl.h> |
---|
456 | * |
---|
457 | * typedef struct { |
---|
458 | * Thread_queue_Control Queue; |
---|
459 | * Thread_Control *owner; |
---|
460 | * } Mutex; |
---|
461 | * |
---|
462 | * void _Mutex_Release( Mutex *mutex ) |
---|
463 | * { |
---|
464 | * ISR_lock_Context lock_context; |
---|
465 | * Thread_Control *first; |
---|
466 | * |
---|
467 | * _Thread_queue_Acquire( &mutex->Queue, &lock_context ); |
---|
468 | * |
---|
469 | * first = _Thread_queue_First_locked( &mutex->Queue ); |
---|
470 | * mutex->owner = first; |
---|
471 | * |
---|
472 | * if ( first != NULL ) { |
---|
473 | * _Thread_queue_Extract_critical( |
---|
474 | * &mutex->Queue.Queue, |
---|
475 | * mutex->Queue.operations, |
---|
476 | * first, |
---|
477 | * NULL, |
---|
478 | * 0, |
---|
479 | * &lock_context |
---|
480 | * ); |
---|
481 | * } |
---|
482 | * @endcode |
---|
483 | * |
---|
484 | * @param[in] queue The actual thread queue. |
---|
485 | * @param[in] operations The thread queue operations. |
---|
486 | * @param[in] the_thread The thread to extract. |
---|
487 | * @param[in] mp_callout Callout to unblock the thread in case it is actually a |
---|
488 | * thread proxy. This parameter is only used on multiprocessing |
---|
489 | * configurations. |
---|
490 | * @param[in] mp_id Object identifier of the object containing the thread |
---|
491 | * queue. This parameter is only used on multiprocessing configurations. |
---|
492 | * @param[in] lock_context The lock context of the lock acquire. |
---|
493 | */ |
---|
494 | #if defined(RTEMS_MULTIPROCESSING) |
---|
495 | #define _Thread_queue_Extract_critical( \ |
---|
496 | queue, \ |
---|
497 | operations, \ |
---|
498 | the_thread, \ |
---|
499 | mp_callout, \ |
---|
500 | mp_id, \ |
---|
501 | lock_context \ |
---|
502 | ) \ |
---|
503 | _Thread_queue_Do_extract_critical( \ |
---|
504 | queue, \ |
---|
505 | operations, \ |
---|
506 | the_thread, \ |
---|
507 | mp_callout, \ |
---|
508 | mp_id, \ |
---|
509 | lock_context \ |
---|
510 | ) |
---|
511 | #else |
---|
512 | #define _Thread_queue_Extract_critical( \ |
---|
513 | queue, \ |
---|
514 | operations, \ |
---|
515 | the_thread, \ |
---|
516 | mp_callout, \ |
---|
517 | mp_id, \ |
---|
518 | lock_context \ |
---|
519 | ) \ |
---|
520 | _Thread_queue_Do_extract_critical( \ |
---|
521 | queue, \ |
---|
522 | operations, \ |
---|
523 | the_thread, \ |
---|
524 | lock_context \ |
---|
525 | ) |
---|
526 | #endif |
---|
527 | |
---|
528 | /** |
---|
529 | * @brief Extracts thread from thread queue. |
---|
530 | * |
---|
531 | * This routine removes @a the_thread its thread queue |
---|
532 | * and cancels any timeouts associated with this blocking. |
---|
533 | * |
---|
534 | * @param[in] the_thread is the pointer to a thread control block that |
---|
535 | * is to be removed |
---|
536 | */ |
---|
537 | void _Thread_queue_Extract( Thread_Control *the_thread ); |
---|
538 | |
---|
539 | /** |
---|
540 | * @brief Extracts the_thread from the_thread_queue. |
---|
541 | * |
---|
542 | * This routine extracts the_thread from the_thread_queue |
---|
543 | * and ensures that if there is a proxy for this task on |
---|
544 | * another node, it is also dealt with. |
---|
545 | */ |
---|
546 | void _Thread_queue_Extract_with_proxy( |
---|
547 | Thread_Control *the_thread |
---|
548 | ); |
---|
549 | |
---|
550 | RTEMS_INLINE_ROUTINE bool _Thread_queue_Is_empty( |
---|
551 | const Thread_queue_Queue *queue |
---|
552 | ) |
---|
553 | { |
---|
554 | return queue->heads == NULL; |
---|
555 | } |
---|
556 | |
---|
557 | /** |
---|
558 | * @brief Returns the first thread on the thread queue if it exists, otherwise |
---|
559 | * @c NULL. |
---|
560 | * |
---|
561 | * The caller must be the owner of the thread queue lock. The thread queue |
---|
562 | * lock is not released. |
---|
563 | * |
---|
564 | * @param[in] the_thread_queue The thread queue. |
---|
565 | * @param[in] operations The thread queue operations. |
---|
566 | * |
---|
567 | * @retval NULL No thread is present on the thread queue. |
---|
568 | * @retval first The first thread on the thread queue according to the enqueue |
---|
569 | * order. |
---|
570 | */ |
---|
571 | RTEMS_INLINE_ROUTINE Thread_Control *_Thread_queue_First_locked( |
---|
572 | Thread_queue_Control *the_thread_queue, |
---|
573 | const Thread_queue_Operations *operations |
---|
574 | ) |
---|
575 | { |
---|
576 | Thread_queue_Heads *heads = the_thread_queue->Queue.heads; |
---|
577 | |
---|
578 | if ( heads != NULL ) { |
---|
579 | return ( *operations->first )( heads ); |
---|
580 | } else { |
---|
581 | return NULL; |
---|
582 | } |
---|
583 | } |
---|
584 | |
---|
585 | /** |
---|
586 | * @brief Returns the first thread on the thread queue if it exists, otherwise |
---|
587 | * @c NULL. |
---|
588 | * |
---|
589 | * @param[in] the_thread_queue The thread queue. |
---|
590 | * |
---|
591 | * @retval NULL No thread is present on the thread queue. |
---|
592 | * @retval first The first thread on the thread queue according to the enqueue |
---|
593 | * order. |
---|
594 | */ |
---|
595 | Thread_Control *_Thread_queue_First( |
---|
596 | Thread_queue_Control *the_thread_queue, |
---|
597 | const Thread_queue_Operations *operations |
---|
598 | ); |
---|
599 | |
---|
600 | /** |
---|
601 | * @brief Thread queue flush filter function. |
---|
602 | * |
---|
603 | * Called under protection of the thread queue lock by |
---|
604 | * _Thread_queue_Flush_critical() to optionally alter the thread wait |
---|
605 | * information and control the iteration. |
---|
606 | * |
---|
607 | * @param the_thread The thread to extract. This is the first parameter to |
---|
608 | * optimize for architectures that use the same register for the first |
---|
609 | * parameter and the return value. |
---|
610 | * @param queue The actual thread queue. |
---|
611 | * @param lock_context The lock context of the lock acquire. May be used to |
---|
612 | * pass additional data to the filter function via an overlay structure. The |
---|
613 | * filter function should not release or acquire the thread queue lock. |
---|
614 | * |
---|
615 | * @retval the_thread Extract this thread. |
---|
616 | * @retval NULL Do not extract this thread and stop the thread queue flush |
---|
617 | * operation. Threads that are already extracted will complete the flush |
---|
618 | * operation. |
---|
619 | */ |
---|
620 | typedef Thread_Control *( *Thread_queue_Flush_filter )( |
---|
621 | Thread_Control *the_thread, |
---|
622 | Thread_queue_Queue *queue, |
---|
623 | ISR_lock_Context *lock_context |
---|
624 | ); |
---|
625 | |
---|
626 | size_t _Thread_queue_Do_flush_critical( |
---|
627 | Thread_queue_Queue *queue, |
---|
628 | const Thread_queue_Operations *operations, |
---|
629 | Thread_queue_Flush_filter filter, |
---|
630 | #if defined(RTEMS_MULTIPROCESSING) |
---|
631 | Thread_queue_MP_callout mp_callout, |
---|
632 | Objects_Id mp_id, |
---|
633 | #endif |
---|
634 | ISR_lock_Context *lock_context |
---|
635 | ); |
---|
636 | |
---|
637 | /** |
---|
638 | * @brief Unblocks all threads enqueued on the thread queue. |
---|
639 | * |
---|
640 | * This function iteratively extracts the first enqueued thread of the thread |
---|
641 | * queue until the thread queue is empty or the filter function indicates a |
---|
642 | * stop. The thread timers of the extracted threads are cancelled. The |
---|
643 | * extracted threads are unblocked. |
---|
644 | * |
---|
645 | * @param queue The actual thread queue. |
---|
646 | * @param operations The thread queue operations. |
---|
647 | * @param filter The filter functions is called for each thread to extract from |
---|
648 | * the thread queue. It may be used to alter the thread under protection of |
---|
649 | * the thread queue lock, for example to set the thread wait return code. |
---|
650 | * The return value of the filter function controls if the thread queue flush |
---|
651 | * operation should stop or continue. |
---|
652 | * @param mp_callout Callout to extract the proxy of a remote thread. This |
---|
653 | * parameter is only used on multiprocessing configurations. |
---|
654 | * @param mp_id Object identifier of the object containing the thread queue. |
---|
655 | * This parameter is only used on multiprocessing configurations. |
---|
656 | * |
---|
657 | * @return The count of extracted threads. |
---|
658 | */ |
---|
659 | #if defined(RTEMS_MULTIPROCESSING) |
---|
660 | #define _Thread_queue_Flush_critical( \ |
---|
661 | queue, \ |
---|
662 | operations, \ |
---|
663 | filter, \ |
---|
664 | mp_callout, \ |
---|
665 | mp_id, \ |
---|
666 | lock_context \ |
---|
667 | ) \ |
---|
668 | _Thread_queue_Do_flush_critical( \ |
---|
669 | queue, \ |
---|
670 | operations, \ |
---|
671 | filter, \ |
---|
672 | mp_callout, \ |
---|
673 | mp_id, \ |
---|
674 | lock_context \ |
---|
675 | ) |
---|
676 | #else |
---|
677 | #define _Thread_queue_Flush_critical( \ |
---|
678 | queue, \ |
---|
679 | operations, \ |
---|
680 | filter, \ |
---|
681 | mp_callout, \ |
---|
682 | mp_id, \ |
---|
683 | lock_context \ |
---|
684 | ) \ |
---|
685 | _Thread_queue_Do_flush_critical( \ |
---|
686 | queue, \ |
---|
687 | operations, \ |
---|
688 | filter, \ |
---|
689 | lock_context \ |
---|
690 | ) |
---|
691 | #endif |
---|
692 | |
---|
693 | void _Thread_queue_Initialize( Thread_queue_Control *the_thread_queue ); |
---|
694 | |
---|
695 | #if defined(RTEMS_SMP) && defined(RTEMS_PROFILING) |
---|
696 | #define THREAD_QUEUE_INITIALIZER( name ) \ |
---|
697 | { \ |
---|
698 | .Queue = { \ |
---|
699 | .heads = NULL, \ |
---|
700 | .Lock = SMP_TICKET_LOCK_INITIALIZER, \ |
---|
701 | }, \ |
---|
702 | .Lock_stats = SMP_LOCK_STATS_INITIALIZER( name ) \ |
---|
703 | } |
---|
704 | #elif defined(RTEMS_SMP) |
---|
705 | #define THREAD_QUEUE_INITIALIZER( name ) \ |
---|
706 | { \ |
---|
707 | .Queue = { \ |
---|
708 | .heads = NULL, \ |
---|
709 | .Lock = SMP_TICKET_LOCK_INITIALIZER, \ |
---|
710 | } \ |
---|
711 | } |
---|
712 | #else |
---|
713 | #define THREAD_QUEUE_INITIALIZER( name ) \ |
---|
714 | { .Queue = { .heads = NULL } } |
---|
715 | #endif |
---|
716 | |
---|
717 | RTEMS_INLINE_ROUTINE void _Thread_queue_Destroy( |
---|
718 | Thread_queue_Control *the_thread_queue |
---|
719 | ) |
---|
720 | { |
---|
721 | #if defined(RTEMS_SMP) |
---|
722 | _SMP_ticket_lock_Destroy( &the_thread_queue->Queue.Lock ); |
---|
723 | _SMP_lock_Stats_destroy( &the_thread_queue->Lock_stats ); |
---|
724 | #endif |
---|
725 | } |
---|
726 | |
---|
727 | /** |
---|
728 | * @brief Boosts the priority of the thread if threads of another scheduler |
---|
729 | * instance are enqueued on the thread queue. |
---|
730 | * |
---|
731 | * The thread queue must use the priority waiting discipline. |
---|
732 | * |
---|
733 | * @param[in] queue The actual thread queue. |
---|
734 | * @param[in] the_thread The thread to boost the priority if necessary. |
---|
735 | */ |
---|
736 | #if defined(RTEMS_SMP) |
---|
737 | void _Thread_queue_Boost_priority( |
---|
738 | Thread_queue_Queue *queue, |
---|
739 | Thread_Control *the_thread |
---|
740 | ); |
---|
741 | #else |
---|
742 | RTEMS_INLINE_ROUTINE void _Thread_queue_Boost_priority( |
---|
743 | Thread_queue_Queue *queue, |
---|
744 | Thread_Control *the_thread |
---|
745 | ) |
---|
746 | { |
---|
747 | (void) queue; |
---|
748 | (void) the_thread; |
---|
749 | } |
---|
750 | #endif |
---|
751 | |
---|
752 | /** |
---|
753 | * @brief Compare two thread's priority for RBTree Insertion. |
---|
754 | * |
---|
755 | * @param[in] left points to the left thread's RBnode |
---|
756 | * @param[in] right points to the right thread's RBnode |
---|
757 | * |
---|
758 | * @retval 1 The @a left node is more important than @a right node. |
---|
759 | * @retval 0 The @a left node is of equal importance with @a right node. |
---|
760 | * @retval 1 The @a left node is less important than @a right node. |
---|
761 | */ |
---|
762 | RBTree_Compare_result _Thread_queue_Compare_priority( |
---|
763 | const RBTree_Node *left, |
---|
764 | const RBTree_Node *right |
---|
765 | ); |
---|
766 | |
---|
767 | #if defined(RTEMS_MULTIPROCESSING) |
---|
768 | void _Thread_queue_MP_callout_do_nothing( |
---|
769 | Thread_Control *the_proxy, |
---|
770 | Objects_Id mp_id |
---|
771 | ); |
---|
772 | #endif |
---|
773 | |
---|
774 | extern const Thread_queue_Operations _Thread_queue_Operations_default; |
---|
775 | |
---|
776 | extern const Thread_queue_Operations _Thread_queue_Operations_FIFO; |
---|
777 | |
---|
778 | extern const Thread_queue_Operations _Thread_queue_Operations_priority; |
---|
779 | |
---|
780 | /**@}*/ |
---|
781 | |
---|
782 | #ifdef __cplusplus |
---|
783 | } |
---|
784 | #endif |
---|
785 | |
---|
786 | #endif |
---|
787 | /* end of include file */ |
---|