Skip to content

Commit 301f3cc

Browse files
committed
Use std::atomic instead of the windows-only primitives for atomic operations in the song renderer
1 parent 4d7493e commit 301f3cc

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

WaveSabreCore/CMakeLists.txt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,16 @@ add_library(WaveSabreCore
5252
src/StateVariableFilter.cpp
5353
src/SynthDevice.cpp
5454
src/Thunder.cpp
55-
src/Twister.cpp)
55+
src/Twister.cpp
56+
)
57+
58+
if(WIN32)
59+
target_sources(WaveSabreCore PRIVATE
60+
)
61+
62+
target_link_libraries(WaveSabreCore PUBLIC Msacm32.lib)
63+
endif()
5664

57-
target_link_libraries(WaveSabreCore Msacm32.lib)
5865
target_include_directories(WaveSabreCore PUBLIC include)
5966

6067
if(MSVC)
@@ -69,4 +76,15 @@ if(MSVC)
6976
target_compile_options(WaveSabreCore PUBLIC
7077
$<$<CONFIG:MinSizeRel>:/Zc:sizedDealloc->)
7178
endif()
79+
else()
80+
# assuming GCC or clang for now
81+
82+
if(CMAKE_BUILD_TYPE EQUAL Debug)
83+
target_compile_options(WaveSabreCore PUBLIC -g -Og)
84+
else()
85+
#set_property(TARGET WaveSabreCore PROPERTY INTERPROCEDURAL_OPTIMIZATION ON)
86+
target_compile_options(WaveSabreCore
87+
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
88+
PRIVATE -ffast-math -march=nocona -ffunction-sections -fdata-sections -Wl,--gc-sections)
89+
endif()
7290
endif()

WaveSabrePlayerLib/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ target_link_libraries(WaveSabrePlayerLib
2222

2323
target_include_directories(WaveSabrePlayerLib PUBLIC include)
2424

25+
# we need C++11 for std::atomic
26+
set_property(TARGET WaveSabrePlayerLib PROPERTY CXX_STANDARD 11)
27+
2528
if(MSVC)
2629
target_compile_definitions(WaveSabrePlayerLib PRIVATE _CRT_SECURE_NO_WARNINGS)
2730
target_compile_options(WaveSabrePlayerLib PUBLIC

WaveSabrePlayerLib/include/WaveSabrePlayerLib/SongRenderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ namespace WaveSabrePlayerLib
172172
int bpm;
173173
int sampleRate;
174174
double length;
175-
175+
176176
int numDevices;
177177
WaveSabreCore::Device **devices;
178178

WaveSabrePlayerLib/src/SongRenderer.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
#include <WaveSabrePlayerLib/SongRenderer.h>
22

3+
#include <atomic>
4+
#include <stdlib.h>
5+
6+
#ifdef _MSC_VER
7+
void* __cdecl operator new[](size_t size)
8+
{
9+
return malloc(size);
10+
}
11+
void __cdecl operator delete[](void* ptr)
12+
{
13+
return free(size);
14+
}
15+
#endif
16+
317
using namespace WaveSabreCore;
418

519
namespace WaveSabrePlayerLib
@@ -195,7 +209,8 @@ namespace WaveSabrePlayerLib
195209

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

209-
if (!InterlockedDecrement(&renderThreadsRunning))
224+
// returns the value *before* the call
225+
if (std::atomic_fetch_sub((std::atomic_int *)&renderThreadsRunning, 1) == 1)
210226
SetEvent(renderDoneEvent);
211227

212228
return true;

0 commit comments

Comments
 (0)