source: rtems/cpukit/score/src/threadqenqueue.c @ 7bf9c8b

5
Last change on this file since 7bf9c8b was 7bf9c8b, checked in by Sebastian Huber <sebastian.huber@…>, on 06/09/16 at 13:14:19

score: Comment _Thread_queue_Enqueue_critical()

  • Property mode set to 100644
File size: 7.7 KB
Line 
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
37void _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  Thread_queue_Context          *queue_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( &queue_context->Lock_context );
65  _Thread_queue_Queue_release( queue, &queue_context->Lock_context );
66
67  if (
68    cpu_self->thread_dispatch_disable_level
69      != queue_context->expected_thread_dispatch_disable_level
70  ) {
71    _Terminate(
72      INTERNAL_ERROR_CORE,
73      false,
74      INTERNAL_ERROR_THREAD_QUEUE_ENQUEUE_FROM_BAD_STATE
75    );
76  }
77
78  /*
79   *  Set the blocking state for this thread queue in the thread.
80   */
81  _Thread_Set_state( the_thread, state );
82
83  /*
84   *  If the thread wants to timeout, then schedule its timer.
85   */
86  if ( timeout != WATCHDOG_NO_TIMEOUT ) {
87    _Thread_Timer_insert_relative(
88      the_thread,
89      cpu_self,
90      _Thread_Timeout,
91      timeout
92    );
93  }
94
95  /*
96   * At this point thread dispatching is disabled, however, we already released
97   * the thread queue lock.  Thus, interrupts or threads on other processors
98   * may already changed our state with respect to the thread queue object.
99   * The request could be satisfied or timed out.  This situation is indicated
100   * by the thread wait flags.  Other parties must not modify our thread state
101   * as long as we are in the THREAD_QUEUE_INTEND_TO_BLOCK thread wait state,
102   * thus we have to cancel the blocking operation ourself if necessary.
103   */
104  success = _Thread_Wait_flags_try_change(
105    the_thread,
106    THREAD_QUEUE_INTEND_TO_BLOCK,
107    THREAD_QUEUE_BLOCKED
108  );
109  if ( !success ) {
110    _Thread_Remove_timer_and_unblock( the_thread, queue );
111  }
112
113  _Thread_Dispatch_enable( cpu_self );
114}
115
116bool _Thread_queue_Do_extract_locked(
117  Thread_queue_Queue            *queue,
118  const Thread_queue_Operations *operations,
119  Thread_Control                *the_thread
120#if defined(RTEMS_MULTIPROCESSING)
121  ,
122  const Thread_queue_Context    *queue_context
123#endif
124)
125{
126  bool success;
127  bool unblock;
128
129#if defined(RTEMS_MULTIPROCESSING)
130  if ( !_Objects_Is_local_id( the_thread->Object.id ) ) {
131    Thread_Proxy_control    *the_proxy;
132    Thread_queue_MP_callout  mp_callout;
133
134    the_proxy = (Thread_Proxy_control *) the_thread;
135    mp_callout = queue_context->mp_callout;
136    _Assert( mp_callout != NULL );
137    the_proxy->thread_queue_callout = queue_context->mp_callout;
138  }
139#endif
140
141  ( *operations->extract )( queue, the_thread );
142
143  /*
144   * We must update the wait flags under protection of the current thread lock,
145   * otherwise a _Thread_Timeout() running on another processor may interfere.
146   */
147  success = _Thread_Wait_flags_try_change_critical(
148    the_thread,
149    THREAD_QUEUE_INTEND_TO_BLOCK,
150    THREAD_QUEUE_READY_AGAIN
151  );
152  if ( success ) {
153    unblock = false;
154  } else {
155    _Assert( _Thread_Wait_flags_get( the_thread ) == THREAD_QUEUE_BLOCKED );
156    _Thread_Wait_flags_set( the_thread, THREAD_QUEUE_READY_AGAIN );
157    unblock = true;
158  }
159
160  _Thread_Wait_set_queue( the_thread, NULL );
161  _Thread_Wait_restore_default_operations( the_thread );
162  _Thread_Lock_restore_default( the_thread );
163
164  return unblock;
165}
166
167void _Thread_queue_Unblock_critical(
168  bool                unblock,
169  Thread_queue_Queue *queue,
170  Thread_Control     *the_thread,
171  ISR_lock_Context   *lock_context
172)
173{
174  if ( unblock ) {
175    Per_CPU_Control *cpu_self;
176
177    cpu_self = _Thread_Dispatch_disable_critical( lock_context );
178    _Thread_queue_Queue_release( queue, lock_context );
179
180    _Thread_Remove_timer_and_unblock( the_thread, queue );
181
182    _Thread_Dispatch_enable( cpu_self );
183  } else {
184    _Thread_queue_Queue_release( queue, lock_context );
185  }
186}
187
188void _Thread_queue_Extract_critical(
189  Thread_queue_Queue            *queue,
190  const Thread_queue_Operations *operations,
191  Thread_Control                *the_thread,
192  Thread_queue_Context          *queue_context
193)
194{
195  bool unblock;
196
197  unblock = _Thread_queue_Extract_locked(
198    queue,
199    operations,
200    the_thread,
201    queue_context
202  );
203
204  _Thread_queue_Unblock_critical(
205    unblock,
206    queue,
207    the_thread,
208    &queue_context->Lock_context
209  );
210}
211
212void _Thread_queue_Extract( Thread_Control *the_thread )
213{
214  Thread_queue_Context  queue_context;
215  void                 *lock;
216  Thread_queue_Queue   *queue;
217
218  _Thread_queue_Context_initialize( &queue_context );
219  lock = _Thread_Lock_acquire( the_thread, &queue_context.Lock_context );
220
221  queue = the_thread->Wait.queue;
222
223  if ( queue != NULL ) {
224    _SMP_Assert( lock == &queue->Lock );
225
226    _Thread_queue_Context_set_MP_callout(
227      &queue_context,
228      _Thread_queue_MP_callout_do_nothing
229    );
230    _Thread_queue_Extract_critical(
231      queue,
232      the_thread->Wait.operations,
233      the_thread,
234      &queue_context
235    );
236  } else {
237    _Thread_Lock_release( lock, &queue_context.Lock_context );
238  }
239}
240
241Thread_Control *_Thread_queue_Do_dequeue(
242  Thread_queue_Control          *the_thread_queue,
243  const Thread_queue_Operations *operations
244#if defined(RTEMS_MULTIPROCESSING)
245  ,
246  Thread_queue_MP_callout        mp_callout
247#endif
248)
249{
250  Thread_queue_Context  queue_context;
251  Thread_Control       *the_thread;
252
253  _Thread_queue_Context_initialize( &queue_context );
254  _Thread_queue_Context_set_MP_callout( &queue_context, mp_callout );
255  _Thread_queue_Acquire( the_thread_queue, &queue_context.Lock_context );
256
257  the_thread = _Thread_queue_First_locked( the_thread_queue, operations );
258
259  if ( the_thread != NULL ) {
260    _SMP_Assert( the_thread->Lock.current == &the_thread_queue->Queue.Lock );
261
262    _Thread_queue_Extract_critical(
263      &the_thread_queue->Queue,
264      operations,
265      the_thread,
266      &queue_context
267    );
268  } else {
269    _Thread_queue_Release( the_thread_queue, &queue_context.Lock_context );
270  }
271
272  return the_thread;
273}
274
275#if defined(RTEMS_MULTIPROCESSING)
276void _Thread_queue_Unblock_proxy(
277  Thread_queue_Queue *queue,
278  Thread_Control     *the_thread
279)
280{
281  const Thread_queue_Object *the_queue_object;
282  Thread_Proxy_control      *the_proxy;
283  Thread_queue_MP_callout    mp_callout;
284
285  the_queue_object = THREAD_QUEUE_QUEUE_TO_OBJECT( queue );
286  the_proxy = (Thread_Proxy_control *) the_thread;
287  mp_callout = the_proxy->thread_queue_callout;
288  ( *mp_callout )( the_thread, the_queue_object->Object.id );
289
290  _Thread_MP_Free_proxy( the_thread );
291}
292#endif
Note: See TracBrowser for help on using the repository browser.