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



Hello,

                    في ج، 25-02-2011 عند 19:45 +0100 ، كتب Luca Dionisi:
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.
Right, but I think a MainLoop is enough for most purposes (you can make
a custom GSource for implementing whatever policy you want, although
after a quick look at the current bindings they may be unusable right
now).


Furthermore:
1. The asynchronous methods can call normal methods, but only if those
callee won't ever need to yield.

I'm not sure I understand this one.

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)
Yes, the other way is to have method B call method C synchronously, that
is start a "recursive" main loop for that call. IIRC, this was planned
to be done in vala, but it's still not implemented. You can do it
manually, but if you are using async methods in the first place, you
shouldn't need this sort of hacks.


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?

The proper way for a "pseudo-thread" (and no, I don't know the proper
way to say this) to terminate is to "return", i.e. if you use "yield" in
an async method, it is suspended, but resources it has allocated aren't
freed, so if the callback isn't called, you get a memory leak.

So you need to tell the async method to terminate, rather than "kill"
it, otherwise you're in trouble ;-) I'm not sure what's the best way to
do this, but that's what you'd need to do.

HTH,
Abderrahim




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