gnome-edu r20 - in trunk/garfunkel: . src



Author: brunobol
Date: Tue Dec 23 11:29:59 2008
New Revision: 20
URL: http://svn.gnome.org/viewvc/gnome-edu?rev=20&view=rev

Log:
fix thread with GMutex - patch from FabrÃcio Godoy

Modified:
   trunk/garfunkel/ChangeLog
   trunk/garfunkel/src/garfunkel.c
   trunk/garfunkel/src/main.c

Modified: trunk/garfunkel/src/garfunkel.c
==============================================================================
--- trunk/garfunkel/src/garfunkel.c	(original)
+++ trunk/garfunkel/src/garfunkel.c	Tue Dec 23 11:29:59 2008
@@ -30,6 +30,7 @@
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 #include <librsvg/rsvg.h>
+#include <glib-2.0/glib.h>
 
 #include "garfunkel.h"
 
@@ -52,22 +53,21 @@
   RsvgHandle *svg_handle;
 
   GarfunkelLights lights;
-
-  gulong blink_time;
   
   GSList *sequence;
   GSList *user_sequence;
+  
+  GMutex *thread_mutex;
 };
 
 static          gboolean garfunkel_expose (GtkWidget *widget, GdkEventExpose *event);
 static void     garfunkel_redraw (Garfunkel *garfunkel);
 static void     garfunkel_blink (Garfunkel *garfunkel, GarfunkelLights light, gulong time);
-static gpointer garfunkel_blink_thread (gpointer data);
 static gboolean garfunkel_key_press (GtkWidget *widget, GdkEventKey *event);
 static gboolean garfunkel_button_press (GtkWidget *widget, GdkEventButton *event);
 static void     garfunkel_sequence_increment (Garfunkel *garfunkel);
 static void     garfunkel_sequence_drop (Garfunkel *garfunkel);
-static void     garfunkel_sequence_blink (Garfunkel *garfunkel);
+static void     garfunkel_sequence_blink (gpointer data);
 
 
 static void
@@ -85,6 +85,8 @@
   garfunkel->priv->lights = 0;
   garfunkel->priv->sequence = NULL;
   garfunkel->priv->user_sequence = NULL;
+  
+  garfunkel->priv->thread_mutex = g_mutex_new ();
 }
 
 
@@ -94,7 +96,9 @@
   Garfunkel * garfunkel = (Garfunkel *) object;
 
   g_object_unref (garfunkel->priv->svg_handle);
-  
+
+  g_mutex_free (garfunkel->priv->thread_mutex);
+    
   g_free(garfunkel->priv);
 }
 
@@ -215,6 +219,8 @@
 static void
 garfunkel_redraw (Garfunkel *garfunkel)
 {
+  gdk_threads_enter ();
+  
   GtkWidget *widget;
   GdkRegion *region;
 
@@ -228,38 +234,29 @@
   gdk_window_process_updates (widget->window, TRUE);
 
   gdk_region_destroy (region);
+  
+  gdk_threads_leave ();
 }
 
 
 static void
 garfunkel_blink (Garfunkel *garfunkel, GarfunkelLights light, gulong time)
 {
-  garfunkel->priv->lights = garfunkel->priv->lights | light;
-
-  garfunkel->priv->blink_time = time;
-
-  garfunkel_redraw (garfunkel);
-
-  g_thread_join (g_thread_create (garfunkel_blink_thread, (gpointer) garfunkel, TRUE, NULL));
-}
-
-
-static gpointer
-garfunkel_blink_thread (gpointer data)
-{
-  Garfunkel *garfunkel = (Garfunkel *) data;
-
   GarfunkelPrivate * gp= NULL;
 
   gp = garfunkel->priv;
 
-  g_usleep (gp->blink_time / 2);
+  gp->lights = garfunkel->priv->lights | light;
+
+  garfunkel_redraw (garfunkel);
+
+  g_usleep (time / 2);
 
   gp->lights = 0;
 
   garfunkel_redraw (garfunkel);
   
-  g_usleep (gp->blink_time);
+  g_usleep (time);
 }
 
 
@@ -281,9 +278,12 @@
 
   garfunkel = (Garfunkel *) widget;
   
+  if (!g_mutex_trylock (garfunkel->priv->thread_mutex))
+    return;
+
   garfunkel_sequence_increment (garfunkel);
   
-  garfunkel_sequence_blink (garfunkel);
+  g_thread_create(garfunkel_sequence_blink, (gpointer) garfunkel, FALSE, NULL);
 
   return FALSE;
 }
@@ -318,8 +318,10 @@
 
 
 static void
-garfunkel_sequence_blink (Garfunkel *garfunkel)
+garfunkel_sequence_blink (gpointer data)
 {
+  Garfunkel *garfunkel = (Garfunkel *) data;
+
   GarfunkelPrivate * gp= NULL;
 
   gp = garfunkel->priv;
@@ -328,11 +330,16 @@
 
   tmplist = gp->sequence;
 
-  while(tmplist){
+  while (tmplist)
+  {
   
     garfunkel_blink (garfunkel, (gint) (tmplist->data), GFK_BLINK_SPEED_NORMAL);
     
-    tmplist = g_slist_next(tmplist);
+    tmplist = g_slist_next (tmplist);
   }
+  
+  garfunkel_redraw (garfunkel);
+
+  g_mutex_unlock (gp->thread_mutex);
 } 
 

Modified: trunk/garfunkel/src/main.c
==============================================================================
--- trunk/garfunkel/src/main.c	(original)
+++ trunk/garfunkel/src/main.c	Tue Dec 23 11:29:59 2008
@@ -34,8 +34,8 @@
   GtkWidget *window;
   GtkWidget *garfunkel;
 
-  g_thread_init(NULL);
-  gdk_threads_init();
+  g_thread_init (NULL);
+  gdk_threads_init ();
 
   gtk_set_locale ();
   gtk_init (&argc, &argv);
@@ -58,9 +58,9 @@
 
   gtk_widget_grab_focus (garfunkel);
   
-  gdk_threads_enter();
-  gtk_main();
-  gdk_threads_leave();
+  gdk_threads_enter ();
+  gtk_main ();
+  gdk_threads_leave ();
 
   return 0;
 }



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