[gupnp-igd] Change the API to not sure GMainContext directly like before, but instead force the same GMainContex



commit 1c51ad2a9520c9bf9b0624eb46319a8ab9ff83f0
Author: Olivier CrÃte <olivier crete collabora com>
Date:   Fri Nov 4 23:08:08 2011 +0000

    Change the API to not sure GMainContext directly like before,
    but instead force the same GMainContext to be pushed on the thread
    for any call to the API.

 libgupnp-igd/gupnp-simple-igd-thread.c |    4 +-
 libgupnp-igd/gupnp-simple-igd.c        |   80 +++----------------------------
 libgupnp-igd/gupnp-simple-igd.h        |    2 +-
 tests/examples/test.c                  |    3 +-
 tests/gtest/gupnp-simple-igd.c         |   26 +++++++----
 5 files changed, 30 insertions(+), 85 deletions(-)
---
diff --git a/libgupnp-igd/gupnp-simple-igd-thread.c b/libgupnp-igd/gupnp-simple-igd-thread.c
index da89b1b..5782a1c 100644
--- a/libgupnp-igd/gupnp-simple-igd-thread.c
+++ b/libgupnp-igd/gupnp-simple-igd-thread.c
@@ -295,8 +295,6 @@ gupnp_simple_igd_thread_constructor (GType type,
 
   self = GUPNP_SIMPLE_IGD_THREAD_CAST (obj);
 
-  g_object_set (self, "main-context", self->priv->context, NULL);
-
   return obj;
 }
 
@@ -306,8 +304,10 @@ gupnp_simple_igd_thread_constructed (GObject *object)
   GUPnPSimpleIgdThread *self = GUPNP_SIMPLE_IGD_THREAD_CAST (object);
   struct thread_data *data = g_slice_new0 (struct thread_data);
 
+  g_main_context_push_thread_default (self->priv->context);
   if (G_OBJECT_CLASS (gupnp_simple_igd_thread_parent_class)->constructed)
     G_OBJECT_CLASS (gupnp_simple_igd_thread_parent_class)->constructed (object);
+  g_main_context_pop_thread_default (self->priv->context);
 
   g_atomic_int_set (&data->refcount, 2);
 
diff --git a/libgupnp-igd/gupnp-simple-igd.c b/libgupnp-igd/gupnp-simple-igd.c
index 4bb922a..5e84f1e 100644
--- a/libgupnp-igd/gupnp-simple-igd.c
+++ b/libgupnp-igd/gupnp-simple-igd.c
@@ -29,6 +29,10 @@
  * It implements the basic functionalities to map ports to external ports.
  * It also allows implementations to know the external port from the router's
  * perspective.
+ *
+ * Every call to this object, including its creation, MUST always be done
+ * using the same thread local #GMainContext pushed via
+ * g_main_context_push_thread_default();
  */
 
 
@@ -44,8 +48,6 @@
 
 struct _GUPnPSimpleIgdPrivate
 {
-  GMainContext *main_context;
-
   GUPnPContextManager *gupnp_context_manager;
 
   GPtrArray *service_proxies;
@@ -97,13 +99,6 @@ enum
   LAST_SIGNAL
 };
 
-/* props */
-enum
-{
-  PROP_0,
-  PROP_MAIN_CONTEXT
-};
-
 
 static guint signals[LAST_SIGNAL] = { 0 };
 
@@ -119,10 +114,6 @@ G_DEFINE_TYPE (GUPnPSimpleIgd, gupnp_simple_igd, G_TYPE_OBJECT);
 static void gupnp_simple_igd_constructed (GObject *object);
 static void gupnp_simple_igd_dispose (GObject *object);
 static void gupnp_simple_igd_finalize (GObject *object);
-static void gupnp_simple_igd_get_property (GObject *object, guint prop_id,
-    GValue *value, GParamSpec *pspec);
-static void gupnp_simple_igd_set_property (GObject *object, guint prop_id,
-    const GValue *value, GParamSpec *pspec);
 
 static void gupnp_simple_igd_gather (GUPnPSimpleIgd *self,
     struct Proxy *prox);
@@ -163,19 +154,10 @@ gupnp_simple_igd_class_init (GUPnPSimpleIgdClass *klass)
   gobject_class->constructed = gupnp_simple_igd_constructed;
   gobject_class->dispose = gupnp_simple_igd_dispose;
   gobject_class->finalize = gupnp_simple_igd_finalize;
-  gobject_class->set_property = gupnp_simple_igd_set_property;
-  gobject_class->get_property = gupnp_simple_igd_get_property;
 
   klass->add_port = gupnp_simple_igd_add_port_real;
   klass->remove_port = gupnp_simple_igd_remove_port_real;
 
-  g_object_class_install_property (gobject_class,
-      PROP_MAIN_CONTEXT,
-      g_param_spec_pointer ("main-context",
-          "The GMainContext to use",
-          "This GMainContext will be used for all async activities",
-          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
-
   /**
    * GUPnPSimpleIgd::mapped-external-port
    * @self: #GUPnPSimpleIgd that emitted the signal
@@ -191,7 +173,6 @@ gupnp_simple_igd_class_init (GUPnPSimpleIgdClass *klass)
    *
    * This signal means that an IGD has been found that that adding a port
    * mapping has succeeded.
-   *
    */
   signals[SIGNAL_MAPPED_EXTERNAL_PORT] = g_signal_new ("mapped-external-port",
       G_TYPE_FROM_CLASS (klass),
@@ -422,8 +403,6 @@ gupnp_simple_igd_finalize (GObject *object)
 {
   GUPnPSimpleIgd *self = GUPNP_SIMPLE_IGD_CAST (object);
 
-  g_main_context_unref (self->priv->main_context);
-
   g_warn_if_fail (self->priv->mappings->len == 0);
   g_ptr_array_free (self->priv->mappings, TRUE);
 
@@ -431,43 +410,6 @@ gupnp_simple_igd_finalize (GObject *object)
 }
 
 static void
-gupnp_simple_igd_get_property (GObject *object, guint prop_id,
-    GValue *value, GParamSpec *pspec)
-{
-  GUPnPSimpleIgd *self = GUPNP_SIMPLE_IGD_CAST (object);
-
-  switch (prop_id) {
-    case PROP_MAIN_CONTEXT:
-      g_value_set_pointer (value, self->priv->main_context);
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
-  }
-
-}
-
-static void
-gupnp_simple_igd_set_property (GObject *object, guint prop_id,
-    const GValue *value, GParamSpec *pspec)
-{
-  GUPnPSimpleIgd *self = GUPNP_SIMPLE_IGD_CAST (object);
-
-  switch (prop_id) {
-    case PROP_MAIN_CONTEXT:
-      if (!self->priv->main_context && g_value_get_pointer (value))
-      {
-        self->priv->main_context = g_value_get_pointer (value);
-        g_main_context_ref (self->priv->main_context);
-      }
-      break;
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      break;
-  }
-}
-
-static void
 _cp_service_avail (GUPnPControlPoint *cp,
     GUPnPServiceProxy *proxy,
     GUPnPSimpleIgd *self)
@@ -559,11 +501,7 @@ gupnp_simple_igd_constructed (GObject *object)
   GUPnPSimpleIgd *self = GUPNP_SIMPLE_IGD_CAST (object);
   SoupSession *session;
 
-  if (!self->priv->main_context)
-    self->priv->main_context = g_main_context_ref (g_main_context_default ());
-
-  self->priv->gupnp_context_manager =
-    gupnp_context_manager_new (self->priv->main_context, 0);
+  self->priv->gupnp_context_manager = gupnp_context_manager_create (0);
 
   g_signal_connect (self->priv->gupnp_context_manager, "context-available",
       G_CALLBACK (_context_available), self);
@@ -574,7 +512,6 @@ gupnp_simple_igd_constructed (GObject *object)
 
 /**
  * gupnp_simple_igd_new:
- * @main_context: the #GMainContext to use (may be NULL for the default
  * main context)
  *
  * This creates a new #GUPnpSimpleIgd object using the special GMainContext
@@ -583,10 +520,9 @@ gupnp_simple_igd_constructed (GObject *object)
  */
 
 GUPnPSimpleIgd *
-gupnp_simple_igd_new (GMainContext *main_context)
+gupnp_simple_igd_new (void)
 {
-  return g_object_new (GUPNP_TYPE_SIMPLE_IGD,
-      "main-context", main_context, NULL);
+  return g_object_new (GUPNP_TYPE_SIMPLE_IGD, NULL);
 }
 
 
@@ -773,7 +709,7 @@ _service_proxy_added_port_mapping (GUPnPServiceProxy *proxy,
         g_timeout_source_new_seconds (pm->mapping->lease_duration / 2);
       g_source_set_callback (pm->renew_src,
           _renew_mapping_timeout, pm, NULL);
-      g_source_attach (pm->renew_src, self->priv->main_context);
+      g_source_attach (pm->renew_src, g_main_context_get_thread_default ());
     }
   }
   else
diff --git a/libgupnp-igd/gupnp-simple-igd.h b/libgupnp-igd/gupnp-simple-igd.h
index 1b6118d..f9d6b5a 100644
--- a/libgupnp-igd/gupnp-simple-igd.h
+++ b/libgupnp-igd/gupnp-simple-igd.h
@@ -113,7 +113,7 @@ GQuark gupnp_simple_igd_error_quark (void);
 GType gupnp_simple_igd_get_type (void);
 
 GUPnPSimpleIgd *
-gupnp_simple_igd_new (GMainContext *main_context);
+gupnp_simple_igd_new (void);
 
 void
 gupnp_simple_igd_add_port (GUPnPSimpleIgd *self,
diff --git a/tests/examples/test.c b/tests/examples/test.c
index 707eca2..178a017 100644
--- a/tests/examples/test.c
+++ b/tests/examples/test.c
@@ -69,7 +69,8 @@ main (int argc, char **argv)
   ctx = g_main_context_new ();
   loop = g_main_loop_new (ctx, FALSE);
 
-  igd = gupnp_simple_igd_new (ctx);
+  g_main_context_push_thread_default (ctx);
+  igd = gupnp_simple_igd_new ();
 
   g_signal_connect (igd, "mapped-external-port",
       G_CALLBACK (_mapped_external_port),
diff --git a/tests/gtest/gupnp-simple-igd.c b/tests/gtest/gupnp-simple-igd.c
index 0992c09..a5c0822 100644
--- a/tests/gtest/gupnp-simple-igd.c
+++ b/tests/gtest/gupnp-simple-igd.c
@@ -55,7 +55,7 @@ gchar *invalid_ip = NULL;
 static void
 test_gupnp_simple_igd_new (void)
 {
-  GUPnPSimpleIgd *igd = gupnp_simple_igd_new (NULL);
+  GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
   GUPnPSimpleIgdThread *igdthread = gupnp_simple_igd_thread_new ();
   GUPnPSimpleIgdThread *igdthread1 = gupnp_simple_igd_thread_new ();
 
@@ -249,7 +249,9 @@ run_gupnp_simple_igd_test (GMainContext *mainctx, GUPnPSimpleIgd *igd,
   GUPnPDeviceInfo *subdev2;
   const gchar *xml_path = ".";
 
-  context = gupnp_context_new (mainctx, NULL, 0, NULL);
+  if (mainctx)
+    g_main_context_push_thread_default (mainctx);
+  context = gupnp_context_new (NULL, NULL, 0, NULL);
   g_assert (context);
 
   if (g_getenv ("XML_PATH"))
@@ -314,6 +316,8 @@ run_gupnp_simple_igd_test (GMainContext *mainctx, GUPnPSimpleIgd *igd,
   g_main_loop_run (loop);
   g_main_loop_unref (loop);
 
+  if (mainctx)
+    g_main_context_pop_thread_default (mainctx);
   g_object_unref (context);
 
 }
@@ -321,7 +325,7 @@ run_gupnp_simple_igd_test (GMainContext *mainctx, GUPnPSimpleIgd *igd,
 static void
 test_gupnp_simple_igd_default_ctx (void)
 {
-  GUPnPSimpleIgd *igd = gupnp_simple_igd_new (NULL);
+  GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
 
   run_gupnp_simple_igd_test (NULL, igd, INTERNAL_PORT);
   g_object_unref (igd);
@@ -331,7 +335,11 @@ static void
 test_gupnp_simple_igd_custom_ctx (void)
 {
   GMainContext *mainctx = g_main_context_new ();
-  GUPnPSimpleIgd *igd = gupnp_simple_igd_new (mainctx);
+  GUPnPSimpleIgd *igd;
+
+  g_main_context_push_thread_default (mainctx);
+  igd = gupnp_simple_igd_new ();
+  g_main_context_pop_thread_default (mainctx);
 
   run_gupnp_simple_igd_test (mainctx, igd, INTERNAL_PORT);
   g_object_unref (igd);
@@ -354,7 +362,7 @@ test_gupnp_simple_igd_thread (void)
 static void
 test_gupnp_simple_igd_random_no_conflict (void)
 {
-  GUPnPSimpleIgd *igd = gupnp_simple_igd_new (NULL);
+  GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
 
   run_gupnp_simple_igd_test (NULL, igd, 0);
   g_object_unref (igd);
@@ -364,7 +372,7 @@ test_gupnp_simple_igd_random_no_conflict (void)
 static void
 test_gupnp_simple_igd_random_conflict (void)
 {
-  GUPnPSimpleIgd *igd = gupnp_simple_igd_new (NULL);
+  GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
 
   return_conflict = TRUE;
   run_gupnp_simple_igd_test (NULL, igd, 0);
@@ -376,7 +384,7 @@ test_gupnp_simple_igd_random_conflict (void)
 static void
 test_gupnp_simple_igd_dispose_removes (void)
 {
-  GUPnPSimpleIgd *igd = gupnp_simple_igd_new (NULL);
+  GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
 
   dispose_removes = TRUE;
   run_gupnp_simple_igd_test (NULL, igd, INTERNAL_PORT);
@@ -400,7 +408,7 @@ test_gupnp_simple_igd_dispose_removes_thread (void)
 static void
 test_gupnp_simple_igd_invalid_ip(void)
 {
-  GUPnPSimpleIgd *igd = gupnp_simple_igd_new (NULL);
+  GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
 
   invalid_ip = "asdas";
   run_gupnp_simple_igd_test (NULL, igd, INTERNAL_PORT);
@@ -410,7 +418,7 @@ test_gupnp_simple_igd_invalid_ip(void)
 static void
 test_gupnp_simple_igd_empty_ip(void)
 {
-  GUPnPSimpleIgd *igd = gupnp_simple_igd_new (NULL);
+  GUPnPSimpleIgd *igd = gupnp_simple_igd_new ();
 
   invalid_ip = "";
   run_gupnp_simple_igd_test (NULL, igd, INTERNAL_PORT);



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