source: rtems/cpukit/score/src/threaddispatch.c @ 8d6e6eeb

4.115
Last change on this file since 8d6e6eeb was 8d6e6eeb, checked in by Sebastian Huber <sebastian.huber@…>, on 02/17/15 at 09:00:43

score: Fix FP context restore via _Thread_Handler

After a context switch we end up in the second part of
_Thread_Dispatch() or in _Thread_Handler() in case of new threads. Use
the same function _Thread_Restore_fp() to restore the floating-point
context. It makes no sense to do this in _Thread_Start_multitasking().
This fixes also a race condition in SMP configurations.

Update #2268.

  • Property mode set to 100644
File size: 4.5 KB
Line 
1/**
2 * @file
3 *
4 * @brief Dispatch Thread
5 * @ingroup ScoreThread
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2009.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  Copyright (c) 2014 embedded brains GmbH.
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.rtems.org/license/LICENSE.
17 */
18
19#if HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include <rtems/score/threaddispatch.h>
24#include <rtems/score/apiext.h>
25#include <rtems/score/assert.h>
26#include <rtems/score/isr.h>
27#include <rtems/score/threadimpl.h>
28#include <rtems/score/todimpl.h>
29#include <rtems/score/userextimpl.h>
30#include <rtems/score/wkspace.h>
31#include <rtems/config.h>
32
33static Thread_Action *_Thread_Get_post_switch_action(
34  Thread_Control *executing
35)
36{
37  Chain_Control *chain = &executing->Post_switch_actions.Chain;
38
39  return (Thread_Action *) _Chain_Get_unprotected( chain );
40}
41
42static void _Thread_Run_post_switch_actions( Thread_Control *executing )
43{
44  ISR_Level        level;
45  Per_CPU_Control *cpu_self;
46  Thread_Action   *action;
47
48  cpu_self = _Thread_Action_ISR_disable_and_acquire( executing, &level );
49  action = _Thread_Get_post_switch_action( executing );
50
51  while ( action != NULL ) {
52    _Chain_Set_off_chain( &action->Node );
53
54    ( *action->handler )( executing, action, cpu_self, level );
55
56    cpu_self = _Thread_Action_ISR_disable_and_acquire( executing, &level );
57    action = _Thread_Get_post_switch_action( executing );
58  }
59
60  _Thread_Action_release_and_ISR_enable( cpu_self, level );
61}
62
63void _Thread_Dispatch( void )
64{
65  Per_CPU_Control  *cpu_self;
66  Thread_Control   *executing;
67  ISR_Level         level;
68
69#if defined( RTEMS_SMP )
70  /*
71   * On SMP the complete context switch must be atomic with respect to one
72   * processor.  See also _Thread_Handler() since _Context_switch() may branch
73   * to this function.
74   */
75  _ISR_Disable_without_giant( level );
76#endif
77
78  cpu_self = _Per_CPU_Get();
79  _Assert( cpu_self->thread_dispatch_disable_level == 0 );
80  _Profiling_Thread_dispatch_disable( cpu_self, 0 );
81  cpu_self->thread_dispatch_disable_level = 1;
82
83  /*
84   *  Now determine if we need to perform a dispatch on the current CPU.
85   */
86  executing = cpu_self->executing;
87
88#if !defined( RTEMS_SMP )
89  _ISR_Disable( level );
90#endif
91
92#if defined( RTEMS_SMP )
93  if ( cpu_self->dispatch_necessary ) {
94#else
95  while ( cpu_self->dispatch_necessary ) {
96#endif
97    Thread_Control *heir = _Thread_Get_heir_and_make_it_executing( cpu_self );
98
99    /*
100     *  When the heir and executing are the same, then we are being
101     *  requested to do the post switch dispatching.  This is normally
102     *  done to dispatch signals.
103     */
104    if ( heir == executing )
105      goto post_switch;
106
107    /*
108     *  Since heir and executing are not the same, we need to do a real
109     *  context switch.
110     */
111#if __RTEMS_ADA__
112    executing->rtems_ada_self = rtems_ada_self;
113    rtems_ada_self = heir->rtems_ada_self;
114#endif
115    if ( heir->budget_algorithm == THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE )
116      heir->cpu_time_budget = rtems_configuration_get_ticks_per_timeslice();
117
118#if !defined( RTEMS_SMP )
119    _ISR_Enable( level );
120#endif
121
122    #ifndef __RTEMS_USE_TICKS_FOR_STATISTICS__
123      _Thread_Update_cpu_time_used(
124        executing,
125        &cpu_self->time_of_last_context_switch
126      );
127    #else
128      {
129        _TOD_Get_uptime( &cpu_self->time_of_last_context_switch );
130        heir->cpu_time_used++;
131      }
132    #endif
133
134#if !defined(__DYNAMIC_REENT__)
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#endif
143
144    _User_extensions_Thread_switch( executing, heir );
145    _Thread_Save_fp( executing );
146    _Context_Switch( &executing->Registers, &heir->Registers );
147    _Thread_Restore_fp( executing );
148
149    /*
150     * We have to obtain this value again after the context switch since the
151     * heir thread may have migrated from another processor.  Values from the
152     * stack or non-volatile registers reflect the old execution environment.
153     */
154    cpu_self = _Per_CPU_Get();
155
156    _Thread_Debug_set_real_processor( executing, cpu_self );
157
158#if !defined( RTEMS_SMP )
159    _ISR_Disable( level );
160#endif
161  }
162
163post_switch:
164  _Assert( cpu_self->thread_dispatch_disable_level == 1 );
165  cpu_self->thread_dispatch_disable_level = 0;
166  _Profiling_Thread_dispatch_enable( cpu_self, 0 );
167
168  _ISR_Enable_without_giant( level );
169
170  _Thread_Run_post_switch_actions( executing );
171}
Note: See TracBrowser for help on using the repository browser.