Juce::Atomic <> alignment


#1

from class Atomic:

Is this necessary even for a 4-byte value?


#2

Possibly not, but it may be needed for 64-bit values, so I erred on the side of caution.


#3

What are your thoughts on

JUCE_ALIGN (sizeof(Type));

??

I’m doing:

template <class Elem, class Tag>
struct List
{
  class Node : NonCopyable
  {
  public:
    Node() { }

    Atomic <Node*> m_next;
  };
};

Where list elements derive from List::Node


#4

TBH I’d be surprised if it made much difference in that case, as the object would be allocated on the heap, so almost certainly aligned to at least 8 bytes anyway.


#5

I have objects accessed from multiple threads that can live in several lists at the same time (through the tag parameter). For example:

[code]

struct Tag_gui {}; // dummy structure to distinguish Node types
//…

struct Track : List<Track,Tag_gui>::Node,
List<Track,Tag_library>::Node,
List<Track,Tag_audiocallback>::Node,
List<Track,Tag_deletedlist>::Node,
List<Track,Tag_preparing>::Node,
List<Track,Tag_by_artist>::Node,
List<Track,Tag_by_title>::Node,
List<Track,Tag_by_genre>::Node,
List<Track,Tag_directory>::Node
{
//…
};[/code]

This is just an example…the real list of base members is actually much larger (lol)


#6

Well, I can’t really think of any objections to aligning it to sizeof(Type)…


#7

This is what I found:

It doesn’t really say definitively. All I can think of is the possibility of a 64-bit system (Itanium perhaps?) that requires even 32-bit atomic operations to be quad-word aligned.

It would be cool if there was some compiler-specific typedef or intrinsic that supplied this information depending on the target system.


#8

BTW, I tried sizeof (Type), but actually it doesn’t work - the pragma can only take a literal constant.


#9

Hmm…I see what you mean. Well if you think it’s worth it, and all Juce platforms support partial class template specialization, this could get some mileage although there is a minor issue with using value in the ctor-initializer list:

template <typename Type, int Bytes>
struct AtomicAlignedValue;

template <typename Type>
struct AtomicAlignedValue <Type, 4>
{
  JUCE_ALIGN(4)
  volatile Type value;
};

template <typename Type>
struct AtomicAlignedValue <Type, 8>
{
  JUCE_ALIGN(8)
  volatile Type value;
};

template <typename Type>
class Atomic : public AtomicAlignedValue <Type, sizeof (Type)>