[farsight2/master] Add thread

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


---
 fs-upnp-simple-igd-thread.c |   49 ++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/fs-upnp-simple-igd-thread.c b/fs-upnp-simple-igd-thread.c
index 4520f57..247e9c5 100644
--- a/fs-upnp-simple-igd-thread.c
+++ b/fs-upnp-simple-igd-thread.c
@@ -33,17 +33,19 @@ struct _FsUpnpSimpleIgdThreadPrivate
 };
 
 
-
-
 #define FS_UPNP_SIMPLE_IGD_THREAD_GET_PRIVATE(o)                        \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), FS_TYPE_UPNP_SIMPLE_IGD_THREAD,    \
    FsUpnpSimpleIgdThreadPrivate))
 
+#define FS_UPNP_SIMPLE_IGD_THREAD_LOCK(o)   g_mutex_lock ((o)->priv->mutex)
+#define FS_UPNP_SIMPLE_IGD_THREAD_UNLOCK(o) g_mutex_unlock ((o)->priv->mutex)
+
 
 G_DEFINE_TYPE (FsUpnpSimpleIgdThread, fs_upnp_simple_igd_thread,
     FS_TYPE_UPNP_SIMPLE_IGD);
 
 static void fs_upnp_simple_igd_thread_constructed (GObject *object);
+static void fs_upnp_simple_igd_thread_dispose (GObject *object);
 static void fs_upnp_simple_igd_thread_finalize (GObject *object);
 
 
@@ -55,6 +57,7 @@ fs_upnp_simple_igd_thread_class_init (FsUpnpSimpleIgdThreadClass *klass)
   g_type_class_add_private (klass, sizeof (FsUpnpSimpleIgdThreadPrivate));
 
   gobject_class->constructed = fs_upnp_simple_igd_thread_constructed;
+  gobject_class->dispose = fs_upnp_simple_igd_thread_dispose;
   gobject_class->finalize = fs_upnp_simple_igd_thread_finalize;
 }
 
@@ -68,6 +71,23 @@ fs_upnp_simple_igd_thread_init (FsUpnpSimpleIgdThread *self)
   self->priv->context = g_main_context_new ();
 }
 
+
+static void
+fs_upnp_simple_igd_thread_dispose (GObject *object)
+{
+  FsUpnpSimpleIgdThread *self = FS_UPNP_SIMPLE_IGD_THREAD_CAST (object);
+
+  FS_UPNP_SIMPLE_IGD_THREAD_LOCK (self);
+  if (self->priv->loop)
+    g_main_loop_quit (self->priv->loop);
+  FS_UPNP_SIMPLE_IGD_THREAD_UNLOCK (self);
+
+  g_thread_join (self->priv->thread);
+  self->priv->thread = NULL;
+
+  G_OBJECT_CLASS (fs_upnp_simple_igd_thread_parent_class)->dispose (object);
+}
+
 static void
 fs_upnp_simple_igd_thread_finalize (GObject *object)
 {
@@ -79,10 +99,33 @@ fs_upnp_simple_igd_thread_finalize (GObject *object)
   G_OBJECT_CLASS (fs_upnp_simple_igd_thread_parent_class)->finalize (object);
 }
 
+static gpointer
+thread_func (gpointer data)
+{
+  FsUpnpSimpleIgdThread *self = data;
+  GMainLoop *loop = g_main_loop_new (self->priv->context, FALSE);
+  FS_UPNP_SIMPLE_IGD_THREAD_LOCK (self);
+  self->priv->loop = loop;
+  FS_UPNP_SIMPLE_IGD_THREAD_UNLOCK (self);
+
+  g_main_loop_run (loop);
+
+  FS_UPNP_SIMPLE_IGD_THREAD_LOCK (self);
+  self->priv->loop = NULL;
+  FS_UPNP_SIMPLE_IGD_THREAD_UNLOCK (self);
+
+  g_main_loop_unref (loop);
+
+  return NULL;
+}
+
 static void
 fs_upnp_simple_igd_thread_constructed (GObject *object)
 {
-  //FsUpnpSimpleIgdThread *self = FS_UPNP_SIMPLE_IGD_THREAD_CAST (object);
+  FsUpnpSimpleIgdThread *self = FS_UPNP_SIMPLE_IGD_THREAD_CAST (object);
+
+  self->priv->thread = g_thread_create (thread_func, self, TRUE, NULL);
+  g_return_if_fail (self->priv->thread);
 
   if (G_OBJECT_CLASS (fs_upnp_simple_igd_thread_parent_class)->constructed)
     G_OBJECT_CLASS (fs_upnp_simple_igd_thread_parent_class)->constructed (object);
-- 
1.5.6.5




More information about the farsight-commits mailing list