secret of light-weight user thread

Simon Marlow marlowsd at
Wed Sep 7 14:29:19 CEST 2011

On 07/09/2011 08:13, Kazu Yamamoto (山本和彦) wrote:
> Simon,
> Thank you for explanation.
>>   - We have an accurate GC, which means that the Haskell stack can be
>>     movable, whereas the C stack isn't.  So we can start with small
>>     stacks and enlarge them as necessary.
> What is the difference between the Haskell stack and the C stack?
> I guess that the C stack means CPU's stack. Is the Haskell stack a
> virtual stack for a virtual machine (STG machine or something)?

There's no fundamental difference between the Haskell stack and the C 
stack, they are both just runtime data structures used by compiled code. 
  We designed the Haskell stack so that pointers within it can be 
identified by the GC, that's all.

When running Haskell code there's a register that points to the top of 
the Haskell stack, just like when running C code (it's a different 
register, but in principle there's no reason it has to be different).

> I quickly read several papers but I have no idea.
>>   - We only preempt threads at safe points.  This means that the
>>     context we have to save at a context switch is platform-independent
>>     and typically much smaller than the entire CPU context.  Safe
>>     points are currently on allocation, which is frequent enough in GHC
>>     for this to be indistinguishable (most of the time) from true
>>     preemption.
> I seems to me that StgRun saves CPU registers. You mean that StgRun
> depends on CPU a bit but the rest part of context is CPU independent?

StgRun is the interface between the C world and the Haskell world, which 
have different ABIs.  In particular, the C ABI requires that function 
calls do not modify certain registers (the callee-saves registers), 
whereas in Haskell there are no callee-saves registers. So StgRun saves 
the callee-saves registers while running Haskell code, that's all.  It 
may have to do other things depending on what specific conventions are 
used by C or Haskell code on the current platform.

This is just something we have to do so that we can call Haskell code 
from C.  It's not related to threads, except that the GHC scheduler is 
written in C so we have to go through StgRun every time we start or stop 
a Haskell thread.


More information about the Glasgow-haskell-users mailing list