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 | #if defined(RTEMS_HEAVY_STACK_DEBUG) || \ |
---|
26 | defined(RTEMS_HEAVY_MALLOC_DEBUG) |
---|
27 | #define __THREAD_DO_NOT_INLINE_DISABLE_DISPATCH__ |
---|
28 | #endif |
---|
29 | |
---|
30 | #if defined(RTEMS_SMP) || \ |
---|
31 | (CPU_INLINE_ENABLE_DISPATCH == FALSE) || \ |
---|
32 | (__RTEMS_DO_NOT_INLINE_THREAD_ENABLE_DISPATCH__ == 1) |
---|
33 | #define __THREAD_DO_NOT_INLINE_ENABLE_DISPATCH__ |
---|
34 | #endif |
---|
35 | |
---|
36 | /** |
---|
37 | * @addtogroup ScoreThread |
---|
38 | * |
---|
39 | * @{ |
---|
40 | */ |
---|
41 | |
---|
42 | /** |
---|
43 | * @brief Indicates if the executing thread is inside a thread dispatch |
---|
44 | * critical section. |
---|
45 | * |
---|
46 | * @retval true Thread dispatching is enabled. |
---|
47 | * @retval false The executing thread is inside a thread dispatch critical |
---|
48 | * section and dispatching is not allowed. |
---|
49 | */ |
---|
50 | RTEMS_INLINE_ROUTINE bool _Thread_Dispatch_is_enabled(void) |
---|
51 | { |
---|
52 | bool enabled; |
---|
53 | |
---|
54 | #if defined(RTEMS_SMP) |
---|
55 | ISR_Level level; |
---|
56 | |
---|
57 | _ISR_Local_disable( level ); |
---|
58 | #endif |
---|
59 | |
---|
60 | enabled = _Thread_Dispatch_disable_level == 0; |
---|
61 | |
---|
62 | #if defined(RTEMS_SMP) |
---|
63 | _ISR_Local_enable( level ); |
---|
64 | #endif |
---|
65 | |
---|
66 | return enabled; |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * @brief Gets thread dispatch disable level. |
---|
71 | * |
---|
72 | * @return The value of the thread dispatch level. |
---|
73 | */ |
---|
74 | RTEMS_INLINE_ROUTINE uint32_t _Thread_Dispatch_get_disable_level(void) |
---|
75 | { |
---|
76 | return _Thread_Dispatch_disable_level; |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * @brief Thread dispatch initialization. |
---|
81 | * |
---|
82 | * This routine initializes the thread dispatching subsystem. |
---|
83 | */ |
---|
84 | RTEMS_INLINE_ROUTINE void _Thread_Dispatch_initialization( void ) |
---|
85 | { |
---|
86 | _Thread_Dispatch_disable_level = 1; |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * @brief Performs a thread dispatch if necessary. |
---|
91 | * |
---|
92 | * This routine is responsible for transferring control of the processor from |
---|
93 | * the executing thread to the heir thread. Once the heir is running an |
---|
94 | * attempt is made to run the pending post-switch thread actions. |
---|
95 | * |
---|
96 | * As part of this process, it is responsible for the following actions |
---|
97 | * - update timing information of the executing thread, |
---|
98 | * - save the context of the executing thread, |
---|
99 | * - invokation of the thread switch user extensions, |
---|
100 | * - restore the context of the heir thread, and |
---|
101 | * - run of pending post-switch thread actions of the resulting executing |
---|
102 | * thread. |
---|
103 | * |
---|
104 | * On entry the thread dispatch level must be equal to zero. |
---|
105 | */ |
---|
106 | void _Thread_Dispatch( void ); |
---|
107 | |
---|
108 | /** |
---|
109 | * @brief Performs a thread dispatch on the current processor. |
---|
110 | * |
---|
111 | * On entry the thread dispatch disable level must be equal to one and |
---|
112 | * interrupts must be disabled. |
---|
113 | * |
---|
114 | * This function assumes that a thread dispatch is necessary. |
---|
115 | * |
---|
116 | * @param[in] cpu_self The current processor. |
---|
117 | * @param[in] level The previous interrupt level. |
---|
118 | * |
---|
119 | * @see _Thread_Dispatch(). |
---|
120 | */ |
---|
121 | void _Thread_Do_dispatch( Per_CPU_Control *cpu_self, ISR_Level level ); |
---|
122 | |
---|
123 | /** |
---|
124 | * @brief Disables thread dispatching inside a critical section (interrupts |
---|
125 | * disabled) with the current processor. |
---|
126 | * |
---|
127 | * @param[in] cpu_self The current processor. |
---|
128 | * @param[in] lock_context The lock context of the corresponding |
---|
129 | * _ISR_lock_ISR_disable() that started the critical section. |
---|
130 | * |
---|
131 | * @return The current processor. |
---|
132 | */ |
---|
133 | RTEMS_INLINE_ROUTINE Per_CPU_Control *_Thread_Dispatch_disable_with_CPU( |
---|
134 | Per_CPU_Control *cpu_self, |
---|
135 | const ISR_lock_Context *lock_context |
---|
136 | ) |
---|
137 | { |
---|
138 | uint32_t disable_level; |
---|
139 | |
---|
140 | disable_level = cpu_self->thread_dispatch_disable_level; |
---|
141 | _Profiling_Thread_dispatch_disable_critical( |
---|
142 | cpu_self, |
---|
143 | disable_level, |
---|
144 | lock_context |
---|
145 | ); |
---|
146 | cpu_self->thread_dispatch_disable_level = disable_level + 1; |
---|
147 | |
---|
148 | return cpu_self; |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * @brief Disables thread dispatching inside a critical section (interrupts |
---|
153 | * disabled). |
---|
154 | * |
---|
155 | * @param[in] lock_context The lock context of the corresponding |
---|
156 | * _ISR_lock_ISR_disable() that started the critical section. |
---|
157 | * |
---|
158 | * @return The current processor. |
---|
159 | */ |
---|
160 | RTEMS_INLINE_ROUTINE Per_CPU_Control *_Thread_Dispatch_disable_critical( |
---|
161 | const ISR_lock_Context *lock_context |
---|
162 | ) |
---|
163 | { |
---|
164 | return _Thread_Dispatch_disable_with_CPU( _Per_CPU_Get(), lock_context ); |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * @brief Disables thread dispatching. |
---|
169 | * |
---|
170 | * @return The current processor. |
---|
171 | */ |
---|
172 | RTEMS_INLINE_ROUTINE Per_CPU_Control *_Thread_Dispatch_disable( void ) |
---|
173 | { |
---|
174 | Per_CPU_Control *cpu_self; |
---|
175 | ISR_lock_Context lock_context; |
---|
176 | |
---|
177 | #if defined( RTEMS_SMP ) || defined( RTEMS_PROFILING ) |
---|
178 | _ISR_lock_ISR_disable( &lock_context ); |
---|
179 | #endif |
---|
180 | |
---|
181 | cpu_self = _Thread_Dispatch_disable_critical( &lock_context ); |
---|
182 | |
---|
183 | #if defined( RTEMS_SMP ) || defined( RTEMS_PROFILING ) |
---|
184 | _ISR_lock_ISR_enable( &lock_context ); |
---|
185 | #endif |
---|
186 | |
---|
187 | return cpu_self; |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * @brief Enables thread dispatching. |
---|
192 | * |
---|
193 | * May perfrom a thread dispatch if necessary as a side-effect. |
---|
194 | * |
---|
195 | * @param[in] cpu_self The current processor. |
---|
196 | */ |
---|
197 | RTEMS_INLINE_ROUTINE void _Thread_Dispatch_enable( Per_CPU_Control *cpu_self ) |
---|
198 | { |
---|
199 | uint32_t disable_level = cpu_self->thread_dispatch_disable_level; |
---|
200 | |
---|
201 | if ( disable_level == 1 ) { |
---|
202 | ISR_Level level; |
---|
203 | |
---|
204 | _ISR_Local_disable( level ); |
---|
205 | |
---|
206 | if ( cpu_self->dispatch_necessary ) { |
---|
207 | _Thread_Do_dispatch( cpu_self, level ); |
---|
208 | } else { |
---|
209 | cpu_self->thread_dispatch_disable_level = 0; |
---|
210 | _Profiling_Thread_dispatch_enable( cpu_self, 0 ); |
---|
211 | } |
---|
212 | |
---|
213 | _ISR_Local_enable( level ); |
---|
214 | } else { |
---|
215 | cpu_self->thread_dispatch_disable_level = disable_level - 1; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * @brief Unnests thread dispatching. |
---|
221 | * |
---|
222 | * @param[in] cpu_self The current processor. |
---|
223 | */ |
---|
224 | RTEMS_INLINE_ROUTINE void _Thread_Dispatch_unnest( Per_CPU_Control *cpu_self ) |
---|
225 | { |
---|
226 | --cpu_self->thread_dispatch_disable_level; |
---|
227 | } |
---|
228 | |
---|
229 | /** @} */ |
---|
230 | |
---|
231 | #ifdef __cplusplus |
---|
232 | } |
---|
233 | #endif /* __cplusplus */ |
---|
234 | |
---|
235 | #endif /* _RTEMS_SCORE_THREADDISPATCH_H */ |
---|