Skip to content

Commit

Permalink
Use std::atomic instead of the windows-only primitives for atomic ope…
Browse files Browse the repository at this point in the history
…rations in the song renderer
  • Loading branch information
PoroCYon committed Sep 9, 2020
1 parent 4d7493e commit 67cb52c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
22 changes: 20 additions & 2 deletions WaveSabreCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,16 @@ add_library(WaveSabreCore
src/StateVariableFilter.cpp
src/SynthDevice.cpp
src/Thunder.cpp
src/Twister.cpp)
src/Twister.cpp
)

if(WIN32)
target_sources(WaveSabreCore PRIVATE
)

target_link_libraries(WaveSabreCore PUBLIC Msacm32.lib)
endif()

target_link_libraries(WaveSabreCore Msacm32.lib)
target_include_directories(WaveSabreCore PUBLIC include)

if(MSVC)
Expand All @@ -69,4 +76,15 @@ if(MSVC)
target_compile_options(WaveSabreCore PUBLIC
$<$<CONFIG:MinSizeRel>:/Zc:sizedDealloc->)
endif()
else()
# assuming GCC or clang for now

if(CMAKE_BUILD_TYPE EQUAL Debug)
target_compile_options(WaveSabreCore PUBLIC -g -Og)
else()
#set_property(TARGET WaveSabreCore PROPERTY INTERPROCEDURAL_OPTIMIZATION ON)
target_compile_options(WaveSabreCore
PUBLIC -O2 -fno-exceptions -fno-rtti -fno-stack-protector -fno-stack-check -fno-unwind-tables -fno-asynchronous-unwind-tables -fomit-frame-pointer -fno-threadsafe-statics
PRIVATE -ffast-math -march=nocona -ffunction-sections -fdata-sections -Wl,--gc-sections)
endif()
endif()
3 changes: 3 additions & 0 deletions WaveSabrePlayerLib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ target_link_libraries(WaveSabrePlayerLib

target_include_directories(WaveSabrePlayerLib PUBLIC include)

# we need C++11 for std::atomic
set_property(TARGET WaveSabrePlayerLib PROPERTY CXX_STANDARD 11)

if(MSVC)
target_compile_definitions(WaveSabrePlayerLib PRIVATE _CRT_SECURE_NO_WARNINGS)
target_compile_options(WaveSabrePlayerLib PUBLIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ namespace WaveSabrePlayerLib
int bpm;
int sampleRate;
double length;

int numDevices;
WaveSabreCore::Device **devices;

Expand Down
20 changes: 18 additions & 2 deletions WaveSabrePlayerLib/src/SongRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#include <WaveSabrePlayerLib/SongRenderer.h>

#include <atomic>
#include <stdlib.h>

#ifdef _MSC_VER
void* __cdecl operator new[](size_t size)
{
return malloc(size);
}
void __cdecl operator delete[](void* ptr)
{
return free(ptr);
}
#endif

using namespace WaveSabreCore;

namespace WaveSabrePlayerLib
Expand Down Expand Up @@ -195,7 +209,8 @@ namespace WaveSabrePlayerLib

// We have a free track that we can work on, yay!
// Let's try to mark it so that no other thread takes it
if ((TrackRenderState)InterlockedCompareExchange((unsigned int *)&trackRenderStates[i], (unsigned int)TrackRenderState::Rendering, (unsigned int)TrackRenderState::Idle) == TrackRenderState::Idle)
int xv = (int)TrackRenderState::Idle;
if (std::atomic_compare_exchange_strong((std::atomic_int *)&trackRenderStates[i], &xv, (int)TrackRenderState::Rendering))
{
// We marked it successfully, so now we'll do the work
tracks[i]->Run(renderThreadNumFloatSamples);
Expand All @@ -206,7 +221,8 @@ namespace WaveSabrePlayerLib
}
}

if (!InterlockedDecrement(&renderThreadsRunning))
// returns the value *before* the call
if (std::atomic_fetch_sub((std::atomic_int *)&renderThreadsRunning, 1) == 1)
SetEvent(renderDoneEvent);

return true;
Expand Down

0 comments on commit 67cb52c

Please sign in to comment.