Skip to content

Commit 92b1f0d

Browse files
authored
Assorted set of thread sanitizer fixes and tests (#76)
1 parent a5991c6 commit 92b1f0d

File tree

25 files changed

+1241
-106
lines changed

25 files changed

+1241
-106
lines changed

modules/yup_audio_basics/midi/yup_MidiFile.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,22 @@ void MidiFile::setTicksPerQuarterNote (int ticks) noexcept
340340

341341
void MidiFile::setSmpteTimeFormat (int framesPerSecond, int subframeResolution) noexcept
342342
{
343-
timeFormat = (short) (((-framesPerSecond) << 8) | subframeResolution);
343+
switch (framesPerSecond)
344+
{
345+
case 24:
346+
case 25:
347+
case 29:
348+
case 30:
349+
break;
350+
351+
default:
352+
framesPerSecond = 25;
353+
break;
354+
}
355+
356+
const int8 smpteByte = static_cast<int8> (-framesPerSecond);
357+
358+
timeFormat = static_cast<int16> ((static_cast<uint8> (smpteByte) << 8) | static_cast<uint8> (subframeResolution));
344359
}
345360

346361
//==============================================================================

modules/yup_audio_devices/native/yup_CoreAudio_mac.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ struct CoreAudioClasses
10551055

10561056
CoreAudioIODevice& owner;
10571057
int bitDepth = 32;
1058-
int xruns = 0;
1058+
std::atomic<int> xruns { 0 };
10591059
Array<double> sampleRates;
10601060
Array<int> bufferSizes;
10611061
AudioDeviceID deviceID;
@@ -1158,12 +1158,12 @@ struct CoreAudioClasses
11581158
{
11591159
auto& intern = *static_cast<CoreAudioInternal*> (inClientData);
11601160

1161-
const auto xruns = std::count_if (pa, pa + numAddresses, [] (const AudioObjectPropertyAddress& x)
1161+
const auto xruns = (int) std::count_if (pa, pa + numAddresses, [] (const AudioObjectPropertyAddress& x)
11621162
{
11631163
return x.mSelector == kAudioDeviceProcessorOverload;
11641164
});
11651165

1166-
intern.xruns += xruns;
1166+
intern.xruns.fetch_add (xruns);
11671167

11681168
const auto detailsChanged = std::any_of (pa, pa + numAddresses, [] (const AudioObjectPropertyAddress& x)
11691169
{
@@ -1318,7 +1318,7 @@ struct CoreAudioClasses
13181318

13191319
int getCurrentBufferSizeSamples() override { return internal->getBufferSize(); }
13201320

1321-
int getXRunCount() const noexcept override { return internal->xruns; }
1321+
int getXRunCount() const noexcept override { return internal->xruns.load (std::memory_order_relaxed); }
13221322

13231323
int getIndexOfDevice (bool asInput) const { return deviceType->getDeviceNames (asInput).indexOf (getName()); }
13241324

@@ -1341,7 +1341,7 @@ struct CoreAudioClasses
13411341
int bufferSizeSamples) override
13421342
{
13431343
isOpen_ = true;
1344-
internal->xruns = 0;
1344+
internal->xruns.store (0);
13451345

13461346
inputChannelsRequested = inputChannels;
13471347
outputChannelsRequested = outputChannels;
@@ -1650,10 +1650,12 @@ struct CoreAudioClasses
16501650
d->close();
16511651
}
16521652

1653-
void restart (AudioIODeviceCallback* cb)
1653+
void restart()
16541654
{
16551655
const ScopedLock sl (closeLock);
16561656

1657+
AudioIODeviceCallback* cb = previousCallback;
1658+
16571659
close();
16581660

16591661
auto newSampleRate = sampleRateRequested;
@@ -1763,22 +1765,22 @@ struct CoreAudioClasses
17631765

17641766
int getXRunCount() const noexcept override
17651767
{
1766-
return xruns.load();
1768+
return xruns.load (std::memory_order_relaxed);
17671769
}
17681770

17691771
private:
17701772
static constexpr auto invalidSampleTime = std::numeric_limits<std::uint64_t>::max();
17711773

17721774
WeakReference<CoreAudioIODeviceType> owner;
17731775
CriticalSection callbackLock;
1776+
CriticalSection closeLock;
17741777
AudioIODeviceCallback* callback = nullptr;
17751778
AudioIODeviceCallback* previousCallback = nullptr;
17761779
double currentSampleRate = 0;
17771780
int currentBufferSize = 0;
17781781
bool active = false;
17791782
String lastError;
17801783
AudioSampleBuffer fifo, scratchBuffer;
1781-
CriticalSection closeLock;
17821784
int targetLatency = 0;
17831785
std::atomic<int> xruns { -1 };
17841786
std::atomic<uint64_t> lastValidReadPosition { invalidSampleTime };
@@ -1791,7 +1793,7 @@ struct CoreAudioClasses
17911793
{
17921794
stopTimer();
17931795

1794-
restart (previousCallback);
1796+
restart();
17951797
}
17961798

17971799
void shutdown (const String& error)
@@ -1965,7 +1967,7 @@ struct CoreAudioClasses
19651967
for (auto& d : getDeviceWrappers())
19661968
d->sampleTime.store (invalidSampleTime);
19671969

1968-
++xruns;
1970+
xruns.fetch_add (1);
19691971
}
19701972

19711973
void handleAudioDeviceAboutToStart (AudioIODevice* device)
@@ -2132,8 +2134,7 @@ struct CoreAudioClasses
21322134
};
21332135

21342136
/* If the current AudioIODeviceCombiner::callback is nullptr, it sets itself as the callback
2135-
and forwards error related callbacks to the provided callback
2136-
*/
2137+
and forwards error related callbacks to the provided callback. */
21372138
class ScopedErrorForwarder final : public AudioIODeviceCallback
21382139
{
21392140
public:

modules/yup_audio_devices/yup_audio_devices.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ YUP_END_IGNORE_WARNINGS_MSVC
164164
#include <iasiodrv.h>
165165
#include "native/yup_ASIO_windows.cpp"
166166
#endif
167-
// clang-format oon
167+
// clang-format on
168168

169169
//==============================================================================
170170
#elif YUP_LINUX || YUP_BSD

modules/yup_audio_gui/yup_audio_gui.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222
#ifdef YUP_AUDIO_GUI_H_INCLUDED
2323
/* When you add this cpp file to your project, you mustn't include it in a file where you've
24-
already included any other headers - just put it inside a file on its own, possibly with your config
25-
flags preceding it, but don't include anything else. That also includes avoiding any automatic prefix
26-
header files that the compiler may be using.
27-
*/
24+
already included any other headers - just put it inside a file on its own, possibly with your config
25+
flags preceding it, but don't include anything else. That also includes avoiding any automatic prefix
26+
header files that the compiler may be using.
27+
*/
2828
#error "Incorrect use of YUP cpp file"
2929
#endif
3030

modules/yup_core/containers/yup_FixedSizeFunction.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ template <size_t len, typename Ret, typename... Args>
117117
class FixedSizeFunction<len, Ret (Args...)>
118118
{
119119
private:
120-
using Storage = std::aligned_storage_t<len>;
120+
using Storage = struct {
121+
alignas (alignof (std::max_align_t)) std::byte data[len];
122+
};
121123

122124
template <typename Item>
123125
using Decay = std::decay_t<Item>;

modules/yup_core/text/yup_StringPairArray.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ StringPairArray::StringPairArray (bool shouldIgnoreCase)
4545
{
4646
}
4747

48+
StringPairArray::StringPairArray (const std::initializer_list<KeyValuePair>& stringPairs)
49+
{
50+
for (const auto& item : stringPairs)
51+
{
52+
keys.add (item.key);
53+
values.add (item.value);
54+
}
55+
}
56+
57+
StringPairArray::StringPairArray (bool shouldIgnoreCase, const std::initializer_list<KeyValuePair>& stringPairs)
58+
: ignoreCase (shouldIgnoreCase)
59+
{
60+
for (const auto& item : stringPairs)
61+
{
62+
keys.add (item.key);
63+
values.add (item.value);
64+
}
65+
}
66+
4867
StringPairArray::StringPairArray (const StringPairArray& other)
4968
: keys (other.keys)
5069
, values (other.values)

0 commit comments

Comments
 (0)