From 2cc001a15a86b123044369845929d5dee3e4c9bb Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Sun, 20 Dec 2015 21:47:12 +0100 Subject: score: Fix watchdog removal Under certain conditions a new watchdog was inserted with a wrong and very large delta interval due to an incomplete iterator update. Bug was introduced by 1ccbd052910ed16131c74b0d5595c8a94066942d. Close #2501. --- cpukit/score/src/watchdogremove.c | 11 ++++++- testsuites/sptests/spwatchdog/init.c | 62 +++++++++++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/cpukit/score/src/watchdogremove.c b/cpukit/score/src/watchdogremove.c index 03c0c5f0fe..2aa72a4e75 100644 --- a/cpukit/score/src/watchdogremove.c +++ b/cpukit/score/src/watchdogremove.c @@ -61,7 +61,16 @@ static void _Watchdog_Remove_it( } if ( iterator->current == &the_watchdog->Node ) { - iterator->current = _Chain_Previous( &the_watchdog->Node ); + Chain_Node *previous = _Chain_Previous( &the_watchdog->Node ); + + iterator->current = previous; + + if ( previous != _Chain_Head( &header->Watchdogs ) ) { + Watchdog_Control *previous_watchdog; + + previous_watchdog = (Watchdog_Control *) previous; + iterator->delta_interval += previous_watchdog->delta_interval; + } } iterator_node = _Chain_Next( iterator_node ); diff --git a/testsuites/sptests/spwatchdog/init.c b/testsuites/sptests/spwatchdog/init.c index b96b680e74..d99c558d43 100644 --- a/testsuites/sptests/spwatchdog/init.c +++ b/testsuites/sptests/spwatchdog/init.c @@ -36,7 +36,7 @@ static void test_watchdog_routine( Objects_Id id, void *arg ) static void init_watchdogs( Watchdog_Header *header, - Watchdog_Control watchdogs[3] + Watchdog_Control watchdogs[4] ) { Watchdog_Control *a = &watchdogs[0]; @@ -105,23 +105,23 @@ static void test_watchdog_insert_and_remove( void ) /* Remove next watchdog of iterator */ _Watchdog_Remove( &header, c ); - rtems_test_assert( i.delta_interval == 2 ); + rtems_test_assert( i.delta_interval == 4 ); rtems_test_assert( i.current == &b->Node ); /* Remove watchdog before the current watchdog of iterator */ _Watchdog_Remove( &header, a ); - rtems_test_assert( i.delta_interval == 4 ); + rtems_test_assert( i.delta_interval == 6 ); rtems_test_assert( i.current == &b->Node ); /* Remove current (= last) watchdog of iterator */ _Watchdog_Remove( &header, b ); - rtems_test_assert( i.delta_interval == 4 ); + rtems_test_assert( i.delta_interval == 6 ); rtems_test_assert( i.current == _Chain_Head( &header.Watchdogs ) ); /* Insert first watchdog */ a->initial = 1; _Watchdog_Insert( &header, a ); - rtems_test_assert( i.delta_interval == 4 ); + rtems_test_assert( i.delta_interval == 6 ); rtems_test_assert( i.current == _Chain_Head( &header.Watchdogs ) ); destroy_watchdogs( &header ); @@ -147,6 +147,57 @@ static void test_watchdog_insert_and_remove( void ) destroy_watchdogs( &header ); } +static void init_watchdogs_remove_second_and_insert_first( + Watchdog_Header *header, + Watchdog_Control watchdogs[3] +) +{ + Watchdog_Control *a = &watchdogs[0]; + Watchdog_Control *b = &watchdogs[1]; + Watchdog_Control *c = &watchdogs[2]; + + _Watchdog_Preinitialize( a ); + _Watchdog_Preinitialize( b ); + _Watchdog_Preinitialize( c ); + + _Watchdog_Header_initialize( header ); + + a->initial = 6; + _Watchdog_Insert( header, a ); + rtems_test_assert( a->delta_interval == 6 ); + + b->initial = 8; + _Watchdog_Insert( header, b ); + rtems_test_assert( a->delta_interval == 6 ); + rtems_test_assert( b->delta_interval == 2 ); +} + +static void test_watchdog_remove_second_and_insert_first( void ) +{ + Watchdog_Header header; + Watchdog_Control watchdogs[3]; + Watchdog_Control *a = &watchdogs[0]; + Watchdog_Control *b = &watchdogs[1]; + Watchdog_Control *c = &watchdogs[2]; + Watchdog_Iterator i; + + init_watchdogs_remove_second_and_insert_first( &header, watchdogs ); + add_iterator( &header, &i, b ); + + _Watchdog_Remove( &header, b ); + rtems_test_assert( i.delta_interval == 8 ); + rtems_test_assert( i.current == &a->Node ); + + c->initial = 4; + _Watchdog_Insert( &header, c ); + rtems_test_assert( a->delta_interval == 2 ); + rtems_test_assert( c->delta_interval == 4 ); + rtems_test_assert( i.delta_interval == 4 ); + rtems_test_assert( i.current == &a->Node ); + + destroy_watchdogs( &header ); +} + static void test_watchdog_static_init( void ) { #if defined(RTEMS_USE_16_BIT_OBJECT) @@ -184,6 +235,7 @@ rtems_task Init( test_watchdog_static_init(); test_watchdog_insert_and_remove(); + test_watchdog_remove_second_and_insert_first(); build_time( &time, 12, 31, 1988, 9, 0, 0, 0 ); -- cgit v1.2.3