mirror of
https://github.com/dlang/phobos.git
synced 2025-04-30 15:10:46 +03:00
Merge pull request #2943 from MartinNowak/useInitOnce
use initOnce for lazy shared initialization
This commit is contained in:
commit
bcedffd0ee
3 changed files with 18 additions and 44 deletions
|
@ -75,7 +75,6 @@ private
|
||||||
import core.sync.mutex;
|
import core.sync.mutex;
|
||||||
import core.sync.condition;
|
import core.sync.condition;
|
||||||
import std.algorithm;
|
import std.algorithm;
|
||||||
import std.datetime;
|
|
||||||
import std.exception;
|
import std.exception;
|
||||||
import std.range;
|
import std.range;
|
||||||
import std.string;
|
import std.string;
|
||||||
|
@ -1373,11 +1372,12 @@ private:
|
||||||
|
|
||||||
override bool wait( Duration period ) nothrow
|
override bool wait( Duration period ) nothrow
|
||||||
{
|
{
|
||||||
|
import core.time;
|
||||||
scope(exit) notified = false;
|
scope(exit) notified = false;
|
||||||
|
|
||||||
for( auto limit = Clock.currSystemTick + period;
|
for( auto limit = TickDuration.currSystemTick + period;
|
||||||
!notified && !period.isNegative;
|
!notified && !period.isNegative;
|
||||||
period = limit - Clock.currSystemTick )
|
period = limit - TickDuration.currSystemTick )
|
||||||
{
|
{
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
|
@ -2047,7 +2047,8 @@ private
|
||||||
|
|
||||||
static if( timedWait )
|
static if( timedWait )
|
||||||
{
|
{
|
||||||
auto limit = Clock.currSystemTick + period;
|
import core.time;
|
||||||
|
auto limit = TickDuration.currSystemTick + period;
|
||||||
}
|
}
|
||||||
|
|
||||||
while( true )
|
while( true )
|
||||||
|
@ -2095,7 +2096,7 @@ private
|
||||||
{
|
{
|
||||||
static if( timedWait )
|
static if( timedWait )
|
||||||
{
|
{
|
||||||
period = limit - Clock.currSystemTick;
|
period = limit - TickDuration.currSystemTick;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27333,31 +27333,15 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static immutable LocalTime _localTime = new immutable(LocalTime)();
|
|
||||||
// Use low-lock singleton pattern with _tzsetWasCalled (see http://dconf.org/talks/simcha.html)
|
|
||||||
static bool _lowLock;
|
|
||||||
static shared bool _tzsetWasCalled;
|
|
||||||
|
|
||||||
|
|
||||||
// This is done so that we can maintain purity in spite of doing an impure
|
// This is done so that we can maintain purity in spite of doing an impure
|
||||||
// operation the first time that LocalTime() is called.
|
// operation the first time that LocalTime() is called.
|
||||||
static immutable(LocalTime) singleton() @trusted
|
static immutable(LocalTime) singleton() @trusted
|
||||||
{
|
{
|
||||||
if(!_lowLock)
|
import std.concurrency : initOnce;
|
||||||
{
|
static instance = new immutable(LocalTime)();
|
||||||
synchronized
|
static shared bool guard;
|
||||||
{
|
initOnce!guard({tzset(); return true;}());
|
||||||
if(!_tzsetWasCalled)
|
return instance;
|
||||||
{
|
|
||||||
tzset();
|
|
||||||
_tzsetWasCalled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_lowLock = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return _localTime;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3273,24 +3273,13 @@ terminating the main thread.
|
||||||
*/
|
*/
|
||||||
@property TaskPool taskPool() @trusted
|
@property TaskPool taskPool() @trusted
|
||||||
{
|
{
|
||||||
static bool initialized;
|
import std.concurrency : initOnce;
|
||||||
__gshared static TaskPool pool;
|
__gshared TaskPool pool;
|
||||||
|
return initOnce!pool({
|
||||||
if(!initialized)
|
auto p = new TaskPool(defaultPoolThreads);
|
||||||
{
|
p.isDaemon = true;
|
||||||
synchronized(typeid(TaskPool))
|
return p;
|
||||||
{
|
}());
|
||||||
if(!pool)
|
|
||||||
{
|
|
||||||
pool = new TaskPool(defaultPoolThreads);
|
|
||||||
pool.isDaemon = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
initialized = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return pool;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private shared uint _defaultPoolThreads;
|
private shared uint _defaultPoolThreads;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue