Posted: Fri May 25, 2007 8:47 am
Do continuations in functional languages capture global state? I don't think so, but I wouldn't bet my life on that.
The Place to Start for Operating System Developers
https://f.osdev.org/
In pure functional languages, global state is immutable, so continuations wouldn't need to capture it.Crazed123 wrote:Do continuations in functional languages capture global state? I don't think so, but I wouldn't bet my life on that.
I'm guessing they wouldn't capture global state... It's one of those cases where "if it hurts, stop doing it" (which is not a very robust solution ). Are there mutexes in Scheme?Crazed123 wrote:Yes, but what about impure functional languages, like the ever-popular Scheme?
Likewise.I read for the first time this topic now and to be honest I do not understand completly the continuation system. So perhaps I should not write this, but still this topic is about thread related abstractions, so I will give you mine idea.
Not exactly.Avarok wrote: Essentially what you're describing is implementing multiple entry-points for processes, which can act as event handlers? You pass the handler to some other process as an entry-point address, and they simply blind-call it, and hope for the best?
You need to trust the OS only The OS is responsible to return you a "timeout" or "destination unreachable" responses, so you don't wait forever.My line of thinking has been that this employs a great deal of trust in the called *thing* (for lack of a better word).
No chance He is a way more famous than I am. He posted an article recently in embedded.com about ARM, so it seems we work in the same area, but that's all.Colonel Kernel wrote:@miro_atc:
(Aside) Are you by any chance Miro Samek?
My threads involve exactly that, but I pusha, making it simply a stack, and then I store the ESP somewhere I can find it so I can load the stack up later, popa, and resume.At the OS level, a continuation is the saved registers and stack of a suspended task.
That sounds cool. If you modified the stack while the thread wasn't running, you could pass asynchronous messages to it that it would process immediately once it resumed. I don't know what might happen if it was already running on a different processor.A running program can communicate with another program by sending data to the other program's saved continuation, which is called throwing the continuation.
Would this be equivalent to having the thread sleep/wait itself (by interrupt/call etc), and then have some other process mark it as "awake" again after changing it's stack?The operation call_with_current_continuation(function) suspends the running program, turning it into a continuation. It then passes that continuation to the given function of one argument, which runs with that continuation as input. When it wants to, it can throw data to the continuation, thus ceasing to run itself as the continuation resumes on the current processor.
This sounds to me like what interrupts tend to already do, but with threaded/continuated interrupt handlers. This can be particularly useful to do above simply 'running' them when you do I/O APIC and LAPIC work.When an event fires, the extremely-simple kernel scheduler decides whether to preempt the running computation to run an event handler (it won't preempt running event handlers to avoid infinitely recursive interrupts).
And for good reason. A continuation is the saved state of a suspended computation. A thread is a persistent object that comes in two states:Avarok wrote: My threads involve exactly that, but I pusha, making it simply a stack, and then I store the ESP somewhere I can find it so I can load the stack up later, popa, and resume.
Continuations do not run on top of threads. They run under threads. You can build threads on top of a continuation system.That sounds cool. If you modified the stack while the thread wasn't running, you could pass asynchronous messages to it that it would process immediately once it resumed. I don't know what might happen if it was already running on a different processor.
Just about, yes. The only difference is that continuations can only be used once. So the kernel scheduler doesn't have a reference to every continuation on the system, it holds references to every event-handler continuation that wants the processor Right Now.Would this be equivalent to having the thread sleep/wait itself (by interrupt/call etc), and then have some other process mark it as "awake" again after changing it's stack?
Precisely. Continuations alone allow the creation of cooperative concurrency systems, so I added events in to abstract over machine interrupts and enable preemptive concurrency.This sounds to me like what interrupts tend to already do, but with threaded/continuated interrupt handlers. This can be particularly useful to do above simply 'running' them when you do I/O APIC and LAPIC work.
See above. In my basic design, event handlers cannot preempt other event handlers. You could add priorities, in which case a high-priority event handler could preempt a low-priority one just like any other continuation. The high-priority handler would receive a continuation of the low-priority one. The low-priority event-handler's continuation would include the saved data naming the continuation of the non-event-handler computation it originally preempted.Say you are running x. A low priority interrupt happens, takes over. You thread it. Then a higher priority (via LAPIC) interrupt happens, and you need to go further up again... you need to preserve the state of the low priority interrupt? (they're both using the ESP0 and same registers?)
No. First of all, other CPUs have their own things to handle, including their own events. Part of the point of a continuation system is to seperate the implementation of OS-provided concurrency from the use of hardware parallelism. Each processor runs its own instruction stream without asking a master processor for work.Also, this might let another CPU pick up the low priority interrupt, simply by sending the thread's stack across?
Few have.Just some thoughts, I have never really implemented anything of this nature before.