Skip to content

Commit

Permalink
[GC] tweak GC cycle selection to always be odd.
Browse files Browse the repository at this point in the history
  • Loading branch information
deadalnix committed Oct 31, 2024
1 parent ae0f5a4 commit af95483
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions sdlib/d/gc/global.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@ private:

public:
ubyte nextGCCycle() shared {
auto c = cycle.fetchAdd(1);
return (c + 1) & ubyte.max;
auto old = cycle.load();
while (true) {
/**
* Because we initialize extents with cycle 0, we want to make sure
* the chosen GC cycle is never 0. To do so, we ensure it is odd.
* Alternatively, we could try to initialize the cycle to
* a specific value. This is almost certainly necessary if we want
* to go concurrent.
*/
ubyte c = ((old + 1) | 0x01) & 0xff;
if (cycle.casWeak(old, c)) {
return c;
}
}
}

/**
Expand Down Expand Up @@ -79,7 +91,7 @@ private:
auto index = roots.length;
auto length = index + 1;

// We realloc everytime. It doesn't really matter at this point.
// We realloc every time. It doesn't really matter at this point.
import d.gc.tcache;
ptr = threadCache.realloc(ptr, length * void*[].sizeof, true);
roots = (cast(const(void*)[]*) ptr)[0 .. length];
Expand Down

0 comments on commit af95483

Please sign in to comment.