Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions std/concurrency.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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; })),
Expand All @@ -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
Expand Down