From 0f7123d33a1ab879191046b86d382318f35fd570 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Tue, 25 Sep 2018 11:53:52 -0400 Subject: [PATCH] more objectification for SafeTime --- libs/ardour/ardour/transport_master.h | 30 +++++++++++++++++++-------- libs/ardour/mtc_slave.cc | 25 +++------------------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/libs/ardour/ardour/transport_master.h b/libs/ardour/ardour/transport_master.h index da30abfae9..81d250dda9 100644 --- a/libs/ardour/ardour/transport_master.h +++ b/libs/ardour/ardour/transport_master.h @@ -26,9 +26,11 @@ #include #include +#include #include +#include "pbd/i18n.h" #include "pbd/properties.h" #include "pbd/signals.h" #include "pbd/stateful.h" @@ -305,15 +307,6 @@ struct LIBARDOUR_API SafeTime { , guard2 (other.guard2.load (boost::memory_order_acquire)) {} - SafeTime& operator= (SafeTime const & other) { - guard1 = other.guard1.load (boost::memory_order_acquire); - position = other.position; - timestamp = other.timestamp; - speed = other.speed; - guard2 = other.guard2.load (boost::memory_order_acquire); - return *this; - } - void update (samplepos_t p, samplepos_t t, double s) { guard1.fetch_add (1, boost::memory_order_acquire); position = p; @@ -321,6 +314,25 @@ struct LIBARDOUR_API SafeTime { speed = s; guard2.fetch_add (1, boost::memory_order_acquire); } + + void safe_read (SafeTime& dst) const { + int tries = 0; + + do { + if (tries == 10) { + std::cerr << X_("SafeTime: atomic read of current time failed, sleeping!") << std::endl; + Glib::usleep (20); + tries = 0; + } + dst.guard1.store (guard1.load (boost::memory_order_seq_cst), boost::memory_order_seq_cst); + dst.position = position; + dst.timestamp = timestamp; + dst.speed = speed; + dst.guard2.store (guard2.load (boost::memory_order_seq_cst), boost::memory_order_seq_cst); + tries++; + + } while (dst.guard1.load (boost::memory_order_seq_cst) != dst.guard2.load (boost::memory_order_seq_cst)); + } }; /** a helper class for any TransportMaster that receives its input via a MIDI diff --git a/libs/ardour/mtc_slave.cc b/libs/ardour/mtc_slave.cc index 7a6d6a6ed6..d110c2a32d 100644 --- a/libs/ardour/mtc_slave.cc +++ b/libs/ardour/mtc_slave.cc @@ -32,8 +32,6 @@ #include "ardour/session.h" #include "ardour/transport_master.h" -#include - #include "pbd/i18n.h" using namespace std; @@ -259,23 +257,6 @@ MTC_TransportMaster::handle_locate (const MIDI::byte* mmc_tc) update_mtc_time (mtc, true, 0); } -void -MTC_TransportMaster::read_current (SafeTime *st) const -{ - int tries = 0; - - do { - if (tries == 10) { - error << _("MTC Slave: atomic read of current time failed, sleeping!") << endmsg; - Glib::usleep (20); - tries = 0; - } - *st = current; - tries++; - - } while (st->guard1.load (boost::memory_order_acquire) != st->guard2.load (boost::memory_order_acquire)); -} - void MTC_TransportMaster::init_mtc_dll(samplepos_t tme, double qtr) { @@ -579,7 +560,7 @@ MTC_TransportMaster::speed_and_position (double& speed, samplepos_t& pos, sample return false; } - read_current (&last); + current.safe_read (last); DEBUG_TRACE (DEBUG::MTC, string_compose ("speed&pos: timestamp %1 speed %2 dir %4 now %5 last-in %6\n", last.timestamp, @@ -634,7 +615,7 @@ std::string MTC_TransportMaster::position_string() const { SafeTime last; - read_current (&last); + current.safe_read (last); if (last.timestamp == 0 || reset_pending) { return " --:--:--:--"; } @@ -650,7 +631,7 @@ MTC_TransportMaster::delta_string () const { char delta[80]; SafeTime last; - read_current (&last); + current.safe_read (last); delta[0] = '\0';