Re: [Vala] [ANNOUNCE] Vala 0.5.1 - Compiler for the GObject type system



On Sun, Nov 23, 2008 at 2:22 AM, Jürg Billeter <j bitron ch> wrote:
On Sun, 2008-11-23 at 01:31 -0800, Christian Hergert wrote:
> I'm excited about the addition of yielding values.  I released GTask
> 0.1.2 last week which is an asynchronous/concurrency library for
> GObject with similar feel to twisted deferreds.  I would love to be
> able to yield tasks and have the method re-enter when the task has
> completed.

This should be possible by adding something like the following to GTask:

void
g_task_run_async (GTask *task, GAsyncReadyCallback callback, gpointer user_data)
{
   AsyncData *data = "" (AsyncData);
   data->callback = callback;
   data->user_data = user_data;
   g_task_add_callback (task, async_callback, data, NULL);
   g_task_scheduler_schedule (g_task_scheduler_get_default (), task);
}

static GValue *
async_callback (GTask *task, const GValue *result, gpointer user_data)
{
   AsyncData *data = "">    GSimpleAsyncResult *res = g_simple_async_result_new (task, data->callback, data->user_data, result);
   g_simple_async_result_complete (res);
   g_slice_free (AsyncData, data);
   return NULL;
}

GValue *
g_task_run_finish (GTask *task, GAsyncResult *res)
{
   return g_async_result_get_user_data (res);
}

In Vala this could be used as follows:

void do_something () yields {
   var task = new Task (_ => { return some_sync_operation (); });
   var result = task.run ();
   stdout.printf ("Result was %s\n", result);
}

Other API variations are possible, of course, that's just an example.

Jürg


Thats awesome!  It will definitely make it into the next release.

-- Christian


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