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

Last change on this file since e273501 was e273501, checked in by Sebastian Huber <sebastian.huber@…>, on Jun 24, 2015 at 9:05:39 AM

score: Introduce Thread_queue_Queue

Separate the thread queue heads and lock from the operations. This
enables the support for light weight objects which only support one
queuing discipline.

  • Property mode set to 100644
File size: 5.4 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/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
36static 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
48void _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
103void _Thread_queue_Extract_locked(
104  Thread_queue_Queue            *queue,
105  const Thread_queue_Operations *operations,
106  Thread_Control                *the_thread
107)
108{
109  ( *operations->extract )( queue, the_thread );
110
111  _Thread_Wait_set_queue( the_thread, NULL );
112  _Thread_Wait_restore_default_operations( the_thread );
113  _Thread_Lock_restore_default( the_thread );
114}
115
116void _Thread_queue_Unblock_critical(
117  Thread_queue_Queue *queue,
118  Thread_Control     *the_thread,
119  ISR_lock_Context   *lock_context
120)
121{
122  bool success;
123  bool unblock;
124
125  success = _Thread_Wait_flags_try_change_critical(
126    the_thread,
127    THREAD_QUEUE_INTEND_TO_BLOCK,
128    THREAD_QUEUE_READY_AGAIN
129  );
130  if ( success ) {
131    unblock = false;
132  } else {
133    _Assert( _Thread_Wait_flags_get( the_thread ) == THREAD_QUEUE_BLOCKED );
134    _Thread_Wait_flags_set( the_thread, THREAD_QUEUE_READY_AGAIN );
135    unblock = true;
136  }
137
138  if ( unblock ) {
139    Per_CPU_Control *cpu_self;
140
141    cpu_self = _Thread_Dispatch_disable_critical( lock_context );
142    _Thread_queue_Queue_release( queue, lock_context );
143
144    _Thread_queue_Unblock( the_thread );
145
146    _Thread_Dispatch_enable( cpu_self );
147  } else {
148    _Thread_queue_Queue_release( queue, lock_context );
149  }
150}
151
152void _Thread_queue_Extract_critical(
153  Thread_queue_Queue            *queue,
154  const Thread_queue_Operations *operations,
155  Thread_Control                *the_thread,
156  ISR_lock_Context              *lock_context
157)
158{
159  _Thread_queue_Extract_locked( queue, operations, the_thread );
160  _Thread_queue_Unblock_critical( queue, the_thread, lock_context );
161}
162
163void _Thread_queue_Extract( Thread_Control *the_thread )
164{
165  ISR_lock_Context    lock_context;
166  ISR_lock_Control   *lock;
167  Thread_queue_Queue *queue;
168
169  lock = _Thread_Lock_acquire( the_thread, &lock_context );
170
171  queue = the_thread->Wait.queue;
172
173  if ( queue != NULL ) {
174    _SMP_Assert( lock == &queue->Lock );
175
176    _Thread_queue_Extract_critical(
177      queue,
178      the_thread->Wait.operations,
179      the_thread,
180      &lock_context
181    );
182  } else {
183    _Thread_Lock_release( lock, &lock_context );
184  }
185}
186
187Thread_Control *_Thread_queue_Dequeue( Thread_queue_Control *the_thread_queue )
188{
189  ISR_lock_Context  lock_context;
190  Thread_Control   *the_thread;
191
192  _Thread_queue_Acquire( the_thread_queue, &lock_context );
193
194  the_thread = _Thread_queue_First_locked( the_thread_queue );
195
196  if ( the_thread != NULL ) {
197    _SMP_Assert( the_thread->Lock.current == &the_thread_queue->Queue.Lock );
198
199    _Thread_queue_Extract_critical(
200      &the_thread_queue->Queue,
201      the_thread_queue->operations,
202      the_thread,
203      &lock_context
204    );
205  } else {
206    _Thread_queue_Release( the_thread_queue, &lock_context );
207  }
208
209  return the_thread;
210}
Note: See TracBrowser for help on using the repository browser.