1 | /* |
---|
2 | * Thread Queue Handler |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-2008. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <rtems/system.h> |
---|
20 | #include <rtems/score/chain.h> |
---|
21 | #include <rtems/score/isr.h> |
---|
22 | #include <rtems/score/object.h> |
---|
23 | #include <rtems/score/states.h> |
---|
24 | #include <rtems/score/thread.h> |
---|
25 | #include <rtems/score/threadq.h> |
---|
26 | #include <rtems/score/tqdata.h> |
---|
27 | |
---|
28 | /*PAGE |
---|
29 | * |
---|
30 | * _Thread_queue_Dequeue_priority |
---|
31 | * |
---|
32 | * This routine removes a thread from the specified PRIORITY based |
---|
33 | * threadq, unblocks it, and cancels its timeout timer. |
---|
34 | * |
---|
35 | * Input parameters: |
---|
36 | * the_thread_queue - pointer to thread queue |
---|
37 | * |
---|
38 | * Output parameters: |
---|
39 | * returns - thread dequeued or NULL |
---|
40 | * |
---|
41 | * INTERRUPT LATENCY: |
---|
42 | * only case |
---|
43 | */ |
---|
44 | |
---|
45 | Thread_Control *_Thread_queue_Dequeue_priority( |
---|
46 | Thread_queue_Control *the_thread_queue |
---|
47 | ) |
---|
48 | { |
---|
49 | uint32_t index; |
---|
50 | ISR_Level level; |
---|
51 | Thread_Control *the_thread = NULL; /* just to remove warnings */ |
---|
52 | Thread_Control *new_first_thread; |
---|
53 | Chain_Node *new_first_node; |
---|
54 | Chain_Node *new_second_node; |
---|
55 | Chain_Node *last_node; |
---|
56 | Chain_Node *next_node; |
---|
57 | Chain_Node *previous_node; |
---|
58 | Thread_blocking_operation_States sync; |
---|
59 | |
---|
60 | _ISR_Disable( level ); |
---|
61 | for( index=0 ; |
---|
62 | index < TASK_QUEUE_DATA_NUMBER_OF_PRIORITY_HEADERS ; |
---|
63 | index++ ) { |
---|
64 | if ( !_Chain_Is_empty( &the_thread_queue->Queues.Priority[ index ] ) ) { |
---|
65 | the_thread = (Thread_Control *) |
---|
66 | the_thread_queue->Queues.Priority[ index ].first; |
---|
67 | goto dequeue; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | /* |
---|
72 | * If we interrupted a blocking operation, cancel it. |
---|
73 | */ |
---|
74 | sync = the_thread_queue->sync_state; |
---|
75 | if ( (sync == THREAD_BLOCKING_OPERATION_SYNCHRONIZED) || |
---|
76 | (sync == THREAD_BLOCKING_OPERATION_SATISFIED) ) { |
---|
77 | _ISR_Enable( level ); |
---|
78 | return NULL; |
---|
79 | } |
---|
80 | |
---|
81 | if ( (sync == THREAD_BLOCKING_OPERATION_NOTHING_HAPPENED) || |
---|
82 | (sync == THREAD_BLOCKING_OPERATION_TIMEOUT ) ) { |
---|
83 | the_thread_queue->sync_state = THREAD_BLOCKING_OPERATION_SATISFIED; |
---|
84 | _ISR_Enable( level ); |
---|
85 | return _Thread_Executing; |
---|
86 | } |
---|
87 | |
---|
88 | dequeue: |
---|
89 | the_thread->Wait.queue = NULL; |
---|
90 | new_first_node = the_thread->Wait.Block2n.first; |
---|
91 | new_first_thread = (Thread_Control *) new_first_node; |
---|
92 | next_node = the_thread->Object.Node.next; |
---|
93 | previous_node = the_thread->Object.Node.previous; |
---|
94 | |
---|
95 | if ( !_Chain_Is_empty( &the_thread->Wait.Block2n ) ) { |
---|
96 | last_node = the_thread->Wait.Block2n.last; |
---|
97 | new_second_node = new_first_node->next; |
---|
98 | |
---|
99 | previous_node->next = new_first_node; |
---|
100 | next_node->previous = new_first_node; |
---|
101 | new_first_node->next = next_node; |
---|
102 | new_first_node->previous = previous_node; |
---|
103 | |
---|
104 | if ( !_Chain_Has_only_one_node( &the_thread->Wait.Block2n ) ) { |
---|
105 | /* > two threads on 2-n */ |
---|
106 | new_second_node->previous = |
---|
107 | _Chain_Head( &new_first_thread->Wait.Block2n ); |
---|
108 | |
---|
109 | new_first_thread->Wait.Block2n.first = new_second_node; |
---|
110 | new_first_thread->Wait.Block2n.last = last_node; |
---|
111 | |
---|
112 | last_node->next = _Chain_Tail( &new_first_thread->Wait.Block2n ); |
---|
113 | } |
---|
114 | } else { |
---|
115 | previous_node->next = next_node; |
---|
116 | next_node->previous = previous_node; |
---|
117 | } |
---|
118 | |
---|
119 | if ( !_Watchdog_Is_active( &the_thread->Timer ) ) { |
---|
120 | _ISR_Enable( level ); |
---|
121 | _Thread_Unblock( the_thread ); |
---|
122 | } else { |
---|
123 | _Watchdog_Deactivate( &the_thread->Timer ); |
---|
124 | _ISR_Enable( level ); |
---|
125 | (void) _Watchdog_Remove( &the_thread->Timer ); |
---|
126 | _Thread_Unblock( the_thread ); |
---|
127 | } |
---|
128 | |
---|
129 | #if defined(RTEMS_MULTIPROCESSING) |
---|
130 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) |
---|
131 | _Thread_MP_Free_proxy( the_thread ); |
---|
132 | #endif |
---|
133 | return( the_thread ); |
---|
134 | } |
---|