source: rtems/cpukit/score/src/threaddispatch.c @ a853c851

5
Last change on this file since a853c851 was a853c851, checked in by Sebastian Huber <sebastian.huber@…>, on 12/10/15 at 14:44:02

Optional Initial Extensions initialization

Update #2408.

  • Property mode set to 100644
File size: 4.1 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
33CHAIN_DEFINE_EMPTY( _User_extensions_Switches_list );
34
35static Thread_Action *_Thread_Get_post_switch_action(
36  Thread_Control *executing
37)
38{
39  Chain_Control *chain = &executing->Post_switch_actions.Chain;
40
41  return (Thread_Action *) _Chain_Get_unprotected( chain );
42}
43
44static void _Thread_Run_post_switch_actions( Thread_Control *executing )
45{
46  ISR_Level        level;
47  Per_CPU_Control *cpu_self;
48  Thread_Action   *action;
49
50  cpu_self = _Thread_Action_ISR_disable_and_acquire( executing, &level );
51  action = _Thread_Get_post_switch_action( executing );
52
53  while ( action != NULL ) {
54    _Chain_Set_off_chain( &action->Node );
55
56    ( *action->handler )( executing, action, cpu_self, level );
57
58    cpu_self = _Thread_Action_ISR_disable_and_acquire( executing, &level );
59    action = _Thread_Get_post_switch_action( executing );
60  }
61
62  _Thread_Action_release_and_ISR_enable( cpu_self, level );
63}
64
65void _Thread_Do_dispatch( Per_CPU_Control *cpu_self, ISR_Level level )
66{
67  Thread_Control *executing;
68
69  _Assert( cpu_self->thread_dispatch_disable_level == 1 );
70
71  executing = cpu_self->executing;
72
73  do {
74    Thread_Control *heir = _Thread_Get_heir_and_make_it_executing( cpu_self );
75
76    /*
77     *  When the heir and executing are the same, then we are being
78     *  requested to do the post switch dispatching.  This is normally
79     *  done to dispatch signals.
80     */
81    if ( heir == executing )
82      goto post_switch;
83
84    /*
85     *  Since heir and executing are not the same, we need to do a real
86     *  context switch.
87     */
88#if __RTEMS_ADA__
89    executing->rtems_ada_self = rtems_ada_self;
90    rtems_ada_self = heir->rtems_ada_self;
91#endif
92    if ( heir->budget_algorithm == THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE )
93      heir->cpu_time_budget = rtems_configuration_get_ticks_per_timeslice();
94
95    /*
96     * On SMP the complete context switch must be atomic with respect to one
97     * processor.  See also _Thread_Handler() since _Context_switch() may branch
98     * to this function.
99     */
100#if !defined( RTEMS_SMP )
101    _ISR_Enable( level );
102#endif
103
104    _Thread_Update_cpu_time_used(
105      executing,
106      &cpu_self->time_of_last_context_switch
107    );
108
109    _User_extensions_Thread_switch( executing, heir );
110    _Thread_Save_fp( executing );
111    _Context_Switch( &executing->Registers, &heir->Registers );
112    _Thread_Restore_fp( executing );
113
114    /*
115     * We have to obtain this value again after the context switch since the
116     * heir thread may have migrated from another processor.  Values from the
117     * stack or non-volatile registers reflect the old execution environment.
118     */
119    cpu_self = _Per_CPU_Get();
120
121    _Thread_Debug_set_real_processor( executing, cpu_self );
122
123#if !defined( RTEMS_SMP )
124    _ISR_Disable( level );
125#endif
126  } while (
127#if defined( RTEMS_SMP )
128    false
129#else
130    cpu_self->dispatch_necessary
131#endif
132  );
133
134post_switch:
135  _Assert( cpu_self->thread_dispatch_disable_level == 1 );
136  cpu_self->thread_dispatch_disable_level = 0;
137  _Profiling_Thread_dispatch_enable( cpu_self, 0 );
138
139  _ISR_Enable_without_giant( level );
140
141  _Thread_Run_post_switch_actions( executing );
142}
143
144void _Thread_Dispatch( void )
145{
146  ISR_Level        level;
147  Per_CPU_Control *cpu_self;
148
149  _ISR_Disable_without_giant( level );
150
151  cpu_self = _Per_CPU_Get();
152
153  if ( cpu_self->dispatch_necessary ) {
154    _Profiling_Thread_dispatch_disable( cpu_self, 0 );
155    cpu_self->thread_dispatch_disable_level = 1;
156    _Thread_Do_dispatch( cpu_self, level );
157  } else {
158    _ISR_Enable_without_giant( level );
159  }
160}
Note: See TracBrowser for help on using the repository browser.