1 | /* |
---|
2 | * Thread Handler |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989-2009. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * |
---|
8 | * The license and distribution terms for this file may be |
---|
9 | * found in the file LICENSE in this distribution or at |
---|
10 | * http://www.rtems.com/license/LICENSE. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #if HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <rtems/system.h> |
---|
20 | #include <rtems/score/apiext.h> |
---|
21 | #include <rtems/score/context.h> |
---|
22 | #include <rtems/score/interr.h> |
---|
23 | #include <rtems/score/isr.h> |
---|
24 | #include <rtems/score/object.h> |
---|
25 | #include <rtems/score/priority.h> |
---|
26 | #include <rtems/score/states.h> |
---|
27 | #include <rtems/score/sysstate.h> |
---|
28 | #include <rtems/score/thread.h> |
---|
29 | #include <rtems/score/threadq.h> |
---|
30 | #include <rtems/score/userext.h> |
---|
31 | #include <rtems/score/wkspace.h> |
---|
32 | |
---|
33 | #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__ |
---|
34 | #include <rtems/score/timestamp.h> |
---|
35 | #endif |
---|
36 | |
---|
37 | /*PAGE |
---|
38 | * |
---|
39 | * _Thread_Enable_dispatch |
---|
40 | * |
---|
41 | * This kernel routine exits a context switch disable critical section. |
---|
42 | * This is the NOT INLINED version. |
---|
43 | * |
---|
44 | * Input parameters: NONE |
---|
45 | * |
---|
46 | * Output parameters: NONE |
---|
47 | * |
---|
48 | * INTERRUPT LATENCY: |
---|
49 | * dispatch thread |
---|
50 | * no dispatch thread |
---|
51 | */ |
---|
52 | |
---|
53 | #if ( (defined(CPU_INLINE_ENABLE_DISPATCH) && \ |
---|
54 | (CPU_INLINE_ENABLE_DISPATCH == FALSE)) || \ |
---|
55 | (__RTEMS_DO_NOT_INLINE_THREAD_ENABLE_DISPATCH__ == 1) ) |
---|
56 | void _Thread_Enable_dispatch( void ) |
---|
57 | { |
---|
58 | if ( --_Thread_Dispatch_disable_level ) |
---|
59 | return; |
---|
60 | _Thread_Dispatch(); |
---|
61 | } |
---|
62 | #endif |
---|
63 | |
---|
64 | /*PAGE |
---|
65 | * |
---|
66 | * _Thread_Dispatch |
---|
67 | * |
---|
68 | * This kernel routine determines if a dispatch is needed, and if so |
---|
69 | * dispatches to the heir thread. Once the heir is running an attempt |
---|
70 | * is made to dispatch any ASRs. |
---|
71 | * |
---|
72 | * ALTERNATE ENTRY POINTS: |
---|
73 | * void _Thread_Enable_dispatch(); |
---|
74 | * |
---|
75 | * Input parameters: NONE |
---|
76 | * |
---|
77 | * Output parameters: NONE |
---|
78 | * |
---|
79 | * INTERRUPT LATENCY: |
---|
80 | * dispatch thread |
---|
81 | * no dispatch thread |
---|
82 | */ |
---|
83 | |
---|
84 | void _Thread_Dispatch( void ) |
---|
85 | { |
---|
86 | Thread_Control *executing; |
---|
87 | Thread_Control *heir; |
---|
88 | ISR_Level level; |
---|
89 | |
---|
90 | executing = _Thread_Executing; |
---|
91 | _ISR_Disable( level ); |
---|
92 | while ( _Thread_Dispatch_necessary == true ) { |
---|
93 | heir = _Thread_Heir; |
---|
94 | _Thread_Dispatch_disable_level = 1; |
---|
95 | _Thread_Dispatch_necessary = false; |
---|
96 | _Thread_Executing = heir; |
---|
97 | |
---|
98 | /* |
---|
99 | * When the heir and executing are the same, then we are being |
---|
100 | * requested to do the post switch dispatching. This is normally |
---|
101 | * done to dispatch signals. |
---|
102 | */ |
---|
103 | if ( heir == executing ) |
---|
104 | goto post_switch; |
---|
105 | |
---|
106 | /* |
---|
107 | * Since heir and executing are not the same, we need to do a real |
---|
108 | * context switch. |
---|
109 | */ |
---|
110 | #if __RTEMS_ADA__ |
---|
111 | executing->rtems_ada_self = rtems_ada_self; |
---|
112 | rtems_ada_self = heir->rtems_ada_self; |
---|
113 | #endif |
---|
114 | if ( heir->budget_algorithm == THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE ) |
---|
115 | heir->cpu_time_budget = _Thread_Ticks_per_timeslice; |
---|
116 | |
---|
117 | _ISR_Enable( level ); |
---|
118 | |
---|
119 | #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__ |
---|
120 | { |
---|
121 | Timestamp_Control uptime, ran; |
---|
122 | _TOD_Get_uptime( &uptime ); |
---|
123 | _Timestamp_Subtract( |
---|
124 | &_Thread_Time_of_last_context_switch, |
---|
125 | &uptime, |
---|
126 | &ran |
---|
127 | ); |
---|
128 | _Timestamp_Add_to( &executing->cpu_time_used, &ran ); |
---|
129 | _Thread_Time_of_last_context_switch = uptime; |
---|
130 | } |
---|
131 | #else |
---|
132 | heir->cpu_time_used++; |
---|
133 | #endif |
---|
134 | |
---|
135 | /* |
---|
136 | * Switch libc's task specific data. |
---|
137 | */ |
---|
138 | if ( _Thread_libc_reent ) { |
---|
139 | executing->libc_reent = *_Thread_libc_reent; |
---|
140 | *_Thread_libc_reent = heir->libc_reent; |
---|
141 | } |
---|
142 | |
---|
143 | _User_extensions_Thread_switch( executing, heir ); |
---|
144 | |
---|
145 | /* |
---|
146 | * If the CPU has hardware floating point, then we must address saving |
---|
147 | * and restoring it as part of the context switch. |
---|
148 | * |
---|
149 | * The second conditional compilation section selects the algorithm used |
---|
150 | * to context switch between floating point tasks. The deferred algorithm |
---|
151 | * can be significantly better in a system with few floating point tasks |
---|
152 | * because it reduces the total number of save and restore FP context |
---|
153 | * operations. However, this algorithm can not be used on all CPUs due |
---|
154 | * to unpredictable use of FP registers by some compilers for integer |
---|
155 | * operations. |
---|
156 | */ |
---|
157 | |
---|
158 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
159 | #if ( CPU_USE_DEFERRED_FP_SWITCH != TRUE ) |
---|
160 | if ( executing->fp_context != NULL ) |
---|
161 | _Context_Save_fp( &executing->fp_context ); |
---|
162 | #endif |
---|
163 | #endif |
---|
164 | |
---|
165 | _Context_Switch( &executing->Registers, &heir->Registers ); |
---|
166 | |
---|
167 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
168 | #if ( CPU_USE_DEFERRED_FP_SWITCH == TRUE ) |
---|
169 | if ( (executing->fp_context != NULL) && |
---|
170 | !_Thread_Is_allocated_fp( executing ) ) { |
---|
171 | if ( _Thread_Allocated_fp != NULL ) |
---|
172 | _Context_Save_fp( &_Thread_Allocated_fp->fp_context ); |
---|
173 | _Context_Restore_fp( &executing->fp_context ); |
---|
174 | _Thread_Allocated_fp = executing; |
---|
175 | } |
---|
176 | #else |
---|
177 | if ( executing->fp_context != NULL ) |
---|
178 | _Context_Restore_fp( &executing->fp_context ); |
---|
179 | #endif |
---|
180 | #endif |
---|
181 | |
---|
182 | executing = _Thread_Executing; |
---|
183 | |
---|
184 | _ISR_Disable( level ); |
---|
185 | } |
---|
186 | |
---|
187 | post_switch: |
---|
188 | _Thread_Dispatch_disable_level = 0; |
---|
189 | |
---|
190 | _ISR_Enable( level ); |
---|
191 | |
---|
192 | _API_extensions_Run_postswitch(); |
---|
193 | } |
---|