Skip to content

Commit

Permalink
Using a shared lock for SHM manipulation.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbevin committed Dec 28, 2016
1 parent 512a55f commit de3c61a
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 38 deletions.
6 changes: 6 additions & 0 deletions Builds/LinuxMakefile/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ OBJECTS := \
$(JUCE_OBJDIR)/AudioMasterProcess_baed3740.o \
$(JUCE_OBJDIR)/AudioProcessMessageUtils_9db78a0c.o \
$(JUCE_OBJDIR)/AudioSlaveProcess_3d06ee2f.o \
$(JUCE_OBJDIR)/SharedLock_3f733879.o \
$(JUCE_OBJDIR)/SharedMemory_b0aa330f.o \
$(JUCE_OBJDIR)/MainContentComponent_52535d99.o \
$(JUCE_OBJDIR)/MainWindow_5f9c9b05.o \
Expand Down Expand Up @@ -129,6 +130,11 @@ $(JUCE_OBJDIR)/AudioSlaveProcess_3d06ee2f.o: ../../Source/Process/AudioSlaveProc
@echo "Compiling AudioSlaveProcess.cpp"
@$(CXX) $(JUCE_CXXFLAGS) -o "$@" -c "$<"

$(JUCE_OBJDIR)/SharedLock_3f733879.o: ../../Source/Process/SharedLock.cpp
-@mkdir -p $(JUCE_OBJDIR)
@echo "Compiling SharedLock.cpp"
@$(CXX) $(JUCE_CXXFLAGS) -o "$@" -c "$<"

$(JUCE_OBJDIR)/SharedMemory_b0aa330f.o: ../../Source/Process/SharedMemory.cpp
-@mkdir -p $(JUCE_OBJDIR)
@echo "Compiling SharedMemory.cpp"
Expand Down
8 changes: 6 additions & 2 deletions Builds/MacOSX/HEELP.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
97B2E4AD2F74E6A606CAB5B7 = {isa = PBXBuildFile; fileRef = 39A74836EA636F2D72E4AC5B; };
3FCF1A1AADECCC7D5508B6B4 = {isa = PBXBuildFile; fileRef = 63C5A8E84B3C16995B15B58E; };
0D57467A365DB5F7A9ACFA0A = {isa = PBXBuildFile; fileRef = F5C69F4A41690CA0D8CCE421; };
B200C6218D63949DC0985BE6 = {isa = PBXBuildFile; fileRef = F671CBD74C9FA0C81B23BD48; };
12D3648604B9BCB0450B6440 = {isa = PBXBuildFile; fileRef = E4A90A260D7E2D509EADFCDB; };
189C979F354DB8919BE42084 = {isa = PBXBuildFile; fileRef = 8D40196CFDA0B1258FC61E8B; };
6209081E9DF9FBA0A2BA8F42 = {isa = PBXBuildFile; fileRef = 4A41DEC831CB153B9E74C55C; };
Expand Down Expand Up @@ -219,6 +220,7 @@
29B6CB1898611E04BEA44EAC = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = "juce_gui_extra.mm"; path = "../../JuceLibraryCode/juce_gui_extra.mm"; sourceTree = "SOURCE_ROOT"; };
29FE29A179EFD71293D3998F = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ChildAudioComponent.h; path = ../../Source/Audio/ChildAudioComponent.h; sourceTree = "SOURCE_ROOT"; };
29FE3A057547CDFB7F4F918B = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_DirectoryContentsDisplayComponent.h"; path = "../../JuceLibraryCode/modules/juce_gui_basics/filebrowser/juce_DirectoryContentsDisplayComponent.h"; sourceTree = "SOURCE_ROOT"; };
2A1AA0E0D8B0D2169E145621 = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SharedLock.h; path = ../../Source/Process/SharedLock.h; sourceTree = "SOURCE_ROOT"; };
2A1C1DF1812C99B78366AB6D = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_URL.h"; path = "../../JuceLibraryCode/modules/juce_core/network/juce_URL.h"; sourceTree = "SOURCE_ROOT"; };
2A377B051421E2E0D7AF6571 = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = "juce_linux_Windowing.cpp"; path = "../../JuceLibraryCode/modules/juce_gui_basics/native/juce_linux_Windowing.cpp"; sourceTree = "SOURCE_ROOT"; };
2A3EF657FB5E28929D95CED2 = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = "juce_android_SystemStats.cpp"; path = "../../JuceLibraryCode/modules/juce_core/native/juce_android_SystemStats.cpp"; sourceTree = "SOURCE_ROOT"; };
Expand Down Expand Up @@ -564,7 +566,6 @@
6CC8C3A69D91CFFC3B932C05 = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; name = res0.c; path = "../../JuceLibraryCode/modules/juce_audio_formats/codecs/oggvorbis/libvorbis-1.3.2/lib/res0.c"; sourceTree = "SOURCE_ROOT"; };
6D114D421F6D31BEBBF0AF89 = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = jdct.h; path = "../../JuceLibraryCode/modules/juce_graphics/image_formats/jpglib/jdct.h"; sourceTree = "SOURCE_ROOT"; };
6D1694DB3CCAE8FA3BC68449 = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_ArrayAllocationBase.h"; path = "../../JuceLibraryCode/modules/juce_core/containers/juce_ArrayAllocationBase.h"; sourceTree = "SOURCE_ROOT"; };
6D2622B8298914054DA2B89F = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ChildAudioState.h; path = ../../Source/Audio/ChildAudioState.h; sourceTree = "SOURCE_ROOT"; };
6D64504C49E82F6D21FD4374 = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; name = transupp.c; path = "../../JuceLibraryCode/modules/juce_graphics/image_formats/jpglib/transupp.c"; sourceTree = "SOURCE_ROOT"; };
6D8EDEA185FC3A839CB485B1 = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_JSON.h"; path = "../../JuceLibraryCode/modules/juce_core/javascript/juce_JSON.h"; sourceTree = "SOURCE_ROOT"; };
6DAA735D6CEA52BD871327D6 = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_FillType.h"; path = "../../JuceLibraryCode/modules/juce_graphics/colour/juce_FillType.h"; sourceTree = "SOURCE_ROOT"; };
Expand Down Expand Up @@ -1186,6 +1187,7 @@
F62CD6F19DB509E4549E71D9 = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_audio_processors.h"; path = "../../../../JUCE/modules/juce_audio_processors/juce_audio_processors.h"; sourceTree = "SOURCE_ROOT"; };
F6321B9366DE902801B33F1A = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = assert.h; path = "../../JuceLibraryCode/modules/juce_audio_formats/codecs/flac/assert.h"; sourceTree = "SOURCE_ROOT"; };
F645DD4B10239F406F7731F8 = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = "juce_FileBrowserComponent.cpp"; path = "../../JuceLibraryCode/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.cpp"; sourceTree = "SOURCE_ROOT"; };
F671CBD74C9FA0C81B23BD48 = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = SharedLock.cpp; path = ../../Source/Process/SharedLock.cpp; sourceTree = "SOURCE_ROOT"; };
F68931B0773FA9565A07180D = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = "juce_ios_UIViewComponentPeer.mm"; path = "../../JuceLibraryCode/modules/juce_gui_basics/native/juce_ios_UIViewComponentPeer.mm"; sourceTree = "SOURCE_ROOT"; };
F71E791057326A686E1986A8 = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_BufferingAudioFormatReader.h"; path = "../../JuceLibraryCode/modules/juce_audio_formats/format/juce_BufferingAudioFormatReader.h"; sourceTree = "SOURCE_ROOT"; };
F7327F239559105C5EA44B65 = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; name = jcapistd.c; path = "../../JuceLibraryCode/modules/juce_graphics/image_formats/jpglib/jcapistd.c"; sourceTree = "SOURCE_ROOT"; };
Expand Down Expand Up @@ -1235,7 +1237,6 @@
6AAE550FF5704C292260D57F = {isa = PBXGroup; children = (
A825A59EFF7D169224376501,
29FE29A179EFD71293D3998F,
6D2622B8298914054DA2B89F,
8A921870D83F6AD11489714F,
F3BCF1C7A3F7F32324ABCBF2, ); name = Audio; sourceTree = "<group>"; };
C3A1D42038074136DA4EF28C = {isa = PBXGroup; children = (
Expand All @@ -1246,6 +1247,8 @@
C2DE9E5909173ED752BD9D19,
F5C69F4A41690CA0D8CCE421,
C911DCB179478C668342267A,
F671CBD74C9FA0C81B23BD48,
2A1AA0E0D8B0D2169E145621,
E4A90A260D7E2D509EADFCDB,
470401B3A251927B1A79EB5D, ); name = Process; sourceTree = "<group>"; };
FE999A2BBD5DF670BA1353B2 = {isa = PBXGroup; children = (
Expand Down Expand Up @@ -2750,6 +2753,7 @@
97B2E4AD2F74E6A606CAB5B7,
3FCF1A1AADECCC7D5508B6B4,
0D57467A365DB5F7A9ACFA0A,
B200C6218D63949DC0985BE6,
12D3648604B9BCB0450B6440,
189C979F354DB8919BE42084,
6209081E9DF9FBA0A2BA8F42,
Expand Down
8 changes: 3 additions & 5 deletions Builds/VisualStudio2015/HEELP.sln
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HEELP", "HEELP.vcxproj", "{740F5CCE-043F-C922-BF4F-39E51C1A4D2B}"
Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2015
Project("{A3130430-CF17-8F70-DAA2-38EB0E340E61}") = "HEELP", "HEELP.vcxproj", "{740F5CCE-043F-C922-BF4F-39E51C1A4D2B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
3 changes: 2 additions & 1 deletion Builds/VisualStudio2015/HEELP.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<ClCompile Include="..\..\Source\Process\AudioMasterProcess.cpp"/>
<ClCompile Include="..\..\Source\Process\AudioProcessMessageUtils.cpp"/>
<ClCompile Include="..\..\Source\Process\AudioSlaveProcess.cpp"/>
<ClCompile Include="..\..\Source\Process\SharedLock.cpp"/>
<ClCompile Include="..\..\Source\Process\SharedMemory.cpp"/>
<ClCompile Include="..\..\Source\UI\MainContentComponent.cpp"/>
<ClCompile Include="..\..\Source\UI\MainWindow.cpp"/>
Expand Down Expand Up @@ -1675,12 +1676,12 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\Source\Audio\ChildAudioComponent.h"/>
<ClInclude Include="..\..\Source\Audio\ChildAudioState.h"/>
<ClInclude Include="..\..\Source\Audio\MainAudioComponent.h"/>
<ClInclude Include="..\..\Source\Process\AudioMasterProcess.h"/>
<ClInclude Include="..\..\Source\Process\AudioProcessMessageTypes.h"/>
<ClInclude Include="..\..\Source\Process\AudioProcessMessageUtils.h"/>
<ClInclude Include="..\..\Source\Process\AudioSlaveProcess.h"/>
<ClInclude Include="..\..\Source\Process\SharedLock.h"/>
<ClInclude Include="..\..\Source\Process\SharedMemory.h"/>
<ClInclude Include="..\..\Source\UI\MainContentComponent.h"/>
<ClInclude Include="..\..\Source\UI\MainWindow.h"/>
Expand Down
9 changes: 6 additions & 3 deletions Builds/VisualStudio2015/HEELP.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@
<ClCompile Include="..\..\Source\Process\AudioSlaveProcess.cpp">
<Filter>HEELP\Source\Process</Filter>
</ClCompile>
<ClCompile Include="..\..\Source\Process\SharedLock.cpp">
<Filter>HEELP\Source\Process</Filter>
</ClCompile>
<ClCompile Include="..\..\Source\Process\SharedMemory.cpp">
<Filter>HEELP\Source\Process</Filter>
</ClCompile>
Expand Down Expand Up @@ -2055,9 +2058,6 @@
<ClInclude Include="..\..\Source\Audio\ChildAudioComponent.h">
<Filter>HEELP\Source\Audio</Filter>
</ClInclude>
<ClInclude Include="..\..\Source\Audio\ChildAudioState.h">
<Filter>HEELP\Source\Audio</Filter>
</ClInclude>
<ClInclude Include="..\..\Source\Audio\MainAudioComponent.h">
<Filter>HEELP\Source\Audio</Filter>
</ClInclude>
Expand All @@ -2073,6 +2073,9 @@
<ClInclude Include="..\..\Source\Process\AudioSlaveProcess.h">
<Filter>HEELP\Source\Process</Filter>
</ClInclude>
<ClInclude Include="..\..\Source\Process\SharedLock.h">
<Filter>HEELP\Source\Process</Filter>
</ClInclude>
<ClInclude Include="..\..\Source\Process\SharedMemory.h">
<Filter>HEELP\Source\Process</Filter>
</ClInclude>
Expand Down
4 changes: 2 additions & 2 deletions HEELP.jucer
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
file="Source/Audio/ChildAudioComponent.cpp"/>
<FILE id="XMRW5G" name="ChildAudioComponent.h" compile="0" resource="0"
file="Source/Audio/ChildAudioComponent.h"/>
<FILE id="V2QvEt" name="ChildAudioState.h" compile="0" resource="0"
file="Source/Audio/ChildAudioState.h"/>
<FILE id="m7e8NN" name="MainAudioComponent.cpp" compile="1" resource="0"
file="Source/Audio/MainAudioComponent.cpp"/>
<FILE id="zdmTwe" name="MainAudioComponent.h" compile="0" resource="0"
Expand All @@ -33,6 +31,8 @@
file="Source/Process/AudioSlaveProcess.cpp"/>
<FILE id="rkxh2s" name="AudioSlaveProcess.h" compile="0" resource="0"
file="Source/Process/AudioSlaveProcess.h"/>
<FILE id="AoAjGU" name="SharedLock.cpp" compile="1" resource="0" file="Source/Process/SharedLock.cpp"/>
<FILE id="rZyGXt" name="SharedLock.h" compile="0" resource="0" file="Source/Process/SharedLock.h"/>
<FILE id="NjHDr2" name="SharedMemory.cpp" compile="1" resource="0"
file="Source/Process/SharedMemory.cpp"/>
<FILE id="tUiYSf" name="SharedMemory.h" compile="0" resource="0" file="Source/Process/SharedMemory.h"/>
Expand Down
32 changes: 21 additions & 11 deletions Source/Audio/ChildAudioComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
#include "../HeelpApplication.h"
#include "../Utils.h"
#include "../Process/AudioProcessMessageTypes.h"
#include "ChildAudioState.h"
#include "../Process/SharedLock.h"

using namespace heelp;

struct ChildAudioComponent::Pimpl : public AudioSource
{
Pimpl(int childId, SharedMemory* shm) : childId_(childId), shm_(shm), sharedAudioBuffer_(nullptr)
Pimpl(int childId, SharedMemory* shm) : childId_(childId), shm_(shm), sharedAudioBuffer_((float*)shm_->getShmAddress()), lock_(nullptr)
{
state_ = (ChildAudioState*)shm_->getShmAddress();
sharedAudioBuffer_ = (float*)&(shm_->getShmAddress()[sizeof(ChildAudioState)]);
}

~Pimpl()
Expand Down Expand Up @@ -70,6 +68,11 @@ struct ChildAudioComponent::Pimpl : public AudioSource

void prepareToPlay(int samplesPerBlockExpected, double sampleRate)
{
if (lock_ == nullptr)
{
lock_ = SharedLock::openForChild(childId_);
}

LOG("Preparing to play audio..." << newLine <<
" samplesPerBlockExpected = " << samplesPerBlockExpected << newLine <<
" sampleRate = " << sampleRate);
Expand All @@ -83,7 +86,7 @@ struct ChildAudioComponent::Pimpl : public AudioSource
static double currentAngle = 0.0;
static double level = 0.5;

double cyclesPerSecond = MidiMessage::getMidiNoteInHertz(40 + childId_ * 12);
double cyclesPerSecond = MidiMessage::getMidiNoteInHertz(52 + childId_ * 2);

AudioDeviceManager::AudioDeviceSetup audioSetup;
deviceManager_.getAudioDeviceSetup(audioSetup);
Expand All @@ -109,9 +112,10 @@ struct ChildAudioComponent::Pimpl : public AudioSource
currentAngle += angleDelta;
++startSample;
}
// state_->mutex_.enter();

lock_->enter();
memcpy(&sharedAudioBuffer_[0], &sharedAudioBuffer_[totalBufferSize], totalBufferSize * sizeof(float));
// state_->mutex_.exit();
lock_->exit();
}

// artificial load generator
Expand All @@ -124,12 +128,18 @@ struct ChildAudioComponent::Pimpl : public AudioSource
void releaseResources()
{
LOG("Releasing audio resources");
if (lock_ != nullptr)
{
delete lock_;
lock_ = nullptr;
}
}

int childId_;
SharedMemory* shm_;
ChildAudioState* state_;
float* sharedAudioBuffer_;
const int childId_;
SharedMemory* const shm_;
float* const sharedAudioBuffer_;

SharedLock* lock_;

AudioDeviceManager deviceManager_;
AudioSourcePlayer audioSourcePlayer_;
Expand Down
11 changes: 6 additions & 5 deletions Source/Audio/MainAudioComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "../HeelpApplication.h"
#include "../Utils.h"
#include "ChildAudioState.h"
#include "../Process/SharedLock.h"

#include <map>

Expand All @@ -29,8 +29,8 @@ namespace
{
struct ChildInfo
{
SharedLock* const lock_;
SharedMemory* const shm_;
ChildAudioState* const state_;
float* const sharedAudioBuffer_;
float* const localAudioBuffer_;
};
Expand Down Expand Up @@ -66,7 +66,7 @@ struct MainAudioComponent::Pimpl : public AudioAppComponent

void registerChild(int childId, SharedMemory* shm, float* localBuffer)
{
ChildInfo childInfo = {shm, (ChildAudioState*)shm->getShmAddress(), (float*)(shm->getShmAddress() + sizeof(ChildAudioState)), localBuffer};
ChildInfo childInfo = {SharedLock::createForChild(childId), shm, (float*)shm->getShmAddress(), localBuffer};
ScopedWriteLock g(childInfosLock_);
childInfos_.insert(std::pair<int, ChildInfo>{childId, childInfo});
}
Expand All @@ -77,6 +77,7 @@ struct MainAudioComponent::Pimpl : public AudioAppComponent
std::map<int, ChildInfo>::iterator it = childInfos_.find(childId);
if (it != childInfos_.end())
{
delete it->second.lock_;
childInfos_.erase(it);
}
}
Expand All @@ -99,9 +100,9 @@ struct MainAudioComponent::Pimpl : public AudioAppComponent
ScopedReadLock g(childInfosLock_);
for (auto it = childInfos_.begin(); it != childInfos_.end(); ++it)
{
// it->second.state_->mutex_.enter();
it->second.lock_->enter();
memcpy(it->second.localAudioBuffer_, it->second.sharedAudioBuffer_, NUM_AUDIO_CHANNELS * bufferToFill.numSamples * sizeof(float));
// it->second.state_->mutex_.exit();
it->second.lock_->exit();
}
}

Expand Down
5 changes: 2 additions & 3 deletions Source/HeelpMainApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "HeelpChildApplication.h"
#include "HeelpLogger.h"
#include "Utils.h"
#include "Audio/ChildAudioState.h"
#include "Audio/MainAudioComponent.h"
#include "Process/AudioMasterProcess.h"
#include "Process/AudioProcessMessageTypes.h"
Expand Down Expand Up @@ -114,7 +113,7 @@ struct HeelpMainApplication::Pimpl : public ChangeListener
if (dm.getCurrentDeviceTypeObject() && dm.getCurrentAudioDevice())
{
// TODO : these should become proper channels, just generating four to test with now
for (int childId = 1; childId <= 4; ++childId)
for (int childId = 1; childId <= 8; ++childId)
{
launchChildProcess(childId);
}
Expand All @@ -137,7 +136,7 @@ struct HeelpMainApplication::Pimpl : public ChangeListener

int bufferSizeBytes = NUM_AUDIO_CHANNELS * setup.bufferSize * sizeof(float);
float* localBuffer = (float*)malloc(bufferSizeBytes);
SharedMemory* shm = SharedMemory::createWithSize(sizeof(ChildAudioState) + NUM_BUFFERS * bufferSizeBytes);
SharedMemory* shm = SharedMemory::createWithSize(NUM_BUFFERS * bufferSizeBytes);
audio_->registerChild(childId, shm, localBuffer);

AudioMasterProcess* masterProcess = new AudioMasterProcess(parent_, childId);
Expand Down
Loading

0 comments on commit de3c61a

Please sign in to comment.