Design idea: Handle to async methods

Problem

It's impossible to wait for an async method to finish without doing some sleep(), and we would never know how long to sleep anyway

Solution

  • Introduce TnyAsyncJoinable handle to be returned (or passed by reference) when async methods are called.
  • A tny_async_joinable_join (handle) could wait until the async thread had finished.

Considerations

There are two ways to implement such a handle. The second one has the advantage of allowing NULL values to be passed for the ones that don't care about the handle. In the first one, the handle would always have to be unreferenced.

  • TnyAsyncJoinable* my_method_async (TnyMyType *self, TnyMyMethodCallback callback);
  • void my_method_async (TnyMyType *self, TnyMyMethodCallback callback, TnyAsyncJoinable *handle);

For example

static void
callback (TnyFolder *folder, gboolean cancelled, GError **err, gpointer user_data)
{
   printf ("ABC\n");
}

static void
status_callback (TnyFolder *folder, const gchar *what, gint status, gint oftotal, gpointer user_data)
{
}

static void
my_routine (TnyAsyncFolder *folder, gpointer user_data)
{
  TnyAsyncJoinable *joinable = NULL;
 
  tny_folder_refresh_async (folder, callback, status_callback, user_data, &joinable);

  /* joining here is indeed pointless (you could use tny_folder_refresh in stead).
   * It's only used as an example here. The purpose of joining is to synchronize
   * (with) asynchronous events.*/

  tny_async_joinable_join (joinable);

  printf ("DEF\n");

  g_object_unref (G_OBJECT (joinable));
}