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

4.115
Last change on this file since d5423295 was d5423295, checked in by Sebastian Huber <sebastian.huber@…>, on 05/18/15 at 09:09:14

score: _Thread_Dispatch_disable_critical()

Thread dispatching is disabled in case interrupts are disabled. To get
an accurate thread dispatch disabled time it is important to use the
interrupt disabled instant in case a transition from an interrupt
disabled section to a thread dispatch level section happens.

  • 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_Control *the_thread_queue,
50  Thread_Control       *the_thread,
51  States_Control        state,
52  Watchdog_Interval     timeout,
53  uint32_t              timeout_code,
54  ISR_lock_Context     *lock_context
55)
56{
57  const Thread_queue_Operations *operations;
58  Per_CPU_Control               *cpu_self;
59  bool                           success;
60
61  _Thread_Lock_set( the_thread, &the_thread_queue->Lock );
62
63  operations = the_thread_queue->operations;
64  _Thread_Wait_set_queue( the_thread, the_thread_queue );
65  _Thread_Wait_set_operations( the_thread, operations );
66
67  ( *operations->enqueue )( the_thread_queue, the_thread );
68
69  _Thread_Wait_flags_set( the_thread, THREAD_QUEUE_INTEND_TO_BLOCK );
70  cpu_self = _Thread_Dispatch_disable_critical( lock_context );
71  _Thread_queue_Release( the_thread_queue, lock_context );
72
73#if defined(RTEMS_MULTIPROCESSING)
74  if ( _Thread_MP_Is_receive( the_thread ) && the_thread->receive_packet )
75    the_thread = _Thread_MP_Allocate_proxy( state );
76  else
77#endif
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_Wait_set_timeout_code( the_thread, timeout_code );
88    _Watchdog_Initialize( &the_thread->Timer, _Thread_Timeout, 0, the_thread );
89    _Watchdog_Insert_ticks( &the_thread->Timer, timeout );
90  }
91
92  success = _Thread_Wait_flags_try_change(
93    the_thread,
94    THREAD_QUEUE_INTEND_TO_BLOCK,
95    THREAD_QUEUE_BLOCKED
96  );
97  if ( !success ) {
98    _Thread_queue_Unblock( the_thread );
99  }
100
101  _Thread_Dispatch_enable( cpu_self );
102}
103
104void _Thread_queue_Extract_locked(
105  Thread_queue_Control *the_thread_queue,
106  Thread_Control       *the_thread
107)
108{
109  ( *the_thread_queue->operations->extract )( the_thread_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_Control *the_thread_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_Release( the_thread_queue, lock_context );
143
144    _Thread_queue_Unblock( the_thread );
145
146    _Thread_Dispatch_enable( cpu_self );
147  } else {
148    _Thread_queue_Release( the_thread_queue, lock_context );
149  }
150}
151
152void _Thread_queue_Extract_critical(
153  Thread_queue_Control *the_thread_queue,
154  Thread_Control       *the_thread,
155  ISR_lock_Context     *lock_context
156)
157{
158  _Thread_queue_Extract_locked( the_thread_queue, the_thread );
159  _Thread_queue_Unblock_critical( the_thread_queue, the_thread, lock_context );
160}
161
162void _Thread_queue_Extract( Thread_Control *the_thread )
163{
164  ISR_lock_Context      lock_context;
165  ISR_lock_Control     *lock;
166  Thread_queue_Control *the_thread_queue;
167
168  lock = _Thread_Lock_acquire( the_thread, &lock_context );
169
170  the_thread_queue = the_thread->Wait.queue;
171
172  if ( the_thread_queue != NULL ) {
173    _SMP_Assert( lock == &the_thread_queue->Lock );
174
175    _Thread_queue_Extract_critical( the_thread_queue, the_thread, &lock_context );
176  } else {
177    _Thread_Lock_release( lock, &lock_context );
178  }
179}
180
181Thread_Control *_Thread_queue_Dequeue( Thread_queue_Control *the_thread_queue )
182{
183  ISR_lock_Context  lock_context;
184  Thread_Control   *the_thread;
185
186  _Thread_queue_Acquire( the_thread_queue, &lock_context );
187
188  the_thread = _Thread_queue_First_locked( the_thread_queue );
189
190  if ( the_thread != NULL ) {
191    _SMP_Assert( the_thread->Lock.current == &the_thread_queue->Lock );
192
193    _Thread_queue_Extract_critical( the_thread_queue, the_thread, &lock_context );
194  } else {
195    _Thread_queue_Release( the_thread_queue, &lock_context );
196  }
197
198  return the_thread;
199}
Note: See TracBrowser for help on using the repository browser.