From f50f070b357c5750bf39215b21f1995c1bcb163e Mon Sep 17 00:00:00 2001 From: Guillaume Piolat Date: Wed, 22 Jan 2025 16:18:52 +0100 Subject: [PATCH] More 1.12 compat --- core/dplug/core/sync.d | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/core/dplug/core/sync.d b/core/dplug/core/sync.d index 0e0fb540..f302766e 100644 --- a/core/dplug/core/sync.d +++ b/core/dplug/core/sync.d @@ -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); } @@ -134,6 +134,7 @@ nothrow @nogc: } else version( Posix ) { + assumeNothrowNoGC( (pthread_mutex_t* handle) { @@ -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. @@ -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); @@ -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