Hi Rand,

> > the user stack, there should be sufficient space. As I said, we have 1
> > MB per stack frame, and this can be easily extended with the 'stack'
> > function.
> 
> The hardware (CPU) will write at least two words (instruction pointer
> and CPU status register, and maybe more) onto the user stack before
> switching to the system stack.

OK, that would be no problem, as there should always be sufficient space
reserved on each coroutine's stack frame.


> 
> > Again, my problem is not to have a stack frame for each task, but having
> > stack frames BELOW the active stack pointer.
> 
> I'm not sure about this in the general case. I would feel uncomfortable about 
> it.

Me too, that's why I started this discussion. :)


To make things more clear, let's consider the following situation. It is
after the program started two coroutines, and then execution continued
in the main program. The two coroutines are suspended, and the hardware
stack points into the main program's stack frame:

                  +---------------+
Stack frame #1    |               |
Main program      |               |
                  |    Stack      |
                  |    Area       | <- SP (Stack pointer register points here)
                  |               |
                        ...
                  |  e.g. 4 MB    |
                  |               |
                  |               |
                  |               |
                  +---------------+
Stack frame #2    |     tag       |
Coroutine #1      +---------------+
                  |   Saved SP    | +--+ This is the SP of the suspended 
coroutine
                  +---------------+    |
                  |               |    |
                  |  Environment  |    |
                  |               |    |
                  +---------------+    |
                  |               |    |
                  |               |    |
                  |    Stack      |    |
                  |    Area       | <--+
                  |               |
                        ...
                  |  e.g. 1 MB    |
                  |               |
                  |               |
                  |               |
                  +---------------+
Stack frame #3    |     tag       |
Coroutine #2      +---------------+
                  |   Saved SP    | +--+ This is the SP of the suspended 
coroutine
                  +---------------+    |
                  |               |    |
                  |  Environment  |    |
                  |               |    |
                  +---------------+    |
                  |               |    |
                  |               |    |
                  |               | <--+
                        ...

Now, if the system inspects this process (during a system call or
interrupt), it finds that - despite the fact that the process ONCE used
a large stack space - the stack pointer is now back in the upper regions
of the stack space.

The system doesn't know about the frame structures. It will assume that
the process did a lot of pushes and/or recursive calls, but is now done
with them, and the stack is no longer needed.

So it might decide to purge it.

The big question is: Will the system (which system?) do that? If so, how
do other languages implement green threads, coroutines or continuations?

♪♫ Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

Reply via email to