From 82fe75f9d58fc1388e61c0411b89d5fb49b16175 Mon Sep 17 00:00:00 2001 From: anonymous Date: Fri, 28 Jul 2017 19:27:09 +0200 Subject: [PATCH 1/2] let initOnce take a shared Mutex --- std/concurrency.d | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/std/concurrency.d b/std/concurrency.d index 2cd714c3aa8..d1d341f3eed 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; } +deprecated("use the overload that takes a `shared(Mutex)`") +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 From 0123ea97e1b550801b41fe762d0b955a20ab49fe Mon Sep 17 00:00:00 2001 From: anonymous Date: Sat, 2 Sep 2017 23:44:20 +0200 Subject: [PATCH 2/2] don't deprecate initOnce with unshared Mutex --- std/concurrency.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/concurrency.d b/std/concurrency.d index d1d341f3eed..c7af7ce2171 100644 --- a/std/concurrency.d +++ b/std/concurrency.d @@ -2498,7 +2498,7 @@ auto ref initOnce(alias var)(lazy typeof(var) init, shared Mutex mutex) return var; } -deprecated("use the overload that takes a `shared(Mutex)`") +/// ditto auto ref initOnce(alias var)(lazy typeof(var) init, Mutex mutex) { return initOnce!var(init, cast(shared) mutex);