shared: add nm_g_task_new() and nm_g_task_is_valid() helper

Note that we should always set the source-tag of our GTask.
This allows us to better assert that the user uses the right
_finish() method for the task.

The plain g_task_new() does not have a souce-tag argument. Hence, we would
always need to explicitly call g_task_set_source_tag().

Likewise, to check the source tag, we would always need to write

  g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
  g_return_val_if_fail (g_async_result_is_tagged (result, tag), FALSE);

Actually, g_async_result_is_tagged() uses the GAsyncResultIface to
call iface->is_tagged(). This has unnecessary overhead, so we should
just call g_task_get_source_tag() directly.

Add helper functions for that.
This commit is contained in:
Thomas Haller 2019-10-04 10:19:31 +02:00
parent 256ba8c4cd
commit 9059b49002

View file

@ -1256,4 +1256,30 @@ guint8 *nm_utils_hexstr2bin_alloc (const char *hexstr,
gsize required_len,
gsize *out_len);
/*****************************************************************************/
static inline GTask *
nm_g_task_new (gpointer source_object,
GCancellable *cancellable,
gpointer source_tag,
GAsyncReadyCallback callback,
gpointer callback_data)
{
GTask *task;
task = g_task_new (source_object, cancellable, callback, callback_data);
if (source_tag)
g_task_set_source_tag (task, source_tag);
return task;
}
static inline gboolean
nm_g_task_is_valid (gpointer task,
gpointer source_object,
gpointer source_tag)
{
return g_task_is_valid (task, source_object)
&& g_task_get_source_tag (task) == source_tag;
}
#endif /* __NM_SHARED_UTILS_H__ */