Skip to content

Commit ce125f5

Browse files
committed
Make IPC message decoding thread safe
1 parent cc1d99b commit ce125f5

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

IPC/ARAIPCEncoding.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ struct _ValueDecoder<std::vector<ElementT>> : public _CompoundValueDecoderBase<s
424424
template<> struct _ValueEncoder<StructT> : public _CompoundValueEncoderBase<StructT> \
425425
{ /* specialization for given struct */ \
426426
using StructType = StructT; \
427-
static inline void encode (MessageEncoder* encoder, const StructType& value) \
427+
static inline void encode (MessageEncoder* encoder, const StructType& value) \
428428
{
429429
#define ARA_IPC_ENCODE_MEMBER(member) \
430430
_encodeAndAppend (encoder, offsetof (StructType, member), value.member);
@@ -467,7 +467,7 @@ template<> struct _ValueEncoder<StructT> : public _CompoundValueEncoderBase<Stru
467467
template<> struct _ValueDecoder<StructT> : public _CompoundValueDecoderBase<StructT> \
468468
{ /* specialization for given struct */ \
469469
using StructType = StructT; \
470-
static inline bool decode (StructType& result, const MessageDecoder* decoder) \
470+
static inline bool decode (StructType& result, const MessageDecoder* decoder) \
471471
{ \
472472
bool success { true };
473473
#define ARA_IPC_BEGIN_DECODE_SIZED(StructT) \
@@ -487,9 +487,9 @@ template<> struct _ValueDecoder<StructT> : public _CompoundValueDecoderBase<Stru
487487
success &= _readAndDecode (tmp_##member, decoder, offsetof (StructType, member)); \
488488
ARA_INTERNAL_ASSERT (success);
489489
#define ARA_IPC_DECODE_VARIABLE_ARRAY(member, count, updateCount) \
490-
/* \todo the outer struct contains a pointer to the inner array, so we need some */ \
491-
/* place to store it - this static only works as long as this is single-threaded! */ \
492-
static std::vector<typename std::remove_const<std::remove_pointer<decltype (result.member)>::type>::type> tmp_##member; \
490+
/* \todo the outer struct contains a pointer to the inner struct, so we need some */ \
491+
/* thread-safe place to store it - is there a better way to achieve this? */ \
492+
thread_local std::vector<typename std::remove_const<std::remove_pointer<decltype (result.member)>::type>::type> tmp_##member; \
493493
if (_readAndDecode (tmp_##member, decoder, offsetof (StructType, member))) { \
494494
result.member = tmp_##member.data (); \
495495
if (updateCount) { result.count = tmp_##member.size (); } \
@@ -518,8 +518,8 @@ template<> struct _ValueDecoder<StructT> : public _CompoundValueDecoderBase<Stru
518518
auto subDecoder_##member { decoder->readSubMessage (offsetof (StructType, member)) }; \
519519
if (subDecoder_##member != nullptr) { \
520520
/* \todo the outer struct contains a pointer to the inner struct, so we need some */\
521-
/* place to store it - this static only works as long as this is single-threaded! */\
522-
static std::remove_const<std::remove_pointer<decltype (result.member)>::type>::type cache; \
521+
/* thread-safe place to store it - is there a better way to achieve this? */\
522+
thread_local std::remove_const<std::remove_pointer<decltype (result.member)>::type>::type cache; \
523523
success &= _ValueDecoder<decltype (cache)>::decode (cache, subDecoder_##member); \
524524
ARA_INTERNAL_ASSERT (success); \
525525
result.member = &cache; \
@@ -621,8 +621,8 @@ ARA_IPC_BEGIN_DECODE_SIZED (ARAAudioSourceProperties)
621621
else
622622
{
623623
/* \todo the outer struct contains a pointer to the inner struct, so we need some */
624-
/* place to store it - this static only works as long as this is single-threaded! */
625-
static ARAByte cache[2016UL]; // some arbitrary space that can be conveniently allocated
624+
/* thread-safe place to store it - is there a better way to achieve this? */
625+
thread_local ARAByte cache[2016UL]; // some arbitrary space that can be conveniently allocated
626626
auto resultSize_channelArrangement { sizeof (cache) };
627627
BytesDecoder tmp_channelArrangement { cache, resultSize_channelArrangement };
628628
if (_readAndDecode (tmp_channelArrangement, decoder, offsetof (ARAAudioSourceProperties, channelArrangement)) &&

0 commit comments

Comments
 (0)