Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1491 from MartinNowak/gc_run_locked
Browse files Browse the repository at this point in the history
replace doLock/doUnlock with runLocked
  • Loading branch information
rainers committed Feb 8, 2016
2 parents bc8b4cd + 34af399 commit fe7f77f
Showing 1 changed file with 57 additions and 108 deletions.
165 changes: 57 additions & 108 deletions src/gc/gc.d
Expand Up @@ -345,40 +345,29 @@ struct GC
gcx.disabled++;
}


mixin template doLock()
auto runLocked(alias func, alias time, alias count, Args...)(auto ref Args args)
{
debug(PROFILE_API)
{
long tm = (GC.config.profile > 1 ? currTime.ticks : 0);
}
debug(PROFILE_API) {} else pragma(inline, true);
debug(PROFILE_API) immutable tm = (GC.config.profile > 1 ? currTime.ticks : 0);
gcLock.lock();
debug(PROFILE_API) immutable tm2 = (GC.config.profile > 1 ? currTime.ticks : 0);

bool locked = (gcLock.lock(), true);
static if (is(typeof(func(args)) == void))
func(args);
else
auto res = func(args);

debug(PROFILE_API)
debug(PROFILE_API) if (GC.config.profile > 1)
{
long tm2 = (GC.config.profile > 1 ? currTime.ticks : 0);
count++;
immutable now = currTime.ticks;
lockTime += tm2 - tm;
time += now - tm2;
}
}
gcLock.unlock();

mixin template doUnlock(alias time, alias count)
{
debug(PROFILE_API)
{
bool unlocked = () {
if (GC.config.profile > 1)
{
count++;
long now = currTicks;
lockTime += tm2 - tm;
time += now - tm2;
}
gcLock.unlock();
return true;
}();
}
else
bool unlocked = (gcLock.unlock(), true);
static if (!is(typeof(func(args)) == void))
return res;
}

/**
Expand All @@ -391,7 +380,7 @@ struct GC
return 0;
}

uint go() nothrow
static uint go(Gcx* gcx, void* p) nothrow
{
Pool* pool = gcx.findPool(p);
uint oldb = 0;
Expand All @@ -406,10 +395,7 @@ struct GC
return oldb;
}

mixin doLock!();
uint rc = go();
mixin doUnlock!(otherTime, numOthers);
return rc;
return runLocked!(go, otherTime, numOthers)(gcx, p);
}


Expand All @@ -423,7 +409,7 @@ struct GC
return 0;
}

uint go() nothrow
static uint go(Gcx* gcx, void* p, uint mask) nothrow
{
Pool* pool = gcx.findPool(p);
uint oldb = 0;
Expand All @@ -439,10 +425,7 @@ struct GC
return oldb;
}

mixin doLock!();
uint rc = go();
mixin doUnlock!(otherTime, numOthers);
return rc;
return runLocked!(go, otherTime, numOthers)(gcx, p, mask);
}


Expand All @@ -456,7 +439,7 @@ struct GC
return 0;
}

uint go() nothrow
static uint go(Gcx* gcx, void* p, uint mask) nothrow
{
Pool* pool = gcx.findPool(p);
uint oldb = 0;
Expand All @@ -472,10 +455,7 @@ struct GC
return oldb;
}

mixin doLock!();
uint rc = go();
mixin doUnlock!(otherTime, numOthers);
return rc;
return runLocked!(go, otherTime, numOthers)(gcx, p, mask);
}

/**
Expand All @@ -490,16 +470,10 @@ struct GC
return null;
}

void* p = void;
size_t localAllocSize = void;
if(alloc_size is null) alloc_size = &localAllocSize;

// Since a finalizer could launch a new thread, we always need to lock
// when collecting. The safest way to do this is to simply always lock
// when allocating.
mixin doLock!();
p = mallocNoSync(size, bits, *alloc_size, ti);
mixin doUnlock!(mallocTime, numMallocs);
auto p = runLocked!(mallocNoSync, mallocTime, numMallocs)(size, bits, *alloc_size, ti);

if (!(bits & BlkAttr.NO_SCAN))
{
Expand Down Expand Up @@ -553,15 +527,9 @@ struct GC
}

size_t localAllocSize = void;
void* p = void;
if(alloc_size is null) alloc_size = &localAllocSize;

// Since a finalizer could launch a new thread, we always need to lock
// when collecting. The safest way to do this is to simply always lock
// when allocating.
mixin doLock!();
p = mallocNoSync(size, bits, *alloc_size, ti);
mixin doUnlock!(mallocTime, numMallocs);
auto p = runLocked!(mallocNoSync, mallocTime, numMallocs)(size, bits, *alloc_size, ti);

memset(p, 0, size);
if (!(bits & BlkAttr.NO_SCAN))
Expand All @@ -581,12 +549,7 @@ struct GC
auto oldp = p;
if(alloc_size is null) alloc_size = &localAllocSize;

// Since a finalizer could launch a new thread, we always need to lock
// when collecting. The safest way to do this is to simply always lock
// when allocating.
mixin doLock!();
p = reallocNoSync(p, size, bits, *alloc_size, ti);
mixin doUnlock!(mallocTime, numMallocs);
p = runLocked!(reallocNoSync, mallocTime, numMallocs)(p, size, bits, *alloc_size, ti);

if (p !is oldp && !(bits & BlkAttr.NO_SCAN))
{
Expand Down Expand Up @@ -752,11 +715,7 @@ struct GC
*/
size_t extend(void* p, size_t minsize, size_t maxsize, const TypeInfo ti = null) nothrow
{
mixin doLock!();
size_t rc = extendNoSync(p, minsize, maxsize, ti);
mixin doUnlock!(extendTime, numExtends);

return rc;
return runLocked!(extendNoSync, extendTime, numExtends)(p, minsize, maxsize, ti);
}


Expand Down Expand Up @@ -861,9 +820,7 @@ struct GC
return;
}

mixin doLock!();
freeNoSync(p);
mixin doUnlock!(freeTime, numFrees);
return runLocked!(freeNoSync, freeTime, numFrees)(p);
}


Expand Down Expand Up @@ -944,11 +901,7 @@ struct GC
return null;
}

mixin doLock!();
void* rc = addrOfNoSync(p);
mixin doUnlock!(otherTime, numOthers);

return rc;
return runLocked!(addrOfNoSync, otherTime, numOthers)(p);
}


Expand Down Expand Up @@ -980,11 +933,7 @@ struct GC
return 0;
}

mixin doLock!();
size_t rc = sizeOfNoSync(p);
mixin doUnlock!(otherTime, numOthers);

return rc;
return runLocked!(sizeOfNoSync, otherTime, numOthers)(p);
}


Expand Down Expand Up @@ -1035,11 +984,7 @@ struct GC
return i;
}

mixin doLock!();
BlkInfo rc = queryNoSync(p);
mixin doUnlock!(otherTime, numOthers);

return rc;
return runLocked!(queryNoSync, otherTime, numOthers)(p);
}


Expand Down Expand Up @@ -1076,9 +1021,7 @@ struct GC
return;
}

mixin doLock!();
checkNoSync(p);
mixin doUnlock!(otherTime, numOthers);
return runLocked!(checkNoSync, otherTime, numOthers)(p);
}


Expand Down Expand Up @@ -1133,9 +1076,11 @@ struct GC
return;
}

mixin doLock!();
gcx.addRoot(p);
mixin doUnlock!(otherTime, numOthers);
static void go(Gcx* gcx, void* p) nothrow
{
gcx.addRoot(p);
}
return runLocked!(go, otherTime, numOthers)(gcx, p);
}


Expand All @@ -1149,9 +1094,11 @@ struct GC
return;
}

mixin doLock!();
gcx.removeRoot(p);
mixin doUnlock!(otherTime, numOthers);
static void go(Gcx* gcx, void* p) nothrow
{
gcx.removeRoot(p);
}
return runLocked!(go, otherTime, numOthers)(gcx, p);
}


Expand Down Expand Up @@ -1182,13 +1129,11 @@ struct GC
return;
}

//debug(PRINTF) printf("+GC.addRange(p = %p, sz = 0x%zx), p + sz = %p\n", p, sz, p + sz);

mixin doLock!();
gcx.addRange(p, p + sz, ti);
mixin doUnlock!(otherTime, numOthers);

//debug(PRINTF) printf("-GC.addRange()\n");
static void go(Gcx* gcx, void* p, size_t sz, const TypeInfo ti) nothrow @nogc
{
gcx.addRange(p, p + sz, ti);
}
return runLocked!(go, otherTime, numOthers)(gcx, p, sz, ti);
}


Expand All @@ -1202,19 +1147,23 @@ struct GC
return;
}

mixin doLock!();
gcx.removeRange(p);
mixin doUnlock!(otherTime, numOthers);
static void go(Gcx* gcx, void* p) nothrow @nogc
{
gcx.removeRange(p);
}
return runLocked!(go, otherTime, numOthers)(gcx, p);
}

/**
* run finalizers
*/
void runFinalizers(in void[] segment) nothrow
{
mixin doLock!();
gcx.runFinalizers(segment);
mixin doUnlock!(otherTime, numOthers);
static void go(Gcx* gcx, in void[] segment) nothrow
{
gcx.runFinalizers(segment);
}
return runLocked!(go, otherTime, numOthers)(gcx, segment);
}

private auto rangeIterImpl(scope int delegate(ref Range) nothrow dg) nothrow
Expand Down

0 comments on commit fe7f77f

Please sign in to comment.