1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Thread Dispatch Disable Functions |
---|
5 | * |
---|
6 | * @ingroup ScoreThread |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2011. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.org/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #include <rtems/score/threaddispatch.h> |
---|
19 | #include <rtems/score/assert.h> |
---|
20 | #include <rtems/score/profiling.h> |
---|
21 | #include <rtems/score/sysstate.h> |
---|
22 | |
---|
23 | #define NO_OWNER_CPU NULL |
---|
24 | |
---|
25 | typedef struct { |
---|
26 | SMP_lock_Control lock; |
---|
27 | Per_CPU_Control *owner_cpu; |
---|
28 | uint32_t nest_level; |
---|
29 | } Giant_Control; |
---|
30 | |
---|
31 | static Giant_Control _Giant = { |
---|
32 | .lock = SMP_LOCK_INITIALIZER("Giant"), |
---|
33 | .owner_cpu = NO_OWNER_CPU, |
---|
34 | .nest_level = 0 |
---|
35 | }; |
---|
36 | |
---|
37 | static void _Giant_Do_acquire( Per_CPU_Control *self_cpu ) |
---|
38 | { |
---|
39 | Giant_Control *giant = &_Giant; |
---|
40 | |
---|
41 | if ( giant->owner_cpu != self_cpu ) { |
---|
42 | _SMP_lock_Acquire( &giant->lock, &self_cpu->Giant_lock_context ); |
---|
43 | giant->owner_cpu = self_cpu; |
---|
44 | giant->nest_level = 1; |
---|
45 | } else { |
---|
46 | ++giant->nest_level; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | static void _Giant_Do_release( Per_CPU_Control *self_cpu ) |
---|
51 | { |
---|
52 | Giant_Control *giant = &_Giant; |
---|
53 | |
---|
54 | --giant->nest_level; |
---|
55 | if ( giant->nest_level == 0 ) { |
---|
56 | giant->owner_cpu = NO_OWNER_CPU; |
---|
57 | _SMP_lock_Release( &giant->lock, &self_cpu->Giant_lock_context ); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | void _Giant_Drop( Per_CPU_Control *self_cpu ) |
---|
62 | { |
---|
63 | Giant_Control *giant = &_Giant; |
---|
64 | |
---|
65 | _Assert( _ISR_Get_level() != 0 ); |
---|
66 | |
---|
67 | if ( giant->owner_cpu == self_cpu ) { |
---|
68 | giant->nest_level = 0; |
---|
69 | giant->owner_cpu = NO_OWNER_CPU; |
---|
70 | _SMP_lock_Release( &giant->lock, &self_cpu->Giant_lock_context ); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | uint32_t _Thread_Dispatch_increment_disable_level( void ) |
---|
75 | { |
---|
76 | ISR_Level isr_level; |
---|
77 | uint32_t disable_level; |
---|
78 | Per_CPU_Control *self_cpu; |
---|
79 | |
---|
80 | _ISR_Disable_without_giant( isr_level ); |
---|
81 | |
---|
82 | /* |
---|
83 | * We must obtain the processor after interrupts are disabled to prevent |
---|
84 | * thread migration. |
---|
85 | */ |
---|
86 | self_cpu = _Per_CPU_Get(); |
---|
87 | |
---|
88 | _Giant_Do_acquire( self_cpu ); |
---|
89 | |
---|
90 | disable_level = self_cpu->thread_dispatch_disable_level; |
---|
91 | _Profiling_Thread_dispatch_disable( self_cpu, disable_level ); |
---|
92 | ++disable_level; |
---|
93 | self_cpu->thread_dispatch_disable_level = disable_level; |
---|
94 | |
---|
95 | _ISR_Enable_without_giant( isr_level ); |
---|
96 | |
---|
97 | return disable_level; |
---|
98 | } |
---|
99 | |
---|
100 | uint32_t _Thread_Dispatch_decrement_disable_level( void ) |
---|
101 | { |
---|
102 | ISR_Level isr_level; |
---|
103 | uint32_t disable_level; |
---|
104 | Per_CPU_Control *self_cpu; |
---|
105 | |
---|
106 | _ISR_Disable_without_giant( isr_level ); |
---|
107 | |
---|
108 | self_cpu = _Per_CPU_Get(); |
---|
109 | disable_level = self_cpu->thread_dispatch_disable_level; |
---|
110 | --disable_level; |
---|
111 | self_cpu->thread_dispatch_disable_level = disable_level; |
---|
112 | |
---|
113 | _Giant_Do_release( self_cpu ); |
---|
114 | _Assert( disable_level != 0 || _Giant.owner_cpu != self_cpu ); |
---|
115 | |
---|
116 | _Profiling_Thread_dispatch_enable( self_cpu, disable_level ); |
---|
117 | _ISR_Enable_without_giant( isr_level ); |
---|
118 | |
---|
119 | return disable_level; |
---|
120 | } |
---|
121 | |
---|
122 | void _Giant_Acquire( void ) |
---|
123 | { |
---|
124 | ISR_Level isr_level; |
---|
125 | |
---|
126 | _ISR_Disable_without_giant( isr_level ); |
---|
127 | _Assert( _Thread_Dispatch_disable_level != 0 ); |
---|
128 | _Giant_Do_acquire( _Per_CPU_Get() ); |
---|
129 | _ISR_Enable_without_giant( isr_level ); |
---|
130 | } |
---|
131 | |
---|
132 | void _Giant_Release( void ) |
---|
133 | { |
---|
134 | ISR_Level isr_level; |
---|
135 | |
---|
136 | _ISR_Disable_without_giant( isr_level ); |
---|
137 | _Assert( _Thread_Dispatch_disable_level != 0 ); |
---|
138 | _Giant_Do_release( _Per_CPU_Get() ); |
---|
139 | _ISR_Enable_without_giant( isr_level ); |
---|
140 | } |
---|
141 | |
---|
142 | #if defined( RTEMS_DEBUG ) |
---|
143 | bool _Debug_Is_owner_of_giant( void ) |
---|
144 | { |
---|
145 | Giant_Control *giant = &_Giant; |
---|
146 | |
---|
147 | return giant->owner_cpu == _Per_CPU_Get_snapshot() |
---|
148 | || !_System_state_Is_up( _System_state_Get() ); |
---|
149 | } |
---|
150 | #endif |
---|