Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Show file tree
Hide file tree
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
67 changes: 47 additions & 20 deletions src/gc/impl/conservative/gc.d
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,7 @@ struct Gcx
alias leakDetector = LeakDetector;

SmallObjectPool*[B_NUMSMALL] recoverPool;
version (Posix) __gshared Gcx* instance;

void initialize()
{
Expand All @@ -1262,10 +1263,23 @@ struct Gcx
usedSmallPages = usedLargePages = 0;
mappedPages = 0;
//printf("gcx = %p, self = %x\n", &this, self);
version (Posix)
{
import core.sys.posix.pthread : pthread_atfork;
instance = &this;
__gshared atforkHandlersInstalled = false;
if (!atforkHandlersInstalled)
{
pthread_atfork(
&_d_gcx_atfork_prepare,
&_d_gcx_atfork_parent,
&_d_gcx_atfork_child);
atforkHandlersInstalled = true;
}
}
debug(INVARIANT) initialized = true;
}


void Dtor()
{
if (config.profile)
Expand Down Expand Up @@ -1310,6 +1324,8 @@ struct Gcx
pauseTime, maxPause, apitxt.ptr);
}

version (Posix)
instance = null;
version (COLLECT_PARALLEL)
stopScanThreads();

Expand Down Expand Up @@ -2841,13 +2857,6 @@ struct Gcx
sigmask_rc = pthread_sigmask(SIG_SETMASK, &old_mask, null);
assert(sigmask_rc == 0, "failed to set up GC scan thread sigmask");
}

version (Posix)
{
import core.sys.posix.pthread;
forkedGcx = &this;
pthread_atfork(null, null, &initChildAfterFork);
}
}

void stopScanThreads() nothrow
Expand Down Expand Up @@ -2883,8 +2892,6 @@ struct Gcx
cstdlib.free(scanThreadData);
// scanThreadData = null; // keep non-null to not start again after shutdown
numScanThreads = 0;
version (Posix)
forkedGcx = null;

debug(PARALLEL_PRINTF) printf("stopScanThreads done\n");
}
Expand Down Expand Up @@ -2942,20 +2949,40 @@ struct Gcx

version (Posix)
{
// make sure the threads and event handles are reinitialized in a fork
__gshared Gcx* forkedGcx;
// A fork might happen while GC code is running in a different thread.
// Because that would leave the GC in an inconsistent state,
// make sure no GC code is running by acquiring the lock here,
// before a fork.

extern(C) static void _d_gcx_atfork_prepare()
{
if (instance)
ConservativeGC.lockNR();
}

extern(C) static void _d_gcx_atfork_parent()
{
if (instance)
ConservativeGC.gcLock.unlock();
}
Copy link
Member Author

@CyberShadow CyberShadow Oct 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will probably later move these out to a general Druntime fork handler which also takes care of the thread stuff.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That probably won't work as the GC can be swapped out at link time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean. The atfork notifications would then be part of the abstract GC interface.


extern(C) static void initChildAfterFork()
extern(C) static void _d_gcx_atfork_child()
{
if (forkedGcx && forkedGcx.scanThreadData)
if (instance)
{
cstdlib.free(forkedGcx.scanThreadData);
forkedGcx.numScanThreads = 0;
forkedGcx.scanThreadData = null;
forkedGcx.busyThreads = 0;
ConservativeGC.gcLock.unlock();

memset(&forkedGcx.evStart, 0, Event.sizeof);
memset(&forkedGcx.evDone, 0, Event.sizeof);
// make sure the threads and event handles are reinitialized in a fork
if (Gcx.instance.scanThreadData)
{
cstdlib.free(Gcx.instance.scanThreadData);
Gcx.instance.numScanThreads = 0;
Gcx.instance.scanThreadData = null;
Gcx.instance.busyThreads = 0;

memset(&Gcx.instance.evStart, 0, Gcx.instance.evStart.sizeof);
memset(&Gcx.instance.evDone, 0, Gcx.instance.evDone.sizeof);
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion test/gc/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include ../common.mak

TESTS := sentinel printf memstomp invariant logging precise precisegc forkgc sigmaskgc
TESTS := sentinel printf memstomp invariant logging precise precisegc forkgc forkgc2 sigmaskgc

SRC_GC = ../../src/gc/impl/conservative/gc.d
SRC = $(SRC_GC) ../../src/rt/lifetime.d
Expand Down Expand Up @@ -40,6 +40,9 @@ $(ROOT)/precisegc: $(SRC)
$(ROOT)/forkgc: forkgc.d
$(DMD) $(UDFLAGS) -of$@ forkgc.d

$(ROOT)/forkgc2: forkgc2.d
$(DMD) $(UDFLAGS) -of$@ forkgc2.d

$(ROOT)/sigmaskgc: sigmaskgc.d
$(DMD) $(UDFLAGS) -of$@ sigmaskgc.d

Expand Down
22 changes: 22 additions & 0 deletions test/gc/forkgc2.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import core.stdc.stdlib : exit;
import core.sys.posix.sys.wait : waitpid;
import core.sys.posix.unistd : fork;
import core.thread : Thread;

void main()
{
foreach (t; 0 .. 10)
new Thread({
foreach (n; 0 .. 100)
{
foreach (x; 0 .. 100)
new ubyte[x];
auto f = fork();
assert(f >= 0);
if (f == 0)
exit(0);
else
waitpid(f, null, 0);
}
}).start();
}