1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Remove Watchdog from List |
---|
5 | * @ingroup ScoreWatchdog |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-1999. |
---|
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.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/score/watchdogimpl.h> |
---|
22 | #include <rtems/score/assert.h> |
---|
23 | |
---|
24 | static void _Watchdog_Remove_it( |
---|
25 | Watchdog_Header *header, |
---|
26 | Watchdog_Control *the_watchdog |
---|
27 | ) |
---|
28 | { |
---|
29 | Chain_Node *next; |
---|
30 | Watchdog_Interval delta; |
---|
31 | const Chain_Node *iterator_tail; |
---|
32 | Chain_Node *iterator_node; |
---|
33 | |
---|
34 | _Assert( the_watchdog->state == WATCHDOG_ACTIVE ); |
---|
35 | |
---|
36 | the_watchdog->state = WATCHDOG_INACTIVE; |
---|
37 | the_watchdog->stop_time = _Watchdog_Ticks_since_boot; |
---|
38 | |
---|
39 | next = _Chain_Next( &the_watchdog->Node ); |
---|
40 | delta = the_watchdog->delta_interval; |
---|
41 | |
---|
42 | if ( next != _Chain_Tail( &header->Watchdogs ) ) { |
---|
43 | Watchdog_Control *next_watchdog; |
---|
44 | |
---|
45 | next_watchdog = (Watchdog_Control *) next; |
---|
46 | next_watchdog->delta_interval += delta; |
---|
47 | } |
---|
48 | |
---|
49 | _Chain_Extract_unprotected( &the_watchdog->Node ); |
---|
50 | |
---|
51 | iterator_node = _Chain_First( &header->Iterators ); |
---|
52 | iterator_tail = _Chain_Immutable_tail( &header->Iterators ); |
---|
53 | |
---|
54 | while ( iterator_node != iterator_tail ) { |
---|
55 | Watchdog_Iterator *iterator; |
---|
56 | |
---|
57 | iterator = (Watchdog_Iterator *) iterator_node; |
---|
58 | |
---|
59 | if ( iterator->current == next ) { |
---|
60 | iterator->delta_interval += delta; |
---|
61 | } |
---|
62 | |
---|
63 | if ( iterator->current == &the_watchdog->Node ) { |
---|
64 | iterator->current = _Chain_Previous( &the_watchdog->Node ); |
---|
65 | } |
---|
66 | |
---|
67 | iterator_node = _Chain_Next( iterator_node ); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | Watchdog_States _Watchdog_Remove( |
---|
72 | Watchdog_Header *header, |
---|
73 | Watchdog_Control *the_watchdog |
---|
74 | ) |
---|
75 | { |
---|
76 | ISR_lock_Context lock_context; |
---|
77 | Watchdog_States previous_state; |
---|
78 | Watchdog_Interval now; |
---|
79 | |
---|
80 | _Watchdog_Acquire( header, &lock_context ); |
---|
81 | previous_state = the_watchdog->state; |
---|
82 | switch ( previous_state ) { |
---|
83 | case WATCHDOG_INACTIVE: |
---|
84 | break; |
---|
85 | |
---|
86 | case WATCHDOG_BEING_INSERTED: |
---|
87 | |
---|
88 | /* |
---|
89 | * It is not actually on the chain so just change the state and |
---|
90 | * the Insert operation we interrupted will be aborted. |
---|
91 | */ |
---|
92 | the_watchdog->state = WATCHDOG_INACTIVE; |
---|
93 | now = _Watchdog_Ticks_since_boot; |
---|
94 | the_watchdog->start_time = now; |
---|
95 | the_watchdog->stop_time = now; |
---|
96 | break; |
---|
97 | |
---|
98 | case WATCHDOG_ACTIVE: |
---|
99 | _Watchdog_Remove_it( header, the_watchdog ); |
---|
100 | break; |
---|
101 | } |
---|
102 | |
---|
103 | _Watchdog_Release( header, &lock_context ); |
---|
104 | return( previous_state ); |
---|
105 | } |
---|
106 | |
---|
107 | void _Watchdog_Tickle( |
---|
108 | Watchdog_Header *header |
---|
109 | ) |
---|
110 | { |
---|
111 | ISR_lock_Context lock_context; |
---|
112 | |
---|
113 | _Watchdog_Acquire( header, &lock_context ); |
---|
114 | |
---|
115 | if ( !_Watchdog_Is_empty( header ) ) { |
---|
116 | Watchdog_Control *first; |
---|
117 | Watchdog_Interval delta; |
---|
118 | |
---|
119 | first = _Watchdog_First( header ); |
---|
120 | delta = first->delta_interval; |
---|
121 | |
---|
122 | /* |
---|
123 | * Although it is forbidden to insert watchdogs with a delta interval of |
---|
124 | * zero it is possible to observe watchdogs with a delta interval of zero |
---|
125 | * at this point. For example lets have a watchdog chain of one watchdog |
---|
126 | * with a delta interval of one and insert a new one with an initial value |
---|
127 | * of one. At the start of the insert procedure it will advance one step |
---|
128 | * and reduce its delta interval by one yielding zero. Now a tick happens. |
---|
129 | * This will remove the watchdog on the chain and update the insert |
---|
130 | * iterator. Now the insert operation continues and will insert the new |
---|
131 | * watchdog with a delta interval of zero. |
---|
132 | */ |
---|
133 | if ( delta > 0 ) { |
---|
134 | --delta; |
---|
135 | first->delta_interval = delta; |
---|
136 | } |
---|
137 | |
---|
138 | while ( delta == 0 ) { |
---|
139 | bool run; |
---|
140 | Watchdog_Service_routine_entry routine; |
---|
141 | Objects_Id id; |
---|
142 | void *user_data; |
---|
143 | |
---|
144 | run = ( first->state == WATCHDOG_ACTIVE ); |
---|
145 | |
---|
146 | _Watchdog_Remove_it( header, first ); |
---|
147 | |
---|
148 | routine = first->routine; |
---|
149 | id = first->id; |
---|
150 | user_data = first->user_data; |
---|
151 | |
---|
152 | _Watchdog_Release( header, &lock_context ); |
---|
153 | |
---|
154 | if ( run ) { |
---|
155 | (*routine)( id, user_data ); |
---|
156 | } |
---|
157 | |
---|
158 | _Watchdog_Acquire( header, &lock_context ); |
---|
159 | |
---|
160 | if ( _Watchdog_Is_empty( header ) ) { |
---|
161 | break; |
---|
162 | } |
---|
163 | |
---|
164 | first = _Watchdog_First( header ); |
---|
165 | delta = first->delta_interval; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | _Watchdog_Release( header, &lock_context ); |
---|
170 | } |
---|