From: Jérémie Galarneau Date: Tue, 30 Jul 2024 19:34:23 +0000 (+0000) Subject: urcu: add rcu_list_iteration_adapter X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=b5c36c2c18c0d71d1fb8d9dd3da11b42f8f38b76;p=lttng-tools.git urcu: add rcu_list_iteration_adapter 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 --- 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 */