source: rtems/cpukit/score/src/threadqenqueue.c @ e709aa85

5
Last change on this file since e709aa85 was e709aa85, checked in by Sebastian Huber <sebastian.huber@…>, on 07/13/15 at 11:49:35

score: Move wait flag update to tq extract

This makes it possible to use _Thread_queue_Extract_locked() for barrier
operations which extract all threads on the queue in one critical
section.

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[f7f1d77]1/**
2 * @file
3 *
[688fbc44]4 * @brief Thread Queue Operations
[f7f1d77]5 * @ingroup ScoreThreadQ
6 */
7
[dfbfa2b0]8/*
[ed7a028]9 *  COPYRIGHT (c) 1989-2014.
[dfbfa2b0]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
[c499856]14 *  http://www.rtems.org/license/LICENSE.
[dfbfa2b0]15 */
16
[a8eed23]17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
[a112364]21#include <rtems/score/threadqimpl.h>
[a6524b9]22#include <rtems/score/assert.h>
[cc366ec]23#include <rtems/score/threaddispatch.h>
[5618c37a]24#include <rtems/score/threadimpl.h>
[4b48ece0]25#include <rtems/score/watchdogimpl.h>
[dfbfa2b0]26
[cc366ec]27#define THREAD_QUEUE_INTEND_TO_BLOCK \
28  (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_INTEND_TO_BLOCK)
[2429766]29
[cc366ec]30#define THREAD_QUEUE_BLOCKED \
31  (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_BLOCKED)
[02c4c441]32
[cc366ec]33#define THREAD_QUEUE_READY_AGAIN \
34  (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_READY_AGAIN)
[2429766]35
[cc366ec]36static void _Thread_queue_Unblock( Thread_Control *the_thread )
37{
38  _Watchdog_Remove_ticks( &the_thread->Timer );
[2429766]39  _Thread_Unblock( the_thread );
40
41#if defined(RTEMS_MULTIPROCESSING)
[cc366ec]42  if ( !_Objects_Is_local_id( the_thread->Object.id ) ) {
[2429766]43    _Thread_MP_Free_proxy( the_thread );
[cc366ec]44  }
[2429766]45#endif
46}
47
[02c4c441]48void _Thread_queue_Enqueue_critical(
[e2735012]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
[dfbfa2b0]56)
57{
[e2735012]58  Per_CPU_Control *cpu_self;
59  bool             success;
[dfbfa2b0]60
[e2735012]61  _Thread_Lock_set( the_thread, &queue->Lock );
[02c4c441]62
[e2735012]63  _Thread_Wait_set_queue( the_thread, queue );
[cc366ec]64  _Thread_Wait_set_operations( the_thread, operations );
65
[e2735012]66  ( *operations->enqueue )( queue, the_thread );
[02c4c441]67
[cc366ec]68  _Thread_Wait_flags_set( the_thread, THREAD_QUEUE_INTEND_TO_BLOCK );
[d5423295]69  cpu_self = _Thread_Dispatch_disable_critical( lock_context );
[e2735012]70  _Thread_queue_Queue_release( queue, lock_context );
[02c4c441]71
[dfbfa2b0]72#if defined(RTEMS_MULTIPROCESSING)
73  if ( _Thread_MP_Is_receive( the_thread ) && the_thread->receive_packet )
[9f10911]74    the_thread = _Thread_MP_Allocate_proxy( state );
[dfbfa2b0]75  else
76#endif
[96d0b64]77  /*
78   *  Set the blocking state for this thread queue in the thread.
79   */
[9f10911]80  _Thread_Set_state( the_thread, state );
[dfbfa2b0]81
[96d0b64]82  /*
83   *  If the thread wants to timeout, then schedule its timer.
[28352fae]84   */
[cc366ec]85  if ( timeout != WATCHDOG_NO_TIMEOUT ) {
[f5d6c8b]86    _Thread_Wait_set_timeout_code( the_thread, timeout_code );
[cc366ec]87    _Watchdog_Initialize( &the_thread->Timer, _Thread_Timeout, 0, the_thread );
[dfbfa2b0]88    _Watchdog_Insert_ticks( &the_thread->Timer, timeout );
89  }
90
[cc366ec]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  }
[3168deaa]99
[cc366ec]100  _Thread_Dispatch_enable( cpu_self );
101}
[3250664]102
[e709aa85]103bool _Thread_queue_Extract_locked(
[e2735012]104  Thread_queue_Queue            *queue,
105  const Thread_queue_Operations *operations,
106  Thread_Control                *the_thread
[cc366ec]107)
108{
[e709aa85]109  bool success;
110  bool unblock;
111
[e2735012]112  ( *operations->extract )( queue, the_thread );
[3250664]113
[cc366ec]114  _Thread_Wait_set_queue( the_thread, NULL );
115  _Thread_Wait_restore_default_operations( the_thread );
116  _Thread_Lock_restore_default( the_thread );
[383cf42]117
[cc366ec]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;
[a6524b9]125  } else {
[cc366ec]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  }
[a6524b9]130
[e709aa85]131  return unblock;
132}
133
134void _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{
[cc366ec]141  if ( unblock ) {
142    Per_CPU_Control *cpu_self;
[a6524b9]143
[d5423295]144    cpu_self = _Thread_Dispatch_disable_critical( lock_context );
[e2735012]145    _Thread_queue_Queue_release( queue, lock_context );
[cc366ec]146
147    _Thread_queue_Unblock( the_thread );
148
149    _Thread_Dispatch_enable( cpu_self );
150  } else {
[e2735012]151    _Thread_queue_Queue_release( queue, lock_context );
[3250664]152  }
[dfbfa2b0]153}
[688fbc44]154
[cc366ec]155void _Thread_queue_Extract_critical(
[e2735012]156  Thread_queue_Queue            *queue,
157  const Thread_queue_Operations *operations,
158  Thread_Control                *the_thread,
159  ISR_lock_Context              *lock_context
[688fbc44]160)
161{
[e709aa85]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 );
[cc366ec]166}
167
168void _Thread_queue_Extract( Thread_Control *the_thread )
169{
[e2735012]170  ISR_lock_Context    lock_context;
171  ISR_lock_Control   *lock;
172  Thread_queue_Queue *queue;
[688fbc44]173
[02c4c441]174  lock = _Thread_Lock_acquire( the_thread, &lock_context );
[688fbc44]175
[e2735012]176  queue = the_thread->Wait.queue;
[688fbc44]177
[e2735012]178  if ( queue != NULL ) {
179    _SMP_Assert( lock == &queue->Lock );
[688fbc44]180
[e2735012]181    _Thread_queue_Extract_critical(
182      queue,
183      the_thread->Wait.operations,
184      the_thread,
185      &lock_context
186    );
[cc366ec]187  } else {
188    _Thread_Lock_release( lock, &lock_context );
189  }
[688fbc44]190}
191
[cc366ec]192Thread_Control *_Thread_queue_Dequeue( Thread_queue_Control *the_thread_queue )
[688fbc44]193{
[cc366ec]194  ISR_lock_Context  lock_context;
195  Thread_Control   *the_thread;
[688fbc44]196
[02c4c441]197  _Thread_queue_Acquire( the_thread_queue, &lock_context );
[688fbc44]198
[cc366ec]199  the_thread = _Thread_queue_First_locked( the_thread_queue );
[688fbc44]200
[cc366ec]201  if ( the_thread != NULL ) {
[e2735012]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    );
[cc366ec]210  } else {
211    _Thread_queue_Release( the_thread_queue, &lock_context );
212  }
[688fbc44]213
214  return the_thread;
215}
Note: See TracBrowser for help on using the repository browser.