From b03816bbb8947e365791bd69542979800ab35242 Mon Sep 17 00:00:00 2001 From: mrbald Date: Tue, 17 Dec 2024 17:00:19 +0100 Subject: [PATCH] thread-unsafe memory type with seq-fit algorithm --- rtb/datacache/entity_cache.hpp | 8 ++++++- rtb/datacache/memory_types.hpp | 44 +++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/rtb/datacache/entity_cache.hpp b/rtb/datacache/entity_cache.hpp index f6229db..3eec15d 100644 --- a/rtb/datacache/entity_cache.hpp +++ b/rtb/datacache/entity_cache.hpp @@ -38,9 +38,11 @@ #include "rtb/core/core.hpp" #include "rtb/common/concepts.hpp" -#include +#include #include #include +#include +#include #include // Define an error_info tag for stacktrace @@ -139,6 +141,10 @@ class entity_cache // TODO: add to ctor to switch between mmap and shm // TODO: maybe needs bip::scoped_lock to lock for other processes calling grow_memory std::string data_base_dir = "/tmp/CACHE"; + + auto guard = bip::scoped_lock{_named_mutex, bip::defer_lock}; + VAV_REQUIRE(guard.try_lock_for(boost::chrono::seconds(10))); + _store_name = Memory::convert_base_dir(data_base_dir) + _cache_name; _segment_ptr.reset(Memory::open_or_create_segment(_store_name.c_str(), memory_size)); _container_ptr = _segment_ptr->template find_or_construct(_cache_name.c_str())( diff --git a/rtb/datacache/memory_types.hpp b/rtb/datacache/memory_types.hpp index 8b62727..1dc133f 100644 --- a/rtb/datacache/memory_types.hpp +++ b/rtb/datacache/memory_types.hpp @@ -23,12 +23,50 @@ #include //Variant-II , open(), mmap() #include // Variant III for heap #include - +#include #include #include namespace mpclmi::ipc { - + +/** A variation of the Shared: + * - simpler memory allocation algorithm suitable for segments storing a single node-based cotainer + * - memory allocation is not thread safe, relying on external synchronization typical for entity_cache-based containers + */ +struct SharedVL { + using char_type = char; + using mutex_family_type = boost::interprocess::null_mutex_family; + using memory_algorithm_type = boost::interprocess::simple_seq_fit; + template using index_type = boost::interprocess::iset_index; + using segment_t = boost::interprocess::basic_managed_shared_memory; + using segment_manager_t = boost::interprocess::segment_manager; + + static segment_t * open_or_create_segment (const std::string &path, size_t size) { + return new segment_t(boost::interprocess::open_or_create, path.c_str(), size) ; + } + static segment_t * open_segment (const std::string &path) { + return new segment_t(boost::interprocess::open_only, path.c_str()) ; + } + static segment_t * create_segment (const std::string &path, size_t size) { + return new segment_t(boost::interprocess::create_only, path.c_str(), size) ; + } + template + static void grow(MemPtr &mem_ptr , const std::string &path, size_t size) { + mem_ptr.reset() ; + segment_t::grow(path.c_str(), size) ; + mem_ptr.reset(open_segment(path)) ; + } + static std::string convert_base_dir([[maybe_unused]] const std::string &base_dir) { + return "" ; + } + + template + static void attach( Function && f ) { + f() ; + } +}; + + struct Shared { typedef boost::interprocess::managed_shared_memory segment_t; typedef boost::interprocess::managed_shared_memory::segment_manager segment_manager_t; @@ -36,7 +74,7 @@ struct Shared { typedef boost::unique_lock scoped_exclusive_locker_t; typedef boost::shared_lock scoped_shared_locker_t; static segment_t * open_or_create_segment (const std::string &path, size_t size) { - return new segment_t(boost::interprocess::open_or_create, path.c_str(), size) ; + return new segment_t(boost::interprocess::open_or_create, path.c_str(), size) ; } static segment_t * open_segment (const std::string &path) { return new segment_t(boost::interprocess::open_only, path.c_str()) ;