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

5
Last change on this file since f23d470 was f23d470, checked in by Gedare Bloom <gedare@…>, on 06/09/16 at 15:33:15

cpukit: Add and use Watchdog_Discipline.

Clock disciplines may be WATCHDOG_RELATIVE, WATCHDOG_ABSOLUTE,
or WATCHDOG_NO_TIMEOUT. A discipline of WATCHDOG_RELATIVE with
a timeout of WATCHDOG_NO_TIMEOUT is equivalent to a discipline
of WATCHDOG_NO_TIMEOUT.

updates #2732

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