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: Wed, 01 Aug 2012 14:13:42 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux)

Vincent Bernat <address@hidden> writes:

> 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).
Well, it all depends on the exact API you want to provide/code to. So,
let's take your "do-something-blocking" function. One way to do this is
to have the yield function be a piece of local state that is only
available when the "thread" is running, for that I'd use a parameter, or
fluid variable. (see the sections 6.21.7/6.21.8 of the manual). Then
these functions would query that, and call the yield function to return,
after registering that the work needs to be done in a different thread
(or however you plan on doing this). I'd want the thread to be known to
the function, so that it could store, say store the return value in it,
so that you don't need to explicitly provide this when you resume. You
probably also want to provide a flag in the thread data type saying
whether or not it could be resumed.

Like I say, it all depends on the API you want to code to. I could whip
up some example code that does the above if you'd like.

> 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?
Right, when you invoke a continuation, you actually give it a value,
which is used as the return value for the call. I actually make use of
this in the code above, so that the return value of the threads function
returns 'not-done. In this case, when you want a thread to return a
value you just call that thread variable with the value. In the example
I posted, the return value is actually always the yield
function. Though, some rewriting could allow you to return other values.

-- 
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]