source: rtems/cpukit/score/src/threadqenqueue.c @ 3a58dc8

5
Last change on this file since 3a58dc8 was 3a58dc8, checked in by Sebastian Huber <sebastian.huber@…>, on 07/05/16 at 11:37:10

score: Priority inherit thread queue operations

Move the priority change due to priority interitance to the thread queue
enqueue operation to simplify the locking on SMP configurations.

Update #2412.
Update #2556.
Update #2765.

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