From ed2507a688d8d2e3aa6825f90ce29192d244d233 Mon Sep 17 00:00:00 2001 From: Sugandha Gupta Date: Wed, 24 Apr 2019 14:31:40 -0700 Subject: liberio: Release context holder on destruction of last liberio xport This will make sure that the context holder for the liberio context is destroyed when the last liberio transport is destroyed, and not on termination of the program. --- host/lib/transport/liberio_zero_copy.cpp | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/host/lib/transport/liberio_zero_copy.cpp b/host/lib/transport/liberio_zero_copy.cpp index 989480382..15131910a 100644 --- a/host/lib/transport/liberio_zero_copy.cpp +++ b/host/lib/transport/liberio_zero_copy.cpp @@ -57,8 +57,6 @@ private: liberio_ctx* _ctx; }; -UHD_SINGLETON_FCN(liberio_context_holder, get_liberio_context_holder); - class liberio_zero_copy_msb : public virtual managed_send_buffer { public: @@ -139,7 +137,15 @@ public: UHD_ASSERT_THROW(xport_params.num_send_frames > 0); UHD_ASSERT_THROW(xport_params.num_recv_frames > 0); - liberio_ctx* ctx = get_liberio_context_holder().get(); + { + std::lock_guard _l(_context_lock); + if (!_ref_count) { + _context_holder = std::make_shared(); + } + + _ref_count++; + } + liberio_ctx* ctx = _context_holder->get(); /* we hold a reference, that we'd drop immediately after, * so no requirement to get another one here ... @@ -193,6 +199,13 @@ public: { liberio_chan_put(_tx_chan); liberio_chan_put(_rx_chan); + { + std::lock_guard _l(_context_lock); + _ref_count--; + if (!_ref_count) { + _context_holder.reset(); + } + } } managed_recv_buffer::sptr get_recv_buff(double timeout = 0.1) @@ -245,8 +258,16 @@ private: size_t _next_send_buff_index; std::mutex _rx_mutex; std::mutex _tx_mutex; + + static std::mutex _context_lock; + static size_t _ref_count; + static std::shared_ptr _context_holder; }; +std::mutex liberio_zero_copy_impl::_context_lock; +size_t liberio_zero_copy_impl::_ref_count = 0; +std::shared_ptr liberio_zero_copy_impl::_context_holder{}; + liberio_zero_copy::sptr liberio_zero_copy::make(const std::string& tx_path, const std::string& rx_path, const zero_copy_xport_params& default_buff_args) -- cgit v1.2.3