Skip to content

Commit

Permalink
Move RtClientStateSetter::mPendingClientStates to Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
cdi-ableton authored and fgo-ableton committed Feb 20, 2018
1 parent 85a42b9 commit ae14492
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions include/ableton/link/Controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class Controller
, mClientState(detail::initClientState(mSessionState))
, mLastIsPlayingForStartStopStateCallback(false)
, mRtClientState(detail::initRtClientState(mClientState))
, mHasPendingRtClientStates(false)
, mSessionPeerCounter(*this, std::move(peerCallback))
, mEnabled(false)
, mStartStopSyncEnabled(false)
Expand Down Expand Up @@ -246,7 +247,7 @@ class Controller
// block on them. If we can't access one or both because of concurrent modification
// we fall back to our cached version of the timeline and/or start stop state.

if (!mRtClientStateSetter.hasPendingClientStates())
if (!mHasPendingRtClientStates)
{
const auto now = mClock.micros();
if (now - mRtClientState.timelineTimestamp > detail::kLocalModGracePeriod)
Expand Down Expand Up @@ -305,6 +306,9 @@ class Controller
mRtClientState.startStopState, *newClientState.startStopState);
}

// This flag ensures that mRtClientState is only updated after all incoming
// client states were processed
mHasPendingRtClientStates = true;
// This will fail in case the Fifo in the RtClientStateSetter is full. This indicates
// a very high rate of calls to the setter. In this case we ignore one value because
// we expect the setter to be called again soon.
Expand Down Expand Up @@ -488,6 +492,7 @@ class Controller
}

handleClientState(clientState);
mHasPendingRtClientStates = false;
}

void joinSession(const Session& session)
Expand Down Expand Up @@ -554,15 +559,13 @@ class Controller

RtClientStateSetter(Controller& controller)
: mController(controller)
, mHasPendingClientStates(false)
, mCallbackDispatcher(
[this] { processPendingClientStates(); }, detail::kRtHandlerFallbackPeriod)
{
}

bool tryPush(const IncomingClientState clientState)
{
mHasPendingClientStates = true;
const auto success = mClientStateFifo.push(clientState);
if (success)
{
Expand All @@ -571,11 +574,6 @@ class Controller
return success;
}

bool hasPendingClientStates() const
{
return mHasPendingClientStates;
}

private:
IncomingClientState buildMergedPendingClientState()
{
Expand All @@ -598,10 +596,8 @@ class Controller
void processPendingClientStates()
{
const auto clientState = buildMergedPendingClientState();
mController.mIo->async([this, clientState]() {
mController.handleRtClientState(clientState);
mHasPendingClientStates = false;
});
mController.mIo->async(
[this, clientState]() { mController.handleRtClientState(clientState); });
}

Controller& mController;
Expand All @@ -610,7 +606,6 @@ class Controller
// per ms.
static const std::size_t kBufferSize = 16;
CircularFifo<IncomingClientState, kBufferSize> mClientStateFifo;
std::atomic<bool> mHasPendingClientStates;
CallbackDispatcher mCallbackDispatcher;
};

Expand Down Expand Up @@ -750,6 +745,7 @@ class Controller
bool mLastIsPlayingForStartStopStateCallback;

mutable RtClientState mRtClientState;
std::atomic<bool> mHasPendingRtClientStates;

SessionPeerCounter mSessionPeerCounter;

Expand Down

0 comments on commit ae14492

Please sign in to comment.