improve bloom filter on send
[platform/upstream/glib.git] / gio / gasyncinitable.c
index e8cf8c9..259057a 100644 (file)
@@ -13,9 +13,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Alexander Larsson <alexl@redhat.com>
  */
@@ -24,6 +22,7 @@
 #include "gasyncinitable.h"
 #include "gasyncresult.h"
 #include "gsimpleasyncresult.h"
+#include "gtask.h"
 #include "glibintl.h"
 
 
@@ -48,7 +47,7 @@
  *
  * A typical implementation might look something like this:
  *
- * |[
+ * |[<!-- language="C" -->
  * enum {
  *    NOT_INITIALIZED,
  *    INITIALIZING,
  *
  *   for (l = self->priv->init_results; l != NULL; l = l->next)
  *     {
- *       GSimpleAsyncResult *simple = l->data;
+ *       GTask *task = l->data;
  *
- *       if (!self->priv->success)
- *         g_simple_async_result_set_error (simple, ...);
- *
- *       g_simple_async_result_complete (simple);
- *       g_object_unref (simple);
+ *       if (self->priv->success)
+ *         g_task_return_boolean (task, TRUE);
+ *       else
+ *         g_task_return_new_error (task, ...);
+ *       g_object_unref (task);
  *     }
  *
  *   g_list_free (self->priv->init_results);
  *                 gpointer              user_data)
  * {
  *   Foo *self = FOO (initable);
- *   GSimpleAsyncResult *simple;
+ *   GTask *task;
  *
- *   simple = g_simple_async_result_new (G_OBJECT (initable)
- *                                       callback,
- *                                       user_data,
- *                                       foo_init_async);
+ *   task = g_task_new (initable, cancellable, callback, user_data);
  *
  *   switch (self->priv->state)
  *     {
  *       case NOT_INITIALIZED:
  *         _foo_get_ready (self);
  *         self->priv->init_results = g_list_append (self->priv->init_results,
- *                                                   simple);
+ *                                                   task);
  *         self->priv->state = INITIALIZING;
  *         break;
  *       case INITIALIZING:
  *         self->priv->init_results = g_list_append (self->priv->init_results,
- *                                                   simple);
+ *                                                   task);
  *         break;
  *       case INITIALIZED:
  *         if (!self->priv->success)
- *           g_simple_async_result_set_error (simple, ...);
- *
- *         g_simple_async_result_complete_in_idle (simple);
- *         g_object_unref (simple);
+ *           g_task_return_new_error (task, ...);
+ *         else
+ *           g_task_return_boolean (task, TRUE);
+ *         g_object_unref (task);
  *         break;
  *     }
  * }
  *                  GAsyncResult         *result,
  *                  GError              **error)
  * {
- *   g_return_val_if_fail (g_simple_async_result_is_valid (result,
- *       G_OBJECT (initable), foo_init_async), FALSE);
+ *   g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);
  *
- *   if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
- *           error))
- *     return FALSE;
- *
- *   return TRUE;
+ *   return g_task_propagate_boolean (G_TASK (result), error);
  * }
  *
  * static void
@@ -165,8 +156,7 @@ g_async_initable_default_init (GAsyncInitableInterface *iface)
 /**
  * g_async_initable_init_async:
  * @initable: a #GAsyncInitable.
- * @io_priority: the <link linkend="io-priority">I/O priority</link>
- *     of the operation.
+ * @io_priority: the [I/O priority][io-priority] of the operation
  * @cancellable: optional #GCancellable object, %NULL to ignore.
  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
  * @user_data: the data to pass to callback function
@@ -257,14 +247,17 @@ g_async_initable_init_finish (GAsyncInitable  *initable,
 }
 
 static void
-async_init_thread (GSimpleAsyncResult *res,
-                  GObject            *object,
-                  GCancellable       *cancellable)
+async_init_thread (GTask        *task,
+                   gpointer      source_object,
+                   gpointer      task_data,
+                   GCancellable *cancellable)
 {
   GError *error = NULL;
 
-  if (!g_initable_init (G_INITABLE (object), cancellable, &error))
-    g_simple_async_result_take_error (res, error);
+  if (g_initable_init (G_INITABLE (source_object), cancellable, &error))
+    g_task_return_boolean (task, TRUE);
+  else
+    g_task_return_error (task, error);
 }
 
 static void
@@ -274,15 +267,14 @@ g_async_initable_real_init_async (GAsyncInitable      *initable,
                                  GAsyncReadyCallback  callback,
                                  gpointer             user_data)
 {
-  GSimpleAsyncResult *res;
+  GTask *task;
 
   g_return_if_fail (G_IS_INITABLE (initable));
 
-  res = g_simple_async_result_new (G_OBJECT (initable), callback, user_data,
-                                  g_async_initable_real_init_async);
-  g_simple_async_result_run_in_thread (res, async_init_thread,
-                                      io_priority, cancellable);
-  g_object_unref (res);
+  task = g_task_new (initable, cancellable, callback, user_data);
+  g_task_set_priority (task, io_priority);
+  g_task_run_in_thread (task, async_init_thread);
+  g_object_unref (task);
 }
 
 static gboolean
@@ -290,23 +282,28 @@ g_async_initable_real_init_finish (GAsyncInitable  *initable,
                                   GAsyncResult    *res,
                                   GError         **error)
 {
-  GSimpleAsyncResult *simple;
-
-  g_return_val_if_fail (g_simple_async_result_is_valid (res, G_OBJECT (initable),
-                                                       g_async_initable_real_init_async),
-                       FALSE);
-
-  simple = G_SIMPLE_ASYNC_RESULT (res);
-  if (g_simple_async_result_propagate_error (simple, error))
-    return FALSE;
-  return TRUE;
+  /* For backward compatibility we have to process GSimpleAsyncResults
+   * even though g_async_initable_real_init_async doesn't generate
+   * them any more.
+   */
+  if (G_IS_SIMPLE_ASYNC_RESULT (res))
+    {
+      GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
+      if (g_simple_async_result_propagate_error (simple, error))
+        return FALSE;
+      else
+        return TRUE;
+    }
+
+  g_return_val_if_fail (g_task_is_valid (res, initable), FALSE);
+
+  return g_task_propagate_boolean (G_TASK (res), error);
 }
 
 /**
  * g_async_initable_new_async:
  * @object_type: a #GType supporting #GAsyncInitable.
- * @io_priority: the <link linkend="io-priority">I/O priority</link>
- *     of the operation.
+ * @io_priority: the [I/O priority][io-priority] of the operation
  * @cancellable: optional #GCancellable object, %NULL to ignore.
  * @callback: a #GAsyncReadyCallback to call when the initialization is
  *     finished
@@ -349,8 +346,7 @@ g_async_initable_new_async (GType                object_type,
  * @object_type: a #GType supporting #GAsyncInitable.
  * @n_parameters: the number of parameters in @parameters
  * @parameters: the parameters to use to construct the object
- * @io_priority: the <link linkend="io-priority">I/O priority</link>
- *     of the operation.
+ * @io_priority: the [I/O priority][io-priority] of the operation
  * @cancellable: optional #GCancellable object, %NULL to ignore.
  * @callback: a #GAsyncReadyCallback to call when the initialization is
  *     finished
@@ -391,8 +387,7 @@ g_async_initable_newv_async (GType                object_type,
  * @first_property_name: the name of the first property, followed by
  * the value, and other property value pairs, and ended by %NULL.
  * @var_args: The var args list generated from @first_property_name.
- * @io_priority: the <link linkend="io-priority">I/O priority</link>
- *     of the operation.
+ * @io_priority: the [I/O priority][io-priority] of the operation
  * @cancellable: optional #GCancellable object, %NULL to ignore.
  * @callback: a #GAsyncReadyCallback to call when the initialization is
  *     finished
@@ -440,8 +435,8 @@ g_async_initable_new_valist_async (GType                object_type,
  * Finishes the async construction for the various g_async_initable_new
  * calls, returning the created object or %NULL on error.
  *
- * Returns: (transfer full): a newly created #GObject, or %NULL on error.
- *     Free with g_object_unref().
+ * Returns: (type GObject.Object) (transfer full): a newly created #GObject,
+ *      or %NULL on error. Free with g_object_unref().
  *
  * Since: 2.22
  */