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/watchdogimpl.h> |
---|
26 | |
---|
27 | #define THREAD_QUEUE_INTEND_TO_BLOCK \ |
---|
28 | (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_INTEND_TO_BLOCK) |
---|
29 | |
---|
30 | #define THREAD_QUEUE_BLOCKED \ |
---|
31 | (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_BLOCKED) |
---|
32 | |
---|
33 | #define THREAD_QUEUE_READY_AGAIN \ |
---|
34 | (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_READY_AGAIN) |
---|
35 | |
---|
36 | static void _Thread_queue_Unblock( Thread_Control *the_thread ) |
---|
37 | { |
---|
38 | _Watchdog_Remove_ticks( &the_thread->Timer ); |
---|
39 | _Thread_Unblock( the_thread ); |
---|
40 | |
---|
41 | #if defined(RTEMS_MULTIPROCESSING) |
---|
42 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) { |
---|
43 | _Thread_MP_Free_proxy( the_thread ); |
---|
44 | } |
---|
45 | #endif |
---|
46 | } |
---|
47 | |
---|
48 | void _Thread_queue_Enqueue_critical( |
---|
49 | Thread_queue_Queue *queue, |
---|
50 | const Thread_queue_Operations *operations, |
---|
51 | Thread_Control *the_thread, |
---|
52 | States_Control state, |
---|
53 | Watchdog_Interval timeout, |
---|
54 | uint32_t timeout_code, |
---|
55 | ISR_lock_Context *lock_context |
---|
56 | ) |
---|
57 | { |
---|
58 | Per_CPU_Control *cpu_self; |
---|
59 | bool success; |
---|
60 | |
---|
61 | _Thread_Lock_set( the_thread, &queue->Lock ); |
---|
62 | |
---|
63 | _Thread_Wait_set_queue( the_thread, queue ); |
---|
64 | _Thread_Wait_set_operations( the_thread, operations ); |
---|
65 | |
---|
66 | ( *operations->enqueue )( queue, the_thread ); |
---|
67 | |
---|
68 | _Thread_Wait_flags_set( the_thread, THREAD_QUEUE_INTEND_TO_BLOCK ); |
---|
69 | cpu_self = _Thread_Dispatch_disable_critical( lock_context ); |
---|
70 | _Thread_queue_Queue_release( queue, lock_context ); |
---|
71 | |
---|
72 | #if defined(RTEMS_MULTIPROCESSING) |
---|
73 | if ( _Thread_MP_Is_receive( the_thread ) && the_thread->receive_packet ) |
---|
74 | the_thread = _Thread_MP_Allocate_proxy( state ); |
---|
75 | else |
---|
76 | #endif |
---|
77 | /* |
---|
78 | * Set the blocking state for this thread queue in the thread. |
---|
79 | */ |
---|
80 | _Thread_Set_state( the_thread, state ); |
---|
81 | |
---|
82 | /* |
---|
83 | * If the thread wants to timeout, then schedule its timer. |
---|
84 | */ |
---|
85 | if ( timeout != WATCHDOG_NO_TIMEOUT ) { |
---|
86 | _Thread_Wait_set_timeout_code( the_thread, timeout_code ); |
---|
87 | _Watchdog_Initialize( &the_thread->Timer, _Thread_Timeout, 0, the_thread ); |
---|
88 | _Watchdog_Insert_ticks( &the_thread->Timer, timeout ); |
---|
89 | } |
---|
90 | |
---|
91 | success = _Thread_Wait_flags_try_change( |
---|
92 | the_thread, |
---|
93 | THREAD_QUEUE_INTEND_TO_BLOCK, |
---|
94 | THREAD_QUEUE_BLOCKED |
---|
95 | ); |
---|
96 | if ( !success ) { |
---|
97 | _Thread_queue_Unblock( the_thread ); |
---|
98 | } |
---|
99 | |
---|
100 | _Thread_Dispatch_enable( cpu_self ); |
---|
101 | } |
---|
102 | |
---|
103 | bool _Thread_queue_Extract_locked( |
---|
104 | Thread_queue_Queue *queue, |
---|
105 | const Thread_queue_Operations *operations, |
---|
106 | Thread_Control *the_thread |
---|
107 | ) |
---|
108 | { |
---|
109 | bool success; |
---|
110 | bool unblock; |
---|
111 | |
---|
112 | ( *operations->extract )( queue, the_thread ); |
---|
113 | |
---|
114 | _Thread_Wait_set_queue( the_thread, NULL ); |
---|
115 | _Thread_Wait_restore_default_operations( the_thread ); |
---|
116 | _Thread_Lock_restore_default( the_thread ); |
---|
117 | |
---|
118 | success = _Thread_Wait_flags_try_change_critical( |
---|
119 | the_thread, |
---|
120 | THREAD_QUEUE_INTEND_TO_BLOCK, |
---|
121 | THREAD_QUEUE_READY_AGAIN |
---|
122 | ); |
---|
123 | if ( success ) { |
---|
124 | unblock = false; |
---|
125 | } else { |
---|
126 | _Assert( _Thread_Wait_flags_get( the_thread ) == THREAD_QUEUE_BLOCKED ); |
---|
127 | _Thread_Wait_flags_set( the_thread, THREAD_QUEUE_READY_AGAIN ); |
---|
128 | unblock = true; |
---|
129 | } |
---|
130 | |
---|
131 | return unblock; |
---|
132 | } |
---|
133 | |
---|
134 | void _Thread_queue_Unblock_critical( |
---|
135 | bool unblock, |
---|
136 | Thread_queue_Queue *queue, |
---|
137 | Thread_Control *the_thread, |
---|
138 | ISR_lock_Context *lock_context |
---|
139 | ) |
---|
140 | { |
---|
141 | if ( unblock ) { |
---|
142 | Per_CPU_Control *cpu_self; |
---|
143 | |
---|
144 | cpu_self = _Thread_Dispatch_disable_critical( lock_context ); |
---|
145 | _Thread_queue_Queue_release( queue, lock_context ); |
---|
146 | |
---|
147 | _Thread_queue_Unblock( the_thread ); |
---|
148 | |
---|
149 | _Thread_Dispatch_enable( cpu_self ); |
---|
150 | } else { |
---|
151 | _Thread_queue_Queue_release( queue, lock_context ); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | void _Thread_queue_Extract_critical( |
---|
156 | Thread_queue_Queue *queue, |
---|
157 | const Thread_queue_Operations *operations, |
---|
158 | Thread_Control *the_thread, |
---|
159 | ISR_lock_Context *lock_context |
---|
160 | ) |
---|
161 | { |
---|
162 | bool unblock; |
---|
163 | |
---|
164 | unblock = _Thread_queue_Extract_locked( queue, operations, the_thread ); |
---|
165 | _Thread_queue_Unblock_critical( unblock, queue, the_thread, lock_context ); |
---|
166 | } |
---|
167 | |
---|
168 | void _Thread_queue_Extract( Thread_Control *the_thread ) |
---|
169 | { |
---|
170 | ISR_lock_Context lock_context; |
---|
171 | void *lock; |
---|
172 | Thread_queue_Queue *queue; |
---|
173 | |
---|
174 | lock = _Thread_Lock_acquire( the_thread, &lock_context ); |
---|
175 | |
---|
176 | queue = the_thread->Wait.queue; |
---|
177 | |
---|
178 | if ( queue != NULL ) { |
---|
179 | _SMP_Assert( lock == &queue->Lock ); |
---|
180 | |
---|
181 | _Thread_queue_Extract_critical( |
---|
182 | queue, |
---|
183 | the_thread->Wait.operations, |
---|
184 | the_thread, |
---|
185 | &lock_context |
---|
186 | ); |
---|
187 | } else { |
---|
188 | _Thread_Lock_release( lock, &lock_context ); |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | Thread_Control *_Thread_queue_Dequeue( Thread_queue_Control *the_thread_queue ) |
---|
193 | { |
---|
194 | ISR_lock_Context lock_context; |
---|
195 | Thread_Control *the_thread; |
---|
196 | |
---|
197 | _Thread_queue_Acquire( the_thread_queue, &lock_context ); |
---|
198 | |
---|
199 | the_thread = _Thread_queue_First_locked( the_thread_queue ); |
---|
200 | |
---|
201 | if ( the_thread != NULL ) { |
---|
202 | _SMP_Assert( the_thread->Lock.current == &the_thread_queue->Queue.Lock ); |
---|
203 | |
---|
204 | _Thread_queue_Extract_critical( |
---|
205 | &the_thread_queue->Queue, |
---|
206 | the_thread_queue->operations, |
---|
207 | the_thread, |
---|
208 | &lock_context |
---|
209 | ); |
---|
210 | } else { |
---|
211 | _Thread_queue_Release( the_thread_queue, &lock_context ); |
---|
212 | } |
---|
213 | |
---|
214 | return the_thread; |
---|
215 | } |
---|