[Vala] Thoughts on asynchronous methods



I did some thinking on the subject of asynchronous (aka "yielding") methods. I'd like to share and discuss some of my ideas here.

--------------------------------------------------------------
1. "yields" keyword is far from intuitive
--------------------------------------------------------------

Not so long ago, I was looking at the Vala sources, at that time unaware of the experimental support for asynchronous methods. I caught a glimpse of keyword "yields" and thought, "What the hell is that??". This first impression stayed with me ever since. It may be that I'm not a native english speaker, but "yield" isn't exactly your friendly neighborhood async-related word. Apart from that, the usage itself goes quite rough against my fur. Reason:

  public virtual void method (int arg) yields throws IOError;

So, okay, it yields. Yields what? Yields throwing error? I've always figured that "throws" is in the form of appended verb because it has an argument. So does "ensures" and "requires". Where does "yields" fit in?

Personally, I would find following much more appropriate:

    public virtual async void method (int arg) throws IOError;

But I guess that's a matter of taste.

--------------------------------------------------------
2. purely asynchronous methods
--------------------------------------------------------

There are actions that can't be represented synchronously and methods where synchronous variant needs to be written separately. 'nuff said. This could perhaps be handled using specialized [Async] attribute with appropriate parameters.

-----------------------------------------------------------------------
3. stopping execution until a callback yields
-----------------------------------------------------------------------

There are actions that can be described as "waiting for callback". For that to work correctly, programmer needs to make sure all the connected callbacks are disconnected as the method returns. If there is mechanism to "pause execution" (from the programmers POV) until some connected callback yields, it would allow doing this cleanup in a finally block. I have a faint feeling that bare "yield" keyword already has some semantics, but for the sake of demonstration:

    public int method () yields throws Error{
        try {
            signal1.connect ((x) => yield return x;);
signal2.connect ((x) => yield throw new Error ("Error occured: %s", x);); // the first one to be executed decides the return condition

            yield;   // wait and treat yields as if they were called here
        }
        finally {
            signal1.disconnect ();
            signal2.disconnect ();
        }
    }

This is also a prime example of method that can't be translated into a synchronous version.

-------------------------------------------
4. co-execution operator
-------------------------------------------

It is my belief that Vala doesn't recognize the comma operator the way C does, right? Well, this offers a nice opportunity to utilize it.

Example worth a thousand words /ahem/:

    public void method () yields {
        ....

async1 (); // async1 is executed, finished, then async2 is executed
        async2 ();

async1 (), async2 (); // async1 and async2 are executed simultaneously; when both return, execution continues

// advanced example - async1 and async2 execute in order, so do async3 and async4, but the chains execute simultaneously
        { async1 (); async2 (); }, { async3 (); async4 (); }

As you can see, in contrast with C understanding of comma operator, this one forms a statement just like semicolon, differing just in the way it's executed. So that's the general idea. Problem I see here is that such concurrently executed statements may not throw (problem: which throw to forward up).



So, that's it for now. I think I have some more ideas, but let's process these first (I can't remember the rest now anyway). I'm looking forward to your opinions.




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