Skip to content

Commit

Permalink
More 1.12 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Piolat committed Jan 22, 2025
1 parent c35efcc commit f50f070
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions core/dplug/core/sync.d
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ nothrow @nogc:
// TODO: not easy to make the mutex destruction thread-safe, because one of the
// thread must wait. Ignore that for now.

void* mutexHandle = atomicLoad(_mutex); // Can be the mutex handle, or null.
void* mutexHandle = atomicLoadCompat(_mutex); // Can be the mutex handle, or null.

if (mutexHandle !is null)
{
// Now, the mutex could have been destroyed by another thread already.
// Make a cas to ensure we are first to attempt it.

if (cas(&_mutex, &mutexHandle, null))
if (casCompat(&_mutex, &mutexHandle, null))
{
destroyMutex(mutexHandle);
}
Expand Down Expand Up @@ -134,6 +134,7 @@ nothrow @nogc:
}
else version( Posix )
{

assumeNothrowNoGC(
(pthread_mutex_t* handle)
{
Expand Down Expand Up @@ -258,7 +259,7 @@ private:
void lazyThreadSafeInitialization() @trusted
{
// Is there an existing mutex already?
if (atomicLoad(_mutex) !is null)
if (atomicLoadCompat(_mutex) !is null)
return;

// Create one mutex.
Expand All @@ -268,7 +269,7 @@ private:
void* ifThis = null;

// Try to set _mutex.
if (!cas(here, &ifThis, p))
if (!casCompat(here, &ifThis, p))
{
// Another thread created _mutex first. Destroy our useless instance.
destroyMutex(mtx);
Expand All @@ -283,6 +284,39 @@ package:
return cast(pthread_mutex_t*) _mutex;
}
}
private:

static void* atomicLoadCompat(ref void* p)
{
static if (__VERSION__ < 2094)
{
// old compiler, do it incorrectly
return p;
}
else
return atomicLoad(p);
}

static bool casCompat(void** here,
void** ifThis,
void* writeThis)
{
static if (__VERSION__ < 2094)
{
// old compiler, do it incorrectly
if (*here == *ifThis)
{
*here = writeThis;
return true;
}
else
return false;
}
else
{
return cas(here, ifThis, writeThis);
}
}
}

unittest
Expand Down

0 comments on commit f50f070

Please # to comment.