From 0be681722653ccbf4b96dcdb2fae82cdb74e4ff8 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Thu, 5 Jun 2014 15:55:34 +0200 Subject: [PATCH] Add 'Weak_ptr' to the public Genode API So far, the lifetime-management utilities 'Weak_ptr' and 'Locked_ptr' had been preserved for core-internal use only. However, the utilities are handy for many use cases outside of core where object lifetimes must be managed. So we promote them to the public API. --- .../lifetime.h => include/base/weak_ptr.h} | 61 ++++++++++--------- repos/base/src/core/include/address_space.h | 4 +- .../src/core/include/trace/source_registry.h | 6 +- .../src/core/include/trace/subject_registry.h | 2 +- .../src/test/{lifetime => weak_ptr}/main.cc | 18 +++--- .../src/test/{lifetime => weak_ptr}/target.mk | 2 +- repos/os/run/{lifetime.run => weak_ptr.run} | 8 +-- 7 files changed, 50 insertions(+), 51 deletions(-) rename repos/base/{src/core/include/lifetime.h => include/base/weak_ptr.h} (81%) rename repos/base/src/test/{lifetime => weak_ptr}/main.cc (93%) rename repos/base/src/test/{lifetime => weak_ptr}/target.mk (74%) rename repos/os/run/{lifetime.run => weak_ptr.run} (77%) diff --git a/repos/base/src/core/include/lifetime.h b/repos/base/include/base/weak_ptr.h similarity index 81% rename from repos/base/src/core/include/lifetime.h rename to repos/base/include/base/weak_ptr.h index 2df404db6..754a803a7 100644 --- a/repos/base/src/core/include/lifetime.h +++ b/repos/base/include/base/weak_ptr.h @@ -14,7 +14,7 @@ * * The utilities provided herein implement a more elegant pattern called * "weak pointers" to deal with such situations. An object that might - * disappear at any time is represented by the 'Volatile_object' class + * disappear at any time is represented by the 'Weak_object' class * template. It keeps track of a list of so-called weak pointers pointing * to the object. A weak pointer, in turn, holds privately the pointer to the * object alongside a validity flag. It cannot be used to dereference the @@ -25,14 +25,14 @@ * can (and should) be detected via the 'Locked_ptr::is_valid()' function prior * dereferencing the pointer. * - * In the event a volatile object gets destructed, all weak pointers that point + * In the event a weak object gets destructed, all weak pointers that point * to the object are automatically invalidated. So a subsequent conversion into * a locked pointer will yield an invalid pointer, which can be detected (in * contrast to a dangling pointer). * - * To use this mechanism, the destruction of a volatile object must be + * To use this mechanism, the destruction of a weak object must be * deferred until no locked pointer points to the object anymore. This is - * done by calling the function 'Volatile_object::lock_for_destruction()' + * done by calling the function 'Weak_object::lock_for_destruction()' * at the beginning of the destructor of the to-be-destructed object. * When this function returns, all weak pointers to the object will have been * invalidated. So it is save to destruct and free the object. @@ -45,17 +45,18 @@ * under the terms of the GNU General Public License version 2. */ -#ifndef _CORE__INCLUDE__LIFETIME_H_ -#define _CORE__INCLUDE__LIFETIME_H_ +#ifndef _INCLUDE__BASE__WEAK_PTR_H_ +#define _INCLUDE__BASE__WEAK_PTR_H_ #include +#include namespace Genode { - class Volatile_object_base; + class Weak_object_base; class Weak_ptr_base; class Locked_ptr_base; - template struct Volatile_object; + template struct Weak_object; template struct Weak_ptr; template struct Locked_ptr; } @@ -65,22 +66,22 @@ class Genode::Weak_ptr_base : public Genode::List::Element { private: - friend class Volatile_object_base; + friend class Weak_object_base; friend class Locked_ptr_base; - Lock mutable _lock; - Volatile_object_base *_obj; - bool _valid; /* true if '_obj' points to an - existing object */ + Lock mutable _lock; + Weak_object_base *_obj; + bool _valid; /* true if '_obj' points to an + existing object */ - inline void _adopt(Volatile_object_base *obj); + inline void _adopt(Weak_object_base *obj); inline void _disassociate(); protected: - Volatile_object_base *obj() const { return _valid ? _obj: 0; } + Weak_object_base *obj() const { return _valid ? _obj: 0; } - explicit inline Weak_ptr_base(Volatile_object_base *obj); + explicit inline Weak_ptr_base(Weak_object_base *obj); public: @@ -108,7 +109,7 @@ class Genode::Weak_ptr_base : public Genode::List::Element }; -class Genode::Volatile_object_base +class Genode::Weak_object_base { private: @@ -123,16 +124,16 @@ class Genode::Volatile_object_base /** * Lock used to defer the destruction of an object derived from - * 'Volatile_object_base' + * 'Weak_object_base' */ Lock _destruct_lock; protected: - inline ~Volatile_object_base(); + inline ~Weak_object_base(); /** - * To be called from 'Volatile_object' only + * To be called from 'Weak_object' only */ template Weak_ptr _weak_ptr(); @@ -140,7 +141,7 @@ class Genode::Volatile_object_base public: /** - * Function to be called by the destructor of a volatile object to + * Function to be called by the destructor of a weak object to * defer the destruction until no 'Locked_ptr' is held to the object. */ void lock_for_destruction() { _destruct_lock.lock(); } @@ -156,7 +157,7 @@ class Genode::Locked_ptr_base { protected: - Volatile_object_base *curr; + Weak_object_base *curr; inline Locked_ptr_base(Weak_ptr_base &weak_ptr); inline ~Locked_ptr_base(); @@ -187,7 +188,7 @@ struct Genode::Weak_ptr : Genode::Weak_ptr_base template -struct Genode::Volatile_object : Genode::Volatile_object_base +struct Genode::Weak_object : Genode::Weak_object_base { Weak_ptr weak_ptr() { return _weak_ptr(); } }; @@ -200,6 +201,8 @@ struct Genode::Locked_ptr : Genode::Locked_ptr_base T *operator -> () { return static_cast(curr); } + T &operator * () { return *static_cast(curr); } + bool is_valid() const { return curr != 0; } }; @@ -208,7 +211,7 @@ struct Genode::Locked_ptr : Genode::Locked_ptr_base ** Implementation ** ********************/ -void Genode::Weak_ptr_base::_adopt(Genode::Volatile_object_base *obj) +void Genode::Weak_ptr_base::_adopt(Genode::Weak_object_base *obj) { if (!obj) return; @@ -252,7 +255,7 @@ void Genode::Weak_ptr_base::_disassociate() } -Genode::Weak_ptr_base::Weak_ptr_base(Genode::Volatile_object_base *obj) +Genode::Weak_ptr_base::Weak_ptr_base(Genode::Weak_object_base *obj) { _adopt(obj); } @@ -267,7 +270,7 @@ void Genode::Weak_ptr_base::operator = (Weak_ptr_base const &other) if (&other == this) return; - Volatile_object_base *obj = other.obj(); + Weak_object_base *obj = other.obj(); _disassociate(); _adopt(obj); } @@ -292,14 +295,14 @@ Genode::Weak_ptr_base::~Weak_ptr_base() template -Genode::Weak_ptr Genode::Volatile_object_base::_weak_ptr() +Genode::Weak_ptr Genode::Weak_object_base::_weak_ptr() { Weak_ptr_base result(this); return *static_cast *>(&result); } -Genode::Volatile_object_base::~Volatile_object_base() +Genode::Weak_object_base::~Weak_object_base() { { Lock::Guard guard(_list_lock); @@ -334,4 +337,4 @@ Genode::Locked_ptr_base::~Locked_ptr_base() curr->_destruct_lock.unlock(); } -#endif /* _CORE__INCLUDE__LIFETIME_H_ */ +#endif /* _INCLUDE__BASE__WEAK_PTR_H_ */ diff --git a/repos/base/src/core/include/address_space.h b/repos/base/src/core/include/address_space.h index 6fe53efcc..822e497e4 100644 --- a/repos/base/src/core/include/address_space.h +++ b/repos/base/src/core/include/address_space.h @@ -15,11 +15,11 @@ #define _CORE__INCLUDE__ADDRESS_SPACE_H_ #include -#include +#include namespace Genode { struct Address_space; } -struct Genode::Address_space : Genode::Volatile_object +struct Genode::Address_space : Genode::Weak_object { /** * Flush memory mappings of virtual address range diff --git a/repos/base/src/core/include/trace/source_registry.h b/repos/base/src/core/include/trace/source_registry.h index 217bf150f..2ebbfe0b3 100644 --- a/repos/base/src/core/include/trace/source_registry.h +++ b/repos/base/src/core/include/trace/source_registry.h @@ -18,9 +18,7 @@ #include #include #include - -/* core includes */ -#include +#include /* base-internal include */ #include @@ -42,7 +40,7 @@ struct Genode::Trace::Source_owner { }; */ class Genode::Trace::Source : - public Genode::Volatile_object, + public Genode::Weak_object, public Genode::List::Element { private: diff --git a/repos/base/src/core/include/trace/subject_registry.h b/repos/base/src/core/include/trace/subject_registry.h index 6ef3e263e..3c4fb96e2 100644 --- a/repos/base/src/core/include/trace/subject_registry.h +++ b/repos/base/src/core/include/trace/subject_registry.h @@ -25,10 +25,10 @@ #include #include #include +#include #include /* core includes */ -#include #include /* base-internal include */ diff --git a/repos/base/src/test/lifetime/main.cc b/repos/base/src/test/weak_ptr/main.cc similarity index 93% rename from repos/base/src/test/lifetime/main.cc rename to repos/base/src/test/weak_ptr/main.cc index fe6af3af2..2c2b6f011 100644 --- a/repos/base/src/test/lifetime/main.cc +++ b/repos/base/src/test/weak_ptr/main.cc @@ -1,5 +1,5 @@ /* - * \brief Test for lifetime-management utilities + * \brief Test for weak-pointer utilities * \author Norman Feske * \date 2013-03-12 */ @@ -15,11 +15,9 @@ #include #include #include +#include #include -/* core includes */ -#include - /******************************************************************** ** Hooks for obtaining internal information of the tested classes ** @@ -28,7 +26,7 @@ static int weak_ptr_cnt; -void Genode::Volatile_object_base::debug_info() const +void Genode::Weak_object_base::debug_info() const { /* count number of weak pointers pointing to the object */ weak_ptr_cnt = 0; @@ -49,7 +47,7 @@ void Genode::Weak_ptr_base::debug_info() const struct Fatal_error { }; -static void assert_weak_ptr_cnt(Genode::Volatile_object_base const *obj, +static void assert_weak_ptr_cnt(Genode::Weak_object_base const *obj, int expected_cnt) { obj->debug_info(); @@ -81,13 +79,13 @@ static void assert_weak_ptr_valid(Genode::Weak_ptr_base const &ptr, bool valid) static bool object_is_constructed; -struct Object : Genode::Volatile_object +struct Object : Genode::Weak_object { Object() { object_is_constructed = true; } ~Object() { - Volatile_object::lock_for_destruction(); + Weak_object::lock_for_destruction(); object_is_constructed = false; } }; @@ -262,7 +260,7 @@ int main(int argc, char **argv) { using namespace Genode; - printf("--- test-lifetime started ---\n"); + printf("--- test-weak_ptr started ---\n"); printf("\n-- test tracking of weak pointers --\n"); test_weak_pointer_tracking(); @@ -273,6 +271,6 @@ int main(int argc, char **argv) printf("\n-- test acquisition failure --\n"); test_acquisition_failure(); - printf("\n--- finished test-lifetime ---\n"); + printf("\n--- finished test-weak_ptr ---\n"); return 0; } diff --git a/repos/base/src/test/lifetime/target.mk b/repos/base/src/test/weak_ptr/target.mk similarity index 74% rename from repos/base/src/test/lifetime/target.mk rename to repos/base/src/test/weak_ptr/target.mk index e68fc9379..9dd4faf32 100644 --- a/repos/base/src/test/lifetime/target.mk +++ b/repos/base/src/test/weak_ptr/target.mk @@ -1,4 +1,4 @@ -TARGET = test-lifetime +TARGET = test-weak_ptr SRC_CC = main.cc LIBS = base INC_DIR += $(REP_DIR)/src/core/include diff --git a/repos/os/run/lifetime.run b/repos/os/run/weak_ptr.run similarity index 77% rename from repos/os/run/lifetime.run rename to repos/os/run/weak_ptr.run index 36f3a10a0..c924596b6 100644 --- a/repos/os/run/lifetime.run +++ b/repos/os/run/weak_ptr.run @@ -1,4 +1,4 @@ -build "core init drivers/timer test/lifetime" +build "core init drivers/timer test/weak_ptr" create_boot_directory @@ -21,16 +21,16 @@ install_config { - + } -build_boot_image "core init timer test-lifetime" +build_boot_image "core init timer test-weak_ptr" append qemu_args "-nographic -m 64" -run_genode_until "--- finished test-lifetime ---.*\n" 30 +run_genode_until "--- finished test-weak_ptr ---.*\n" 30 puts "Test succeeded"