diff options
author | Martin Braun <martin.braun@ettus.com> | 2019-03-24 16:54:11 -0700 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2019-11-26 11:49:13 -0800 |
commit | efb1d5a4729ea892ea03b8d0265aae9e8fadfff1 (patch) | |
tree | 23cd4dbf85c635d381aaf7b0f6e9bd15eeca08fc /host/lib | |
parent | 44f9bca2c5b561fa3f5f08d0d616c91d2142cbf9 (diff) | |
download | uhd-efb1d5a4729ea892ea03b8d0265aae9e8fadfff1.tar.gz uhd-efb1d5a4729ea892ea03b8d0265aae9e8fadfff1.tar.bz2 uhd-efb1d5a4729ea892ea03b8d0265aae9e8fadfff1.zip |
rfnoc: Add properties, nodes, and accessors
Adds the following classes:
- uhd::rfnoc::node_t, the base class for RFNoC nodes
- uhd::rfnoc::node_accessor_t, a class to access private properties
- uhd::rfnoc::res_source_info, a struct that identifies where properties
come from
- uhd::rfnoc::property_t, and property_base_t (its parent)
- uhd::rfnoc::prop_accessor_t, a class to access properties
Add always dirty property (dirtifier).
Also adds unit tests for properties.
Diffstat (limited to 'host/lib')
-rw-r--r-- | host/lib/include/uhdlib/rfnoc/node_accessor.hpp | 84 | ||||
-rw-r--r-- | host/lib/include/uhdlib/rfnoc/prop_accessor.hpp | 96 | ||||
-rw-r--r-- | host/lib/rfnoc/CMakeLists.txt | 1 | ||||
-rw-r--r-- | host/lib/rfnoc/node.cpp | 119 |
4 files changed, 300 insertions, 0 deletions
diff --git a/host/lib/include/uhdlib/rfnoc/node_accessor.hpp b/host/lib/include/uhdlib/rfnoc/node_accessor.hpp new file mode 100644 index 000000000..26e6a5607 --- /dev/null +++ b/host/lib/include/uhdlib/rfnoc/node_accessor.hpp @@ -0,0 +1,84 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#ifndef INCLUDED_LIBUHD_NODE_ACCESSOR_HPP +#define INCLUDED_LIBUHD_NODE_ACCESSOR_HPP + +#include <uhd/rfnoc/node.hpp> +#include <functional> + +namespace uhd { namespace rfnoc { + +//! Special class which may access nodes +// +// For the sake of property resolution, we require access to certain private +// members of nodes. Instead of giving the entire graph +// access to everything, we create this accessor class which is not available +// in the public API. +class node_accessor_t +{ +public: + using prop_ptrs_t = node_t::prop_ptrs_t; + + /*! Initializes the properties of a node. See node_t::init_props() for + * details. + */ + void init_props(node_t* node) + { + node->init_props(); + } + + /*! Does a local resolution of properties on \p node. + * + * See node_t::resolve_props for details. + */ + void resolve_props(node_t* node) + { + node->resolve_props(); + } + + /*! Returns a filtered list of properties. + * + * The return list contains all properties that match a given predicate. + */ + template <typename PredicateType> + node_t::prop_ptrs_t filter_props(node_t* node, PredicateType&& predicate) + { + return node->filter_props(std::forward<PredicateType>(predicate)); + } + + /*! Mark all properties on this node as clean + * + * See node_t::clean_props() for details. + */ + void clean_props(node_t* node) + { + node->clean_props(); + } + + /*! Set a resolver callback for the node + * + * See node_t::set_resolve_all_callback() for details. + */ + void set_resolve_all_callback(node_t* node, node_t::resolve_callback_t&& resolver) + { + node->set_resolve_all_callback(std::move(resolver)); + } + + /*! Forward an edge property to \p dst_node + * + * See node_t::forward_edge_property() for details. + */ + void forward_edge_property(node_t* dst_node, property_base_t* incoming_prop) + { + dst_node->forward_edge_property(incoming_prop); + } +}; + + +}} /* namespace uhd::rfnoc */ + +#endif /* INCLUDED_LIBUHD_NODE_ACCESSOR_HPP */ diff --git a/host/lib/include/uhdlib/rfnoc/prop_accessor.hpp b/host/lib/include/uhdlib/rfnoc/prop_accessor.hpp new file mode 100644 index 000000000..a62f54620 --- /dev/null +++ b/host/lib/include/uhdlib/rfnoc/prop_accessor.hpp @@ -0,0 +1,96 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#ifndef INCLUDED_LIBUHD_PROP_ACCESSOR_HPP +#define INCLUDED_LIBUHD_PROP_ACCESSOR_HPP + +#include <uhd/rfnoc/property.hpp> +#include <uhd/utils/scope_exit.hpp> +#include <functional> + +namespace uhd { namespace rfnoc { + +//! Special class which may access properties +// +// For the sake of property resolution, we require access to certain private +// members of properties. Instead of giving the entire graph access to +// everything, we create this accessor class which is not available +// in the public API. +class prop_accessor_t +{ +public: + //! Clear the dirty bit on a property + void mark_clean(property_base_t& prop) + { + prop.mark_clean(); + } + + //! Set the access mode on a property + void set_access(property_base_t& prop, const property_base_t::access_t access) + { + prop._access_mode = access; + } + + //! Set the access mode on a property + void set_access(property_base_t* prop, const property_base_t::access_t access) + { + prop->_access_mode = access; + } + + //! RAII-Style access mode setter + // + // This will return an object which will set the access mode on a property + // only for the duration of its own lifetime. Use this in situations where + // you want to guarantee a certain read-write mode of a property, even when + // an exception is thrown. + // + // \param prop A reference to the property + // \param access The temporary access mode which will be set as long as the + // scope_exit object is alive + // \param default_access The access mode which will be set once the + // scope_exit object will be destroyed + uhd::utils::scope_exit::uptr get_scoped_prop_access(property_base_t& prop, + property_base_t::access_t access, + property_base_t::access_t default_access = property_base_t::RO) + { + prop._access_mode = access; + return uhd::utils::scope_exit::make( + [&prop, default_access]() { prop._access_mode = default_access; }); + } + + /*! Forward the value from \p source to \p dst + * + * Note: This method will grant temporary write access to the destination + * property! + * If \p safe is set to true, it'll only allow RWLOCKED forwarding, i.e., + * the new value cannot be different. + * + * \throws uhd::type_error if types mismatch + */ + template <bool safe> + void forward(property_base_t* source, property_base_t* dst) + { + const auto w_access_type = (safe && dst->is_dirty()) ? property_base_t::RWLOCKED + : property_base_t::RW; + auto read_access = get_scoped_prop_access( + *source, property_base_t::RO, source->get_access_mode()); + auto write_access = + get_scoped_prop_access(*dst, w_access_type, dst->get_access_mode()); + source->forward(dst); + } + + /*! Returns true if \p source and \p dst are of the same type + */ + bool are_compatible(property_base_t* source, property_base_t* dst) + { + return source->is_type_equal(dst); + } +}; + + +}} /* namespace uhd::rfnoc */ + +#endif /* INCLUDED_LIBUHD_PROP_ACCESSOR_HPP */ diff --git a/host/lib/rfnoc/CMakeLists.txt b/host/lib/rfnoc/CMakeLists.txt index af58e8953..dce1f286b 100644 --- a/host/lib/rfnoc/CMakeLists.txt +++ b/host/lib/rfnoc/CMakeLists.txt @@ -21,6 +21,7 @@ LIBUHD_APPEND_SOURCES( ${CMAKE_CURRENT_SOURCE_DIR}/graph_impl.cpp ${CMAKE_CURRENT_SOURCE_DIR}/legacy_compat.cpp ${CMAKE_CURRENT_SOURCE_DIR}/node_ctrl_base.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/node.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rate_node_ctrl.cpp ${CMAKE_CURRENT_SOURCE_DIR}/rx_stream_terminator.cpp ${CMAKE_CURRENT_SOURCE_DIR}/scalar_node_ctrl.cpp diff --git a/host/lib/rfnoc/node.cpp b/host/lib/rfnoc/node.cpp new file mode 100644 index 000000000..68ba5e283 --- /dev/null +++ b/host/lib/rfnoc/node.cpp @@ -0,0 +1,119 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include <uhd/rfnoc/node.hpp> +#include <uhdlib/rfnoc/prop_accessor.hpp> +#include <boost/format.hpp> + +using namespace uhd::rfnoc; + + +std::string node_t::get_unique_id() const +{ + // TODO return something better + return str(boost::format("%08X") % this); +} + +std::vector<std::string> node_t::get_property_ids() const +{ + std::lock_guard<std::mutex> _l(_prop_mutex); + if (_props.count(res_source_info::USER) == 0) { + return {}; + } + + auto& user_props = _props.at(res_source_info::USER); + // TODO use a range here, we're not savages + std::vector<std::string> return_value(user_props.size()); + for (size_t i = 0; i < user_props.size(); ++i) { + return_value[i] = user_props[i]->get_id(); + } + + return return_value; +} + +/*** Protected methods *******************************************************/ +void node_t::register_property(property_base_t* prop) +{ + std::lock_guard<std::mutex> _l(_prop_mutex); + + const auto src_type = prop->get_src_info().type; + auto prop_already_registered = [prop](const property_base_t* existing_prop) { + return (prop->get_src_info() == existing_prop->get_src_info() + && prop->get_id() == existing_prop->get_id()) + || (prop == existing_prop); + }; + + // If the map is empty for this source type, create an empty vector + if (_props.count(src_type) == 0) { + _props[src_type] = {}; + } + + // Now go and make sure no one has registered this property before + auto& props = _props[src_type]; + for (const auto& existing_prop : props) { + if (prop_already_registered(existing_prop)) { + throw uhd::runtime_error("Attempting to double-register prop"); + } + } + + _props[src_type].push_back(prop); +} + +void node_t::add_property_resolver(std::set<property_base_t*>&& inputs, + std::set<property_base_t*>&& outputs, + resolver_fn_t&& resolver_fn) +{ + std::lock_guard<std::mutex> _l(_prop_mutex); + + // Sanity check: All inputs and outputs must be registered properties + auto prop_is_registered = [this](property_base_t* prop) -> bool { + return bool(this->_find_property(prop->get_src_info(), prop->get_id())); + }; + for (const auto& prop : inputs) { + if (!prop_is_registered(prop)) { + throw uhd::runtime_error( + std::string("Cannot add property resolver, input property ") + + prop->get_id() + " is not registered!"); + } + } + for (const auto& prop : outputs) { + if (!prop_is_registered(prop)) { + throw uhd::runtime_error( + std::string("Cannot add property resolver, output property ") + + prop->get_id() + " is not registered!"); + } + } + + // All good, we can store it + _prop_resolvers.push_back(std::make_tuple( + std::forward<std::set<property_base_t*>>(inputs), + std::forward<std::set<property_base_t*>>(outputs), + std::forward<resolver_fn_t>(resolver_fn))); +} + +/*** Private methods *********************************************************/ +property_base_t* node_t::_find_property(res_source_info src_info, const std::string& id) const +{ + for (const auto& type_prop_pair : _props) { + if (type_prop_pair.first != src_info.type) { + continue; + } + for (const auto& prop : type_prop_pair.second) { + if (prop->get_id() == id && prop->get_src_info() == src_info) { + return prop; + } + } + } + + return nullptr; +} + +uhd::utils::scope_exit::uptr node_t::_request_property_access( + property_base_t* prop, property_base_t::access_t access) const +{ + return prop_accessor_t{}.get_scoped_prop_access(*prop, access); +} + |