[farsight2/master] Add main context construction/destruction aparatus

Olivier Crête olivier.crete at collabora.co.uk
Tue Dec 23 15:24:28 PST 2008


---
 fs-upnp-simple-igd.c |  101 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 91 insertions(+), 10 deletions(-)

diff --git a/fs-upnp-simple-igd.c b/fs-upnp-simple-igd.c
index 0d60a27..b2bbc8d 100644
--- a/fs-upnp-simple-igd.c
+++ b/fs-upnp-simple-igd.c
@@ -28,7 +28,11 @@
 
 struct _FsUpnpSimpleIgdPrivate
 {
+  GMutex *mutex;
+
+  GMainLoop *loop;
   GMainContext *context;
+  GThread *thread;
 };
 
 /* signals */
@@ -51,42 +55,42 @@ enum
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), FS_TYPE_UPNP_SIMPLE_IGD,             \
    FsUpnpSimpleIgdPrivate))
 
+
+
+#define FS_UPNP_SIMPLE_IGD_LOCK(self) \
+  g_mutex_lock ((self)->priv->mutex)
+#define FS_UPNP_SIMPLE_IGD_UNLOCK(self) \
+  g_mutex_unlock ((self)->priv->mutex)
+
+
 G_DEFINE_TYPE (FsUpnpSimpleIgd, fs_upnp_simple_igd, G_TYPE_OBJECT);
 
 
 static void fs_upnp_simple_igd_dispose (GObject *object);
 static void fs_upnp_simple_igd_finalize (GObject *object);
-static void fs_upnp_simple_igd_constructed (GObject *object);
-
 static void fs_upnp_simple_igd_get_property (GObject *object, guint prop_id,
     GValue *value, GParamSpec *pspec);
 static void fs_upnp_simple_igd_set_property (GObject *object, guint prop_id,
     const GValue *value, GParamSpec *pspec);
 
-
 static void
 fs_upnp_simple_igd_class_init (FsUpnpSimpleIgdClass *klass)
 {
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
-  gobject_class->constructed = fs_upnp_simple_igd_constructed;
   gobject_class->dispose = fs_upnp_simple_igd_dispose;
   gobject_class->finalize = fs_upnp_simple_igd_finalize;
   gobject_class->set_property = fs_upnp_simple_igd_set_property;
   gobject_class->get_property = fs_upnp_simple_igd_get_property;
+
 }
 
 static void
 fs_upnp_simple_igd_init (FsUpnpSimpleIgd *self)
 {
   self->priv = FS_UPNP_SIMPLE_IGD_GET_PRIVATE (self);
-}
 
-static void
-fs_upnp_simple_igd_constructed (GObject *object)
-{
-  if (G_OBJECT_CLASS (fs_upnp_simple_igd_parent_class)->constructed)
-    G_OBJECT_CLASS (fs_upnp_simple_igd_parent_class)->constructed (object);
+  self->priv->mutex = g_mutex_new ();
 }
 
 static void
@@ -102,6 +106,8 @@ fs_upnp_simple_igd_finalize (GObject *object)
 
   g_main_context_unref (self->priv->context);
 
+  g_mutex_free (self->priv->mutex);
+
   G_OBJECT_CLASS (fs_upnp_simple_igd_parent_class)->finalize (object);
 }
 
@@ -118,4 +124,79 @@ fs_upnp_simple_igd_set_property (GObject *object, guint prop_id,
 }
 
 
+FsUpnpSimpleIgd *
+fs_upnp_simple_igd_new (GMainContext *context)
+{
+  FsUpnpSimpleIgd *self = g_object_new (FS_TYPE_UPNP_SIMPLE_IGD, NULL);
+
+  self->priv->context = g_main_context_ref (context);
+
+  return self;
+}
+
+static gpointer
+fs_upnp_simple_igd_loop_func (gpointer data)
+{
+  FsUpnpSimpleIgd *self = data;
+
+  FS_UPNP_SIMPLE_IGD_LOCK (self);
+  self->priv->loop = g_main_loop_new (self->priv->context, FALSE);
+  FS_UPNP_SIMPLE_IGD_UNLOCK (self);
+
+  g_main_loop_run (self->priv->loop);
+
+  FS_UPNP_SIMPLE_IGD_LOCK (self);
+  g_main_loop_unref (self->priv->loop);
+  self->priv->loop = NULL;
+  FS_UPNP_SIMPLE_IGD_UNLOCK (self);
 
+  return NULL;
+}
+
+FsUpnpSimpleIgd *
+fs_upnp_simple_igd_new_with_thread ()
+{
+  FsUpnpSimpleIgd *self = g_object_new (FS_TYPE_UPNP_SIMPLE_IGD, NULL);
+
+  self->priv->context = g_main_context_new ();
+
+  self->priv->thread = g_thread_create (fs_upnp_simple_igd_loop_func,
+      self, TRUE, NULL);
+
+  if (!self->priv->thread)
+  {
+    g_object_unref (self);
+    self = NULL;
+  }
+
+  return self;
+}
+
+
+void
+fs_upnp_simple_igd_stop (FsUpnpSimpleIgd *self)
+{
+  GSource *source;
+  GMainLoop *loop;
+
+  if (!self->priv->thread)
+    return;
+
+  source = g_idle_source_new ();
+
+
+  FS_UPNP_SIMPLE_IGD_LOCK (self);
+  loop = g_main_loop_ref (self->priv->loop);
+  FS_UPNP_SIMPLE_IGD_UNLOCK (self);
+
+  g_source_set_callback (source,
+      (GSourceFunc) g_main_loop_quit,
+      loop,
+      NULL);
+
+  g_source_attach (source, self->priv->context);
+
+  g_thread_join (self->priv->thread);
+
+  g_main_loop_unref (loop);
+}
-- 
1.5.6.5




More information about the farsight-commits mailing list