Re: [Vala] Coroutines in vala: libtask, pthread or simply async?



Hi all

I am still doubtful about what is the best choice for my needs, but at
least I figured out how to use the async methods feature of vala.

Perhaps this feature could suffice.
If I get it right I may do something like this:

1. In a synchronous method the app instantiate some sort of scheduler.
It may be a MainLoop or something else that I can implement by myself.
2. In a synchronous method the app starts some asynchronous methods
with xyz.begin().
3. These methods start to run. After some operations they can yield to
the caller, but before to do that they register with the scheduler
their continuation using xyz.callback.
4. The caller receives the flow control back and it starts the scheduler's loop.
5. The scheduler (with its logic, e.g. round robin) calls the methods
that have been registered with it. When no more methods have been
scheduled it returns.
6. The caller of the scheduler's loop gets the flow control again and
it can exit the app.

Furthermore:
1. The asynchronous methods can call normal methods, but only if those
callee won't ever need to yield.
2. An asynchronous method can call another async method with yield. In
this way it will "wait" for the return value.
3. A method (sync or async) can call an async method with begin(). In
this way it will, say, launch another independent pseudo-thread.
4. If async method A wants to call method B and B wants to call async
method C and then pass the return value to A, then B has to be async
and all the calls have to use the yield keyword. (at least it seems so
to me, for the tests I did)

Is this [one of] the way that this feature is supposed to be used?

There is one thing I would need to be able to do.
When some code starts a new async call with begin() (I would call this
"launching a pseudo-thread", or is there some better expression?) I
would like that code to be able, in future, to tell the scheduler to
kill that pseudo-thread.
It should work like this:
1. a pseudo-thread A creates a new thread with xyz.begin()
2. xyz might call some other async method with yield qwe()
3. qwe wants to let other tasks to work; so it registers qwe.callback
with the scheduler and then yields.
4. the scheduler eventually gives the control back to the pseudo-thread A.
5. A asks the scheduler to "kill" the psuedo-thread xyz, that it created.
6. How can the scheduler know that in order to kill xyz it just has to
remove qwe.callback from its queue?

Answer to the last point.

TIA
--Luca

On Wed, Feb 23, 2011 at 11:01 PM, Luca Dionisi <luca dionisi gmail com> wrote:
I just came from reading the tutorial at http://live.gnome.org/Vala/Tutorial
It is very comprehensive, kudos to the authors.

I don't understand, though, the way of using the async methods as
explained there.
More precisely I don't understand whether this feature is what I am looking for.

I am in need of something similar to the tasklet concept of python
stackless, if you know.
That is, a way of scheduling several (a lot of) routines which will
cooperatively run in pseudo-parallel. All in a single o.s. thread.
Co-routines[1].

Is this feature of GIO, which async methods in vala are based upon, an
approach to do what I need?
Or do I really want to look at gnu portable thread[2]? And if so,
anyone already used it in vala?
Or is libtask[3] the only real solution?

--Luca

[1] http://en.wikipedia.org/wiki/Coroutine
[2] http://www.gnu.org/software/pth/
[3] http://swtch.com/libtask/




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