Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/core/internal/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ template maxAlignment(U...)
}
}

template classInstanceAlignment(T)
if (is(T == class))
{
alias classInstanceAlignment = maxAlignment!(void*, typeof(T.tupleof));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to work for fields with explicit alignment:

class C { align(16) int x; }
pragma(msg, classInstanceAlignment!C); // 4u on 32-bit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither does the phobos template of the same name. It's a library limitation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rainers I can't find a way to get the explicit alignment of class fields:

class C { align(16) int x; }

void main()
{
    auto c = new C();    
    pragma (msg, c.x.alignof);
    pragma (msg, C.x.alignof);
    pragma (msg, C.tupleof[0].alignof);
}
4LU
4LU
4LU

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem (maybe a bug) seems to be that the compiler only looks at the type, not the symbol.

}

/// See $(REF hasElaborateMove, std,traits)
template hasElaborateMove(S)
{
Expand Down
30 changes: 19 additions & 11 deletions src/core/thread/osthread.d
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ private
{
import core.atomic, core.memory, core.sync.mutex;

// Handling unaligned mutexes are not supported on all platforms, so we must
// ensure that the address of all shared data are appropriately aligned.
import core.internal.traits : classInstanceAlignment;

enum mutexAlign = classInstanceAlignment!Mutex;
enum mutexClassInstanceSize = __traits(classInstanceSize, Mutex);

//
// exposed by compiler runtime
//
Expand Down Expand Up @@ -1790,29 +1797,30 @@ package(core.thread):
// lock order inversion.
@property static Mutex slock() nothrow @nogc
{
return cast(Mutex)_locks[0].ptr;
return cast(Mutex)_slock.ptr;
}

@property static Mutex criticalRegionLock() nothrow @nogc
{
return cast(Mutex)_locks[1].ptr;
return cast(Mutex)_criticalRegionLock.ptr;
}

__gshared align(Mutex.alignof) void[__traits(classInstanceSize, Mutex)][2] _locks;
__gshared align(mutexAlign) void[mutexClassInstanceSize] _slock;
__gshared align(mutexAlign) void[mutexClassInstanceSize] _criticalRegionLock;

static void initLocks() @nogc
{
foreach (ref lock; _locks)
{
lock[] = typeid(Mutex).initializer[];
(cast(Mutex)lock.ptr).__ctor();
}
_slock[] = typeid(Mutex).initializer[];
(cast(Mutex)_slock.ptr).__ctor();

_criticalRegionLock[] = typeid(Mutex).initializer[];
(cast(Mutex)_criticalRegionLock.ptr).__ctor();
}

static void termLocks() @nogc
{
foreach (ref lock; _locks)
(cast(Mutex)lock.ptr).__dtor();
(cast(Mutex)_slock.ptr).__dtor();
(cast(Mutex)_criticalRegionLock.ptr).__dtor();
}

__gshared Context* sm_cbeg;
Expand Down Expand Up @@ -3726,7 +3734,7 @@ private
__gshared size_t ll_nThreads;
__gshared ll_ThreadData* ll_pThreads;

__gshared align(Mutex.alignof) void[__traits(classInstanceSize, Mutex)] ll_lock;
__gshared align(mutexAlign) void[mutexClassInstanceSize] ll_lock;

@property Mutex lowlevelLock() nothrow @nogc
{
Expand Down