1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Thread Queue Dequeue Priority |
---|
5 | * @ingroup ScoreThreadQ |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2008. |
---|
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/chainimpl.h> |
---|
23 | #include <rtems/score/isrlevel.h> |
---|
24 | #include <rtems/score/threadimpl.h> |
---|
25 | #include <rtems/score/watchdogimpl.h> |
---|
26 | |
---|
27 | Thread_Control *_Thread_queue_Dequeue_priority( |
---|
28 | Thread_queue_Control *the_thread_queue |
---|
29 | ) |
---|
30 | { |
---|
31 | uint32_t index; |
---|
32 | ISR_Level level; |
---|
33 | Thread_Control *the_thread = NULL; /* just to remove warnings */ |
---|
34 | Thread_Control *new_first_thread; |
---|
35 | Chain_Node *head; |
---|
36 | Chain_Node *tail; |
---|
37 | Chain_Node *new_first_node; |
---|
38 | Chain_Node *new_second_node; |
---|
39 | Chain_Node *last_node; |
---|
40 | Chain_Node *next_node; |
---|
41 | Chain_Node *previous_node; |
---|
42 | |
---|
43 | _ISR_Disable( level ); |
---|
44 | for( index=0 ; |
---|
45 | index < TASK_QUEUE_DATA_NUMBER_OF_PRIORITY_HEADERS ; |
---|
46 | index++ ) { |
---|
47 | if ( !_Chain_Is_empty( &the_thread_queue->Queues.Priority[ index ] ) ) { |
---|
48 | the_thread = (Thread_Control *) _Chain_First( |
---|
49 | &the_thread_queue->Queues.Priority[ index ] |
---|
50 | ); |
---|
51 | goto dequeue; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | /* |
---|
56 | * We did not find a thread to unblock. |
---|
57 | */ |
---|
58 | _ISR_Enable( level ); |
---|
59 | return NULL; |
---|
60 | |
---|
61 | dequeue: |
---|
62 | the_thread->Wait.queue = NULL; |
---|
63 | new_first_node = _Chain_First( &the_thread->Wait.Block2n ); |
---|
64 | new_first_thread = (Thread_Control *) new_first_node; |
---|
65 | next_node = the_thread->Object.Node.next; |
---|
66 | previous_node = the_thread->Object.Node.previous; |
---|
67 | |
---|
68 | if ( !_Chain_Is_empty( &the_thread->Wait.Block2n ) ) { |
---|
69 | last_node = _Chain_Last( &the_thread->Wait.Block2n ); |
---|
70 | new_second_node = new_first_node->next; |
---|
71 | |
---|
72 | previous_node->next = new_first_node; |
---|
73 | next_node->previous = new_first_node; |
---|
74 | new_first_node->next = next_node; |
---|
75 | new_first_node->previous = previous_node; |
---|
76 | |
---|
77 | if ( !_Chain_Has_only_one_node( &the_thread->Wait.Block2n ) ) { |
---|
78 | /* > two threads on 2-n */ |
---|
79 | head = _Chain_Head( &new_first_thread->Wait.Block2n ); |
---|
80 | tail = _Chain_Tail( &new_first_thread->Wait.Block2n ); |
---|
81 | |
---|
82 | new_second_node->previous = head; |
---|
83 | head->next = new_second_node; |
---|
84 | tail->previous = last_node; |
---|
85 | last_node->next = tail; |
---|
86 | } |
---|
87 | } else { |
---|
88 | previous_node->next = next_node; |
---|
89 | next_node->previous = previous_node; |
---|
90 | } |
---|
91 | |
---|
92 | if ( !_Watchdog_Is_active( &the_thread->Timer ) ) { |
---|
93 | _ISR_Enable( level ); |
---|
94 | } else { |
---|
95 | _Watchdog_Deactivate( &the_thread->Timer ); |
---|
96 | _ISR_Enable( level ); |
---|
97 | (void) _Watchdog_Remove( &the_thread->Timer ); |
---|
98 | } |
---|
99 | |
---|
100 | _Thread_Unblock( the_thread ); |
---|
101 | |
---|
102 | #if defined(RTEMS_MULTIPROCESSING) |
---|
103 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) |
---|
104 | _Thread_MP_Free_proxy( the_thread ); |
---|
105 | #endif |
---|
106 | return( the_thread ); |
---|
107 | } |
---|