From b5c36c2c18c0d71d1fb8d9dd3da11b42f8f38b76 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Tue, 30 Jul 2024 19:34:23 +0000 Subject: [PATCH] urcu: add rcu_list_iteration_adapter MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Like its list_iteration_adapter counterpart, this utility allows the iteration over cds_list structures and provides ranged-for semantics. This utility differs in that it provides an RCU-protected iteration over the list. Change-Id: I810126ee5e099a6e6bb15509d00efa0ebfa56e73 Signed-off-by: Jérémie Galarneau --- src/common/urcu.hpp | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/common/urcu.hpp b/src/common/urcu.hpp index b59c2282c..6466fac43 100644 --- a/src/common/urcu.hpp +++ b/src/common/urcu.hpp @@ -366,6 +366,58 @@ protected: cds_list_head& _list; }; +template +class rcu_list_iteration_adapter : public list_iteration_adapter { +public: + /* Nested iterator class defines the iterator for rcu_list_iteration_adapter. */ + class iterator : public list_iteration_adapter::iterator { + /* Allow rcu_list_iteration_adapter to access private members of iterator. */ + friend rcu_list_iteration_adapter; + + public: + iterator(const iterator& other) = default; + iterator(iterator&& other) noexcept = default; + ~iterator() = default; + iterator& operator=(const iterator&) = delete; + iterator& operator=(iterator&&) noexcept = delete; + + /* Move to the next element in the hash table. */ + iterator& operator++() + { + this->_node = rcu_dereference(this->_node_contents.next); + this->_node_contents = *this->_node; + return *this; + } + + protected: + explicit iterator(const cds_list_head& node) : + list_iteration_adapter::iterator(node) + { + } + }; + + explicit rcu_list_iteration_adapter(cds_list_head& list) : + list_iteration_adapter(list) + { + } + + /* Return an iterator to the beginning of the hash table. */ + iterator begin() const noexcept + { + return iterator(*rcu_dereference(this->_list.next)); + } + + /* Return an iterator to the end of the hash table. */ + iterator end() const noexcept + { + return iterator(this->_list); + } + +protected: + /* RCU read lock held during the iteration. */ + const lttng::urcu::read_lock_guard read_lock; +}; + } /* namespace urcu */ } /* namespace lttng */ -- 2.34.1