From dd8692ac9d9f9fbe44538bf24cc9d94dc301a49a Mon Sep 17 00:00:00 2001
From: Aaron Cuevas Lopez <aacuelo@teleco.upv.es>
Date: Sun, 8 Mar 2015 21:48:33 +0100
Subject: [PATCH] Add mutex to Lfp display to avoid synchronization issues

---
 Source/Processors/LfpDisplayNode/LfpDisplayCanvas.cpp | 2 ++
 Source/Processors/LfpDisplayNode/LfpDisplayNode.cpp   | 2 ++
 Source/Processors/LfpDisplayNode/LfpDisplayNode.h     | 7 +++++++
 3 files changed, 11 insertions(+)

diff --git a/Source/Processors/LfpDisplayNode/LfpDisplayCanvas.cpp b/Source/Processors/LfpDisplayNode/LfpDisplayCanvas.cpp
index 0c4208765..03791626e 100755
--- a/Source/Processors/LfpDisplayNode/LfpDisplayCanvas.cpp
+++ b/Source/Processors/LfpDisplayNode/LfpDisplayCanvas.cpp
@@ -658,6 +658,8 @@ void LfpDisplayCanvas::updateScreenBuffer()
     // copy new samples from the displayBuffer into the screenBuffer
     int maxSamples = lfpDisplay->getWidth() - leftmargin;
 
+	ScopedLock displayLock(*processor->getMutex());
+
     for (int channel = 0; channel <= nChans; channel++) // pull one extra channel for event display
     {
 
diff --git a/Source/Processors/LfpDisplayNode/LfpDisplayNode.cpp b/Source/Processors/LfpDisplayNode/LfpDisplayNode.cpp
index 5092af286..c89c4611e 100755
--- a/Source/Processors/LfpDisplayNode/LfpDisplayNode.cpp
+++ b/Source/Processors/LfpDisplayNode/LfpDisplayNode.cpp
@@ -297,6 +297,8 @@ void LfpDisplayNode::process(AudioSampleBuffer& buffer, MidiBuffer& events)
 
     checkForEvents(events); // see if we got any TTL events
 
+	ScopedLock displayLock(displayMutex);
+
     for (int chan = 0; chan < buffer.getNumChannels(); chan++)
     {
          int samplesLeft = displayBuffer->getNumSamples() - displayBufferIndex[chan];
diff --git a/Source/Processors/LfpDisplayNode/LfpDisplayNode.h b/Source/Processors/LfpDisplayNode/LfpDisplayNode.h
index a475e19f3..18426bb7f 100755
--- a/Source/Processors/LfpDisplayNode/LfpDisplayNode.h
+++ b/Source/Processors/LfpDisplayNode/LfpDisplayNode.h
@@ -75,6 +75,11 @@ public:
         return displayBufferIndex[chan];
     }
 
+	CriticalSection* getMutex()
+	{
+		return &displayMutex;
+	}
+
 private:
 
     void initializeEventChannels();
@@ -99,6 +104,8 @@ private:
 
     bool resizeBuffer();
 
+	CriticalSection displayMutex;
+
     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LfpDisplayNode);
 
 };
-- 
GitLab