diff --git a/std/concurrency.d b/std/concurrency.d index 2cd714c3aa8..c7af7ce2171 100644 --- a/std/concurrency.d +++ b/std/concurrency.d @@ -2390,15 +2390,15 @@ version (unittest) } } -private @property Mutex initOnceLock() +private @property shared(Mutex) initOnceLock() { - __gshared Mutex lock; - if (auto mtx = cast() atomicLoad!(MemoryOrder.acq)(*cast(shared)&lock)) + static shared Mutex lock; + if (auto mtx = atomicLoad!(MemoryOrder.acq)(lock)) return mtx; - auto mtx = new Mutex; - if (cas(cast(shared)&lock, cast(shared) null, cast(shared) mtx)) + auto mtx = new shared Mutex; + if (cas(&lock, cast(shared) null, mtx)) return mtx; - return cast() atomicLoad!(MemoryOrder.acq)(*cast(shared)&lock); + return atomicLoad!(MemoryOrder.acq)(lock); } /** @@ -2476,7 +2476,7 @@ auto ref initOnce(alias var)(lazy typeof(var) init) * Returns: * A reference to the initialized variable */ -auto ref initOnce(alias var)(lazy typeof(var) init, Mutex mutex) +auto ref initOnce(alias var)(lazy typeof(var) init, shared Mutex mutex) { // check that var is global, can't take address of a TLS variable static assert(is(typeof({ __gshared p = &var; })), @@ -2498,14 +2498,20 @@ auto ref initOnce(alias var)(lazy typeof(var) init, Mutex mutex) return var; } +/// ditto +auto ref initOnce(alias var)(lazy typeof(var) init, Mutex mutex) +{ + return initOnce!var(init, cast(shared) mutex); +} + /// Use a separate mutex when init blocks on another thread that might also call initOnce. @system unittest { import core.sync.mutex : Mutex; static shared bool varA, varB; - __gshared Mutex m; - m = new Mutex; + static shared Mutex m; + m = new shared Mutex; spawn({ // use a different mutex for varB to avoid a dead-lock