1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief RTEMS Task Mode |
---|
5 | * @ingroup ClassicTasks |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2010. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.com/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/system.h> |
---|
22 | #include <rtems/config.h> |
---|
23 | #include <rtems/rtems/status.h> |
---|
24 | #include <rtems/rtems/support.h> |
---|
25 | #include <rtems/rtems/modes.h> |
---|
26 | #include <rtems/score/object.h> |
---|
27 | #include <rtems/score/stack.h> |
---|
28 | #include <rtems/score/states.h> |
---|
29 | #include <rtems/rtems/tasks.h> |
---|
30 | #include <rtems/score/thread.h> |
---|
31 | #include <rtems/score/threadq.h> |
---|
32 | #include <rtems/score/tod.h> |
---|
33 | #include <rtems/score/wkspace.h> |
---|
34 | #include <rtems/score/apiext.h> |
---|
35 | #include <rtems/score/sysstate.h> |
---|
36 | |
---|
37 | rtems_status_code rtems_task_mode( |
---|
38 | rtems_mode mode_set, |
---|
39 | rtems_mode mask, |
---|
40 | rtems_mode *previous_mode_set |
---|
41 | ) |
---|
42 | { |
---|
43 | Thread_Control *executing; |
---|
44 | RTEMS_API_Control *api; |
---|
45 | ASR_Information *asr; |
---|
46 | bool needs_asr_dispatching; |
---|
47 | rtems_mode old_mode; |
---|
48 | |
---|
49 | if ( !previous_mode_set ) |
---|
50 | return RTEMS_INVALID_ADDRESS; |
---|
51 | |
---|
52 | executing = _Thread_Get_executing(); |
---|
53 | api = executing->API_Extensions[ THREAD_API_RTEMS ]; |
---|
54 | asr = &api->Signal; |
---|
55 | |
---|
56 | old_mode = (executing->is_preemptible) ? RTEMS_PREEMPT : RTEMS_NO_PREEMPT; |
---|
57 | |
---|
58 | if ( executing->budget_algorithm == THREAD_CPU_BUDGET_ALGORITHM_NONE ) |
---|
59 | old_mode |= RTEMS_NO_TIMESLICE; |
---|
60 | else |
---|
61 | old_mode |= RTEMS_TIMESLICE; |
---|
62 | |
---|
63 | old_mode |= (asr->is_enabled) ? RTEMS_ASR : RTEMS_NO_ASR; |
---|
64 | old_mode |= _ISR_Get_level(); |
---|
65 | |
---|
66 | *previous_mode_set = old_mode; |
---|
67 | |
---|
68 | /* |
---|
69 | * These are generic thread scheduling characteristics. |
---|
70 | */ |
---|
71 | if ( mask & RTEMS_PREEMPT_MASK ) { |
---|
72 | #if defined( RTEMS_SMP ) |
---|
73 | if ( |
---|
74 | rtems_configuration_is_smp_enabled() |
---|
75 | && !_Modes_Is_preempt( mode_set ) |
---|
76 | ) { |
---|
77 | return RTEMS_NOT_IMPLEMENTED; |
---|
78 | } |
---|
79 | #endif |
---|
80 | |
---|
81 | executing->is_preemptible = _Modes_Is_preempt( mode_set ); |
---|
82 | } |
---|
83 | |
---|
84 | if ( mask & RTEMS_TIMESLICE_MASK ) { |
---|
85 | if ( _Modes_Is_timeslice(mode_set) ) { |
---|
86 | executing->budget_algorithm = THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE; |
---|
87 | executing->cpu_time_budget = _Thread_Ticks_per_timeslice; |
---|
88 | } else |
---|
89 | executing->budget_algorithm = THREAD_CPU_BUDGET_ALGORITHM_NONE; |
---|
90 | } |
---|
91 | |
---|
92 | /* |
---|
93 | * Set the new interrupt level |
---|
94 | */ |
---|
95 | if ( mask & RTEMS_INTERRUPT_MASK ) |
---|
96 | _Modes_Set_interrupt_level( mode_set ); |
---|
97 | |
---|
98 | /* |
---|
99 | * This is specific to the RTEMS API |
---|
100 | */ |
---|
101 | needs_asr_dispatching = false; |
---|
102 | if ( mask & RTEMS_ASR_MASK ) { |
---|
103 | bool is_asr_enabled = !_Modes_Is_asr_disabled( mode_set ); |
---|
104 | |
---|
105 | if ( is_asr_enabled != asr->is_enabled ) { |
---|
106 | asr->is_enabled = is_asr_enabled; |
---|
107 | _ASR_Swap_signals( asr ); |
---|
108 | if ( _ASR_Are_signals_pending( asr ) ) { |
---|
109 | needs_asr_dispatching = true; |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | _Thread_Dispatch_if_necessary( executing, needs_asr_dispatching ); |
---|
115 | |
---|
116 | return RTEMS_SUCCESSFUL; |
---|
117 | } |
---|