[Issue 19481] Aborting from local/libphobos/libdruntime/core/sync/mutex.d(95) Error: pthread_mutex_init failed.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Dec 13 23:03:35 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=19481

--- Comment #5 from Iain Buclaw <ibuclaw at gdcproject.org> ---
(In reply to kinke from comment #4)
> Argh, it's an array, and class instances aren't padded... - the error stems
> from the manual initialization in `initLocks()`, right? That should be
> easily adaptable; the ctor only gets the `this` pointer and shouldn't have
> any idea about the actual size.

Correct.

This line:

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


The size should be rounded up to a suitable align boundary, then this line:

    lock[] = typeid(Mutex).initializer[];

The slice assignment is adjusted to only initialize up to
__traits(classInstanceSize, Mutex).

So we end up with something that looks like:

---

version (Windows)
{
    import core.sys.windows.winbase : CRITICAL_SECTION;
    enum lockAlign = CRITICAL_SECTION.alignof;
}
else version (Posix)
{
    import core.sys.posix.sys.types : pthread_mutex_t;
    enum lockAlign = pthread_mutex_t.alignof;
}

enum mutexInstanceSize = __traits(classInstanceSize, Mutex)
enum lockAlignedSize = (mutexInstanceSize + lockAlign - 1) & ~(lockAlign - 1);

__gshared align(lockAlign) void[lockAlignedSize][2] _locks;

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

---

I don't think `align(lockAlign)` is really necessary based on feedback, but it
covers us "just incase".

--


More information about the Digitalmars-d-bugs mailing list