From: Vincent Bernat <bernat@luffy.cx>
To: Ian Price <ianprice90@googlemail.com>
Cc: guile-user@gnu.org
Subject: Re: guile and coroutines controlled from C
Date: Mon, 30 Jul 2012 08:28:26 +0200 [thread overview]
Message-ID: <m3txwplq45.fsf@neo.luffy.cx> (raw)
In-Reply-To: <877gtlvoc4.fsf@Kagami.home> (Ian Price's message of "Mon, 30 Jul 2012 05:56:43 +0100")
❦ 30 juillet 2012 06:56 CEST, Ian Price <ianprice90@googlemail.com> :
>> #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
next prev parent reply other threads:[~2012-07-30 6:28 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-07-28 17:29 guile and coroutines controlled from C Vincent Bernat
2012-07-30 4:56 ` Ian Price
2012-07-30 5:38 ` Thien-Thi Nguyen
2012-08-01 13:15 ` Ian Price
2012-07-30 6:28 ` Vincent Bernat [this message]
2012-08-01 13:13 ` Ian Price
2012-08-02 17:28 ` Vincent Bernat
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m3txwplq45.fsf@neo.luffy.cx \
--to=bernat@luffy.cx \
--cc=guile-user@gnu.org \
--cc=ianprice90@googlemail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).