guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: guile and coroutines controlled from C


From: Vincent Bernat
Subject: Re: guile and coroutines controlled from C
Date: Mon, 30 Jul 2012 08:28:26 +0200
User-agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.1 (gnu/linux)

 ❦ 30 juillet 2012 06:56 CEST, Ian Price <address@hidden> :

>> #v+
>> (let ((v1 (do-something-blocking "arg1" "arg2"))
>>       (v2 (do-something-blocking "arg3" "arg4")))
>>  (when (< (+ v1 v2) 10) (...)))
>> #v-
>>
>> `do-something-blocking` is a registered function written in C. I want to
>> be able to pause the execution inside this function and then resume it
>> later (when I my program got the data wanted by the user). In the
>> meantime, another "cooperative thread" may be executed.
>>
>> In Lua, this is something that can be done with coroutines which can be
>> yield and resumed from C. In Guile, there are continuations but I have
>> hard time to understand how this should work from C.
>>
>> What would be the equivalent of this in Guile?
>>  http://kristianrumberg.wordpress.com/2010/11/21/135/
>
> So, is it a C function you want to pause and resume, or a scheme one?
> Your first statement implies the former, but that example you pointed to
> implies you want scheme coroutines, that are callable from C. Both
> should be possible, but I'd need to do some experimentation before
> providing help with the former, as I'm not entirely sure about the
> interaction C and continuations in the vm.

Hum. Both I think. :) I want a user to provide a scheme function that
will be run several times in parallel against dozens of targets with
cooperative multithreading. The user does not have to know how
cooperation works. It just uses some primitives (implemented in C) that
may yield control to another thread. The C application is event-driven.

#v+
(grab-something-from-network device arg1 arg2)
(do-some-computation-with-result)
(grab-something-else-from-network device arg3 arg4)
(grab-something-else device arg5)
(some-computation)
#v-

The `grab-something-*` functions will be implemented in C. They will
pause the current thread and resume it when data get available. In
Python, there is something called "greenlets" that use the same concept.

> As for scheme coroutines, I think Wingo does that in one of the
> asynchronous IO branches, but I'm not sure if it exported for general
> use.
>
> A quick hack, I wrote is posted below. Threads are represented by thunks
> and are ran by calling said thunk. They would be called from C in the
> same way as any other function. In practice, it would be cleaner to
> create a new record type for these.
[...]

Your example is great! It fits exactly what I need. Since I am still
pretty new, I need some time to understand each line but this seems a
very good start for what I want to do. I just need to translate some
parts in C since the event loop is here. From what I understand, only
the `run` function needs to be moved in C. The remaining of the code can
be hidden in functions or macros (but it would help if the `yield` part
could be translated in C).

Just one other question.

> (define thread1
>   (make-thread
>    (lambda (yield)
>      (let loop ((i 3))
>        (if (zero? i)
>            'done
>            (begin
>              (display "in thread 1...\n")
>              (yield)
>              (loop (- i 1))))))))

After `yield`, the routine is resumed when scheduled by the previous
`run` function. In my case, yielding happened because I was waiting for data
from the network. How could I return the appropriate data on resume? I
could call some function that will provide the revelant data from some
cache just after yield:

#v+
(yield)
(some-computation-with (grab-result-local-to-this-thread))
#v-

However, maybe `yield` could return the result?

> I hope that gives you a head start. If I've confused you more, I
> can only apologise. If you aren't familiar with continuations at all, 
> you could try reading http://tmp.barzilay.org/cont.txt

Thanks for your help. I am still a bit confused but less than a day
before. I just need some time to experiment a bit.
-- 
panic("Aarggh: attempting to free lock with active wait queue - shoot Andy");
        2.0.38 /usr/src/linux/fs/locks.c



reply via email to

[Prev in Thread] Current Thread [Next in Thread]