1 | /** |
---|
2 | * @brief Constants and Structures Related with Thread Dispatch |
---|
3 | */ |
---|
4 | |
---|
5 | /* |
---|
6 | * COPYRIGHT (c) 1989-2009. |
---|
7 | * On-Line Applications Research Corporation (OAR). |
---|
8 | * |
---|
9 | * The license and distribution terms for this file may be |
---|
10 | * found in the file LICENSE in this distribution or at |
---|
11 | * http://www.rtems.org/license/LICENSE. |
---|
12 | */ |
---|
13 | |
---|
14 | #ifndef _RTEMS_SCORE_THREADDISPATCH_H |
---|
15 | #define _RTEMS_SCORE_THREADDISPATCH_H |
---|
16 | |
---|
17 | #include <rtems/score/percpu.h> |
---|
18 | #include <rtems/score/isrlock.h> |
---|
19 | #include <rtems/score/profiling.h> |
---|
20 | |
---|
21 | #ifdef __cplusplus |
---|
22 | extern "C" { |
---|
23 | #endif /* __cplusplus */ |
---|
24 | |
---|
25 | /** |
---|
26 | * @addtogroup ScoreThread |
---|
27 | * |
---|
28 | * @{ |
---|
29 | */ |
---|
30 | |
---|
31 | #if defined(RTEMS_SMP) || ( CPU_ENABLE_ROBUST_THREAD_DISPATCH == TRUE ) |
---|
32 | /** |
---|
33 | * @brief Enables a robust thread dispatch. |
---|
34 | * |
---|
35 | * On each change of the thread dispatch disable level from one to zero the |
---|
36 | * interrupt status is checked. In case interrupts are disabled and SMP is |
---|
37 | * enabled or the CPU port needs it, then the system terminates with the fatal |
---|
38 | * internal error INTERNAL_ERROR_BAD_THREAD_DISPATCH_ENVIRONMENT. |
---|
39 | */ |
---|
40 | #define RTEMS_SCORE_ROBUST_THREAD_DISPATCH |
---|
41 | #endif |
---|
42 | |
---|
43 | /** |
---|
44 | * @brief Indicates if the executing thread is inside a thread dispatch |
---|
45 | * critical section. |
---|
46 | * |
---|
47 | * @retval true Thread dispatching is enabled. |
---|
48 | * @retval false The executing thread is inside a thread dispatch critical |
---|
49 | * section and dispatching is not allowed. |
---|
50 | */ |
---|
51 | RTEMS_INLINE_ROUTINE bool _Thread_Dispatch_is_enabled(void) |
---|
52 | { |
---|
53 | bool enabled; |
---|
54 | |
---|
55 | #if defined(RTEMS_SMP) |
---|
56 | ISR_Level level; |
---|
57 | |
---|
58 | _ISR_Local_disable( level ); |
---|
59 | #endif |
---|
60 | |
---|
61 | enabled = _Thread_Dispatch_disable_level == 0; |
---|
62 | |
---|
63 | #if defined(RTEMS_SMP) |
---|
64 | _ISR_Local_enable( level ); |
---|
65 | #endif |
---|
66 | |
---|
67 | return enabled; |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * @brief Gets thread dispatch disable level. |
---|
72 | * |
---|
73 | * @return The value of the thread dispatch level. |
---|
74 | */ |
---|
75 | RTEMS_INLINE_ROUTINE uint32_t _Thread_Dispatch_get_disable_level(void) |
---|
76 | { |
---|
77 | return _Thread_Dispatch_disable_level; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * @brief Thread dispatch initialization. |
---|
82 | * |
---|
83 | * This routine initializes the thread dispatching subsystem. |
---|
84 | */ |
---|
85 | RTEMS_INLINE_ROUTINE void _Thread_Dispatch_initialization( void ) |
---|
86 | { |
---|
87 | _Thread_Dispatch_disable_level = 1; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * @brief Performs a thread dispatch if necessary. |
---|
92 | * |
---|
93 | * This routine is responsible for transferring control of the processor from |
---|
94 | * the executing thread to the heir thread. Once the heir is running an |
---|
95 | * attempt is made to run the pending post-switch thread actions. |
---|
96 | * |
---|
97 | * As part of this process, it is responsible for the following actions |
---|
98 | * - update timing information of the executing thread, |
---|
99 | * - save the context of the executing thread, |
---|
100 | * - invokation of the thread switch user extensions, |
---|
101 | * - restore the context of the heir thread, and |
---|
102 | * - run of pending post-switch thread actions of the resulting executing |
---|
103 | * thread. |
---|
104 | * |
---|
105 | * On entry the thread dispatch level must be equal to zero. |
---|
106 | */ |
---|
107 | void _Thread_Dispatch( void ); |
---|
108 | |
---|
109 | /** |
---|
110 | * @brief Directly do a thread dispatch. |
---|
111 | * |
---|
112 | * Must be called with a thread dispatch disable level of one, otherwise the |
---|
113 | * INTERNAL_ERROR_BAD_THREAD_DISPATCH_DISABLE_LEVEL will occur. This function |
---|
114 | * is useful for operations which synchronously block, e.g. self restart, self |
---|
115 | * deletion, yield, sleep. |
---|
116 | * |
---|
117 | * @param[in] cpu_self The current processor. |
---|
118 | * |
---|
119 | * @see _Thread_Dispatch(). |
---|
120 | */ |
---|
121 | void _Thread_Dispatch_direct( Per_CPU_Control *cpu_self ); |
---|
122 | |
---|
123 | /** |
---|
124 | * @brief Performs a thread dispatch on the current processor. |
---|
125 | * |
---|
126 | * On entry the thread dispatch disable level must be equal to one and |
---|
127 | * interrupts must be disabled. |
---|
128 | * |
---|
129 | * This function assumes that a thread dispatch is necessary. |
---|
130 | * |
---|
131 | * @param[in] cpu_self The current processor. |
---|
132 | * @param[in] level The previous interrupt level. |
---|
133 | * |
---|
134 | * @see _Thread_Dispatch(). |
---|
135 | */ |
---|
136 | void _Thread_Do_dispatch( Per_CPU_Control *cpu_self, ISR_Level level ); |
---|
137 | |
---|
138 | /** |
---|
139 | * @brief Disables thread dispatching inside a critical section (interrupts |
---|
140 | * disabled) with the current processor. |
---|
141 | * |
---|
142 | * @param[in] cpu_self The current processor. |
---|
143 | * @param[in] lock_context The lock context of the corresponding |
---|
144 | * _ISR_lock_ISR_disable() that started the critical section. |
---|
145 | * |
---|
146 | * @return The current processor. |
---|
147 | */ |
---|
148 | RTEMS_INLINE_ROUTINE Per_CPU_Control *_Thread_Dispatch_disable_with_CPU( |
---|
149 | Per_CPU_Control *cpu_self, |
---|
150 | const ISR_lock_Context *lock_context |
---|
151 | ) |
---|
152 | { |
---|
153 | uint32_t disable_level; |
---|
154 | |
---|
155 | disable_level = cpu_self->thread_dispatch_disable_level; |
---|
156 | _Profiling_Thread_dispatch_disable_critical( |
---|
157 | cpu_self, |
---|
158 | disable_level, |
---|
159 | lock_context |
---|
160 | ); |
---|
161 | cpu_self->thread_dispatch_disable_level = disable_level + 1; |
---|
162 | |
---|
163 | return cpu_self; |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * @brief Disables thread dispatching inside a critical section (interrupts |
---|
168 | * disabled). |
---|
169 | * |
---|
170 | * @param[in] lock_context The lock context of the corresponding |
---|
171 | * _ISR_lock_ISR_disable() that started the critical section. |
---|
172 | * |
---|
173 | * @return The current processor. |
---|
174 | */ |
---|
175 | RTEMS_INLINE_ROUTINE Per_CPU_Control *_Thread_Dispatch_disable_critical( |
---|
176 | const ISR_lock_Context *lock_context |
---|
177 | ) |
---|
178 | { |
---|
179 | return _Thread_Dispatch_disable_with_CPU( _Per_CPU_Get(), lock_context ); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * @brief Disables thread dispatching. |
---|
184 | * |
---|
185 | * @return The current processor. |
---|
186 | */ |
---|
187 | RTEMS_INLINE_ROUTINE Per_CPU_Control *_Thread_Dispatch_disable( void ) |
---|
188 | { |
---|
189 | Per_CPU_Control *cpu_self; |
---|
190 | ISR_lock_Context lock_context; |
---|
191 | |
---|
192 | #if defined( RTEMS_SMP ) || defined( RTEMS_PROFILING ) |
---|
193 | _ISR_lock_ISR_disable( &lock_context ); |
---|
194 | #endif |
---|
195 | |
---|
196 | cpu_self = _Thread_Dispatch_disable_critical( &lock_context ); |
---|
197 | |
---|
198 | #if defined( RTEMS_SMP ) || defined( RTEMS_PROFILING ) |
---|
199 | _ISR_lock_ISR_enable( &lock_context ); |
---|
200 | #endif |
---|
201 | |
---|
202 | return cpu_self; |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * @brief Enables thread dispatching. |
---|
207 | * |
---|
208 | * May perform a thread dispatch if necessary as a side-effect. |
---|
209 | * |
---|
210 | * @param[in] cpu_self The current processor. |
---|
211 | */ |
---|
212 | void _Thread_Dispatch_enable( Per_CPU_Control *cpu_self ); |
---|
213 | |
---|
214 | /** |
---|
215 | * @brief Unnests thread dispatching. |
---|
216 | * |
---|
217 | * @param[in] cpu_self The current processor. |
---|
218 | */ |
---|
219 | RTEMS_INLINE_ROUTINE void _Thread_Dispatch_unnest( Per_CPU_Control *cpu_self ) |
---|
220 | { |
---|
221 | _Assert( cpu_self->thread_dispatch_disable_level > 0 ); |
---|
222 | --cpu_self->thread_dispatch_disable_level; |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * @brief Requests a thread dispatch on the target processor. |
---|
227 | * |
---|
228 | * @param[in] cpu_self The current processor. |
---|
229 | * @param[in] cpu_target The target processor to request a thread dispatch. |
---|
230 | */ |
---|
231 | RTEMS_INLINE_ROUTINE void _Thread_Dispatch_request( |
---|
232 | Per_CPU_Control *cpu_self, |
---|
233 | Per_CPU_Control *cpu_target |
---|
234 | ) |
---|
235 | { |
---|
236 | #if defined( RTEMS_SMP ) |
---|
237 | if ( cpu_self == cpu_target ) { |
---|
238 | cpu_self->dispatch_necessary = true; |
---|
239 | } else { |
---|
240 | _Atomic_Fetch_or_ulong( &cpu_target->message, 0, ATOMIC_ORDER_RELEASE ); |
---|
241 | _CPU_SMP_Send_interrupt( _Per_CPU_Get_index( cpu_target ) ); |
---|
242 | } |
---|
243 | #else |
---|
244 | cpu_self->dispatch_necessary = true; |
---|
245 | (void) cpu_target; |
---|
246 | #endif |
---|
247 | } |
---|
248 | |
---|
249 | /** @} */ |
---|
250 | |
---|
251 | #ifdef __cplusplus |
---|
252 | } |
---|
253 | #endif /* __cplusplus */ |
---|
254 | |
---|
255 | #endif /* _RTEMS_SCORE_THREADDISPATCH_H */ |
---|