From: Ian Price <ianprice90@googlemail.com>
To: Vincent Bernat <bernat@luffy.cx>
Cc: guile-user@gnu.org
Subject: Re: guile and coroutines controlled from C
Date: Mon, 30 Jul 2012 05:56:43 +0100 [thread overview]
Message-ID: <877gtlvoc4.fsf@Kagami.home> (raw)
In-Reply-To: <m3pq7fn6a1.fsf@neo.luffy.cx> (Vincent Bernat's message of "Sat, 28 Jul 2012 19:29:26 +0200")
Vincent Bernat <bernat@luffy.cx> writes:
> Hi!
> I would like to add Guile scripting to an actual program to allow a user
> to write simple network-related scenarios. Those scenarios will be run
> in parallel on several entities. The application is event-driven and I
> want to hide this fact to the user. She should be able to write a
> scenario in a simple blocking style:
Hi, always happy to new uses for guile.
>
> #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.
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.
(use-modules (ice-9 q)) ;; yech, but it'll do
(define (list->q l)
(define q (make-q))
(for-each (lambda (x) (enq! q x)) l)
q)
(define (run threads)
;; simple round robin scheduler
(define *q* (list->q threads))
(while (not (q-empty? *q*))
(let ((thread (deq! *q*)))
(when (eq? 'not-done (thread))
(enq! *q* thread)))))
(define (make-thread thread)
;; NB. this might be cleaner with delimited continuations, but they
;; seem to scare people even more than continuations for some reason...
;; A thread is a thunk, that performs some computation, then returns
;; either 'not-done or 'done
(lambda ()
(call-with-current-continuation
(lambda (start) ;; capture "the stack" before routines is ran
(thread (lambda ()
(call-with-current-continuation
(lambda (current)
;; save current "stack" for thread
;; so that we continue from there next time
(set! thread current)
;; then we need to cleanup, and go back to where
;; we were before the thread began executing
(start 'not-done)))))
;; should probably mutate thread one last time, so that further
;; calls always just return 'done
'done))))
(define thread1
(make-thread
(lambda (yield)
(let loop ((i 3))
(if (zero? i)
'done
(begin
(display "in thread 1...\n")
(yield)
(loop (- i 1))))))))
(define thread2
(make-thread
(lambda (yield)
(let loop ((i 5))
(if (zero? i)
'done
(begin
(display "in thread 2...\n")
(yield)
(loop (- i 1))))))))
(define thread3
(make-thread
(lambda (yield)
(let loop ((i 4))
(if (zero? i)
'done
(begin
(display "in thread 3...\n")
(yield)
(loop (- i 1))))))))
(run (list thread1 thread2 thread3))
;; Strangely enough, I was able to write this out and have it work first
;; time, which is a first for me writing continuation code :P
~ $ guile -s /tmp/contexample.scm
in thread 1...
in thread 2...
in thread 3...
in thread 1...
in thread 2...
in thread 3...
in thread 1...
in thread 2...
in thread 3...
in thread 2...
in thread 3...
in thread 2...
~ $
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
--
Ian Price
"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"
next prev parent reply other threads:[~2012-07-30 4:56 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 [this message]
2012-07-30 5:38 ` Thien-Thi Nguyen
2012-08-01 13:15 ` Ian Price
2012-07-30 6:28 ` Vincent Bernat
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=877gtlvoc4.fsf@Kagami.home \
--to=ianprice90@googlemail.com \
--cc=bernat@luffy.cx \
--cc=guile-user@gnu.org \
/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).