diff options
author | Simon Marlow <marlowsd@gmail.com> | 2011-12-01 10:53:28 +0000 |
---|---|---|
committer | Simon Marlow <marlowsd@gmail.com> | 2011-12-01 11:29:53 +0000 |
commit | 6d18141d880d55958c3392f6a7ae621dc33ee5c1 (patch) | |
tree | edd5607a50a92ffad4707cef792a06246b5b41e4 /rts/Capability.h | |
parent | 1d012e31577951ff5fe74d0277fabdb08c27929d (diff) | |
download | haskell-6d18141d880d55958c3392f6a7ae621dc33ee5c1.tar.gz |
Fix a scheduling bug in the threaded RTS
The parallel GC was using setContextSwitches() to stop all the other
threads, which sets the context_switch flag on every Capability. That
had the side effect of causing every Capability to also switch
threads, and since GCs can be much more frequent than context
switches, this increased the context switch frequency. When context
switches are expensive (because the switch is between two bound
threads or a bound and unbound thread), the difference is quite
noticeable.
The fix is to have a separate flag to indicate that a Capability
should stop and return to the scheduler, but not switch threads. I've
called this the "interrupt" flag.
Diffstat (limited to 'rts/Capability.h')
-rw-r--r-- | rts/Capability.h | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/rts/Capability.h b/rts/Capability.h index 0bc2985d9e..1957487329 100644 --- a/rts/Capability.h +++ b/rts/Capability.h @@ -72,10 +72,20 @@ struct Capability_ { // block for allocating pinned objects into bdescr *pinned_object_block; - // Context switch flag. We used to have one global flag, now one - // per capability. Locks required : none (conflicts are harmless) + // Context switch flag. When non-zero, this means: stop running + // Haskell code, and switch threads. int context_switch; + // Interrupt flag. Like the context_switch flag, this also + // indicates that we should stop running Haskell code, but we do + // *not* switch threads. This is used to stop a Capability in + // order to do GC, for example. + // + // The interrupt flag is always reset before we start running + // Haskell code, unlike the context_switch flag which is only + // reset after we have executed the context switch. + int interrupt; + #if defined(THREADED_RTS) // Worker Tasks waiting in the wings. Singly-linked. Task *spare_workers; @@ -275,9 +285,14 @@ void shutdownCapability (Capability *cap, Task *task, rtsBool wait_foreign); void shutdownCapabilities(Task *task, rtsBool wait_foreign); // cause all capabilities to context switch as soon as possible. -void setContextSwitches(void); +void contextSwitchAllCapabilities(void); INLINE_HEADER void contextSwitchCapability(Capability *cap); +// cause all capabilities to stop running Haskell code and return to +// the scheduler as soon as possible. +void interruptAllCapabilities(void); +INLINE_HEADER void interruptCapability(Capability *cap); + // Free all capabilities void freeCapabilities (void); @@ -346,14 +361,27 @@ discardSparksCap (Capability *cap) #endif INLINE_HEADER void -contextSwitchCapability (Capability *cap) +stopCapability (Capability *cap) { - // setting HpLim to NULL ensures that the next heap check will - // fail, and the thread will return to the scheduler. + // setting HpLim to NULL tries to make the next heap check will + // fail, which will cause the thread to return to the scheduler. + // It may not work - the thread might be updating HpLim itself + // at the same time - so we also have the context_switch/interrupted + // flags as a sticky way to tell the thread to stop. cap->r.rHpLim = NULL; - // But just in case it didn't work (the target thread might be - // modifying HpLim at the same time), we set the end-of-block - // context-switch flag too: +} + +INLINE_HEADER void +interruptCapability (Capability *cap) +{ + stopCapability(cap); + cap->interrupt = 1; +} + +INLINE_HEADER void +contextSwitchCapability (Capability *cap) +{ + stopCapability(cap); cap->context_switch = 1; } |