Skip to content

Commit

Permalink
[GC] Consistently take the CollectorState lock when collecting.
Browse files Browse the repository at this point in the history
  • Loading branch information
deadalnix committed Sep 27, 2024
1 parent 807835a commit 003b3b5
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions sdlib/d/gc/collector.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ struct Collector {
}

void runGCCycle() {
gCollectorState.mutex.lock();
scope(exit) gCollectorState.mutex.unlock();

runGCCycleLocked();
}

private:
void runGCCycleLocked() {
assert(gCollectorState.mutex.isHeld(), "Mutex not held!");
assert(!threadCache.state.busy, "Cannot run GC cycle while busy!");

import d.gc.thread;
stopTheWorld();
scope(exit) restartTheWorld();
Expand All @@ -47,7 +58,7 @@ struct Collector {
/**
* We might have allocated, and therefore refilled the bin
* during the collection process. As a result, slots in the
* bins may not be makred at this point.
* bins may not be marked at this point.
*
* The straightforward way to handle this is simply to flush
* the bins.
Expand Down Expand Up @@ -92,9 +103,14 @@ private:
public:
bool maybeRunGCCycle(ref Collector collector, ref size_t delta,
ref size_t target) shared {
mutex.lock();
scope(exit) mutex.unlock();
// Do not unnecessarily create contention on this mutex.
if (!mutex.tryLock()) {
delta = 0;
target = 0;
return false;
}

scope(exit) mutex.unlock();
return (cast(CollectorState*) &this)
.maybeRunGCCycleImpl(collector, delta, target);
}
Expand Down

0 comments on commit 003b3b5

Please sign in to comment.