13
0

more objectification for SafeTime

This commit is contained in:
Paul Davis 2018-09-25 11:53:52 -04:00
parent b6aefaf100
commit 0f7123d33a
2 changed files with 24 additions and 31 deletions

View File

@ -26,9 +26,11 @@
#include <boost/atomic.hpp>
#include <glibmm/threads.h>
#include <glibmm/timer.h>
#include <ltc.h>
#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

View File

@ -32,8 +32,6 @@
#include "ardour/session.h"
#include "ardour/transport_master.h"
#include <glibmm/timer.h>
#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';