This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 411
Add fork handling to GC #2817
Merged
Merged
Add fork handling to GC #2817
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1251,6 +1251,7 @@ struct Gcx | |
| alias leakDetector = LeakDetector; | ||
|
|
||
| SmallObjectPool*[B_NUMSMALL] recoverPool; | ||
| version (Posix) __gshared Gcx* instance; | ||
|
|
||
| void initialize() | ||
| { | ||
|
|
@@ -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) | ||
|
|
@@ -1310,6 +1324,8 @@ struct Gcx | |
| pauseTime, maxPause, apitxt.ptr); | ||
| } | ||
|
|
||
| version (Posix) | ||
| instance = null; | ||
| version (COLLECT_PARALLEL) | ||
| stopScanThreads(); | ||
|
|
||
|
|
@@ -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 | ||
|
|
@@ -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"); | ||
| } | ||
|
|
@@ -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(); | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.