From c951c30d8a2e67ffe80bdf05d6d0b829f34f0e41 Mon Sep 17 00:00:00 2001 From: Stefan Kalkowski Date: Tue, 30 Jun 2015 11:56:03 +0200 Subject: [PATCH] base: extend weak pointer test (Ref #1607) Add a test where a locked pointer shall be taken during object destruction. Moreover, extend the run-script so it runs on different platforms with "real" timers. --- repos/base/src/test/weak_ptr/main.cc | 67 +++++++++++++++++++++++++--- repos/os/run/weak_ptr.run | 3 ++ 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/repos/base/src/test/weak_ptr/main.cc b/repos/base/src/test/weak_ptr/main.cc index 2c2b6f011..ba387e427 100644 --- a/repos/base/src/test/weak_ptr/main.cc +++ b/repos/base/src/test/weak_ptr/main.cc @@ -40,7 +40,7 @@ static int weak_ptr_is_valid; void Genode::Weak_ptr_base::debug_info() const { - weak_ptr_is_valid = _valid; + weak_ptr_is_valid = (_obj != nullptr); } @@ -82,7 +82,7 @@ static bool object_is_constructed; struct Object : Genode::Weak_object { Object() { object_is_constructed = true; } - + ~Object() { Weak_object::lock_for_destruction(); @@ -142,9 +142,10 @@ static void test_weak_pointer_tracking() ** Test for deferring object destruction ** *******************************************/ +template struct Destruct_thread : Genode::Thread<4096> { - Object *obj; + O *obj; void entry() { @@ -154,7 +155,7 @@ struct Destruct_thread : Genode::Thread<4096> PLOG("thread: destruction completed, job done"); } - Destruct_thread(Object *obj) : Thread("object_destructor"), obj(obj) { } + Destruct_thread(O *obj) : Thread("object_destructor"), obj(obj) { } }; @@ -183,7 +184,7 @@ static void test_deferred_destruction() assert_constructed(true); /* create thread that will be used to destruct the object */ - Destruct_thread destruct_thread(obj); + Destruct_thread destruct_thread(obj); { /* acquire possession over the object */ @@ -252,6 +253,59 @@ static void test_acquisition_failure() } +/******************************************************* + ** Test the failed aquisition during the destruction ** + *******************************************************/ + +struct Object_with_delayed_destruction +: Genode::Weak_object +{ + Timer::Connection timer; + + Object_with_delayed_destruction() { object_is_constructed = true; } + + ~Object_with_delayed_destruction() + { + Weak_object::lock_for_destruction(); + timer.msleep(2000); + object_is_constructed = false; + } +}; + + +static void test_acquisition_during_destruction() +{ + using namespace Genode; + + static Timer::Connection timer; + + Object_with_delayed_destruction *obj = + new (env()->heap()) Object_with_delayed_destruction(); + + Weak_ptr ptr = obj->weak_ptr(); + assert_weak_ptr_cnt(obj, 1); + assert_weak_ptr_valid(ptr, true); + assert_constructed(true); + + /* create and start thread that will be used to destruct the object */ + Destruct_thread destruct_thread(obj); + destruct_thread.start(); + + /* wait so that the thread enters the destructor */ + timer.msleep(500); + + { + /* acquire possession over the object */ + Locked_ptr locked_ptr(ptr); + + /* the object should be invalid */ + assert_weak_ptr_valid(ptr, false); + } + + /* synchronize destruction of thread */ + destruct_thread.join(); +} + /****************** ** Main program ** ******************/ @@ -271,6 +325,9 @@ int main(int argc, char **argv) printf("\n-- test acquisition failure --\n"); test_acquisition_failure(); + printf("\n-- test acquisition during destruction --\n"); + test_acquisition_during_destruction(); + printf("\n--- finished test-weak_ptr ---\n"); return 0; } diff --git a/repos/os/run/weak_ptr.run b/repos/os/run/weak_ptr.run index c924596b6..04065a544 100644 --- a/repos/os/run/weak_ptr.run +++ b/repos/os/run/weak_ptr.run @@ -12,6 +12,9 @@ install_config { + + +