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: Ian Price
Subject: Re: guile and coroutines controlled from C
Date: Mon, 30 Jul 2012 05:56:43 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux)

Vincent Bernat <address@hidden> 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"



reply via email to

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