Re: gdk_threads_enter/leave & OO.o ...



Hi Owen,

	Thanks for your detailed comments; I've addressed all your points, it
looks much nicer now; I include the new version;

	I'm wondering if we should bin the branches in
gdk_threads_impl_lock/unlock, otherwise may I commit ?

	Regards,

		Michael.

Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gtk+/ChangeLog,v
retrieving revision 1.4680
diff -u -p -u -r1.4680 ChangeLog
--- ChangeLog	4 Dec 2003 18:17:22 -0000	1.4680
+++ ChangeLog	5 Dec 2003 00:34:55 -0000
@@ -1,3 +1,19 @@
+2003-12-02  Michael Meeks  <michael ximian com>
+
+	Based on a patch by Martin Kretzschmar
+	
+	* gdk/gdk.h: new gdk_threads_lock, gdk_threads_unlock, point to
+	implementation of GDK_THREADS_ENTER / GDK_THREADS_LEAVE.
+	(GDK_THREADS_ENTER, GDK_THREADS_LEAVE): use gdk_threads_[un]lock
+	function pointers. Deprecate the global gdk_threads_mutex variable.
+	
+	* gdk/gdk.c (gdk_threads_impl_lock, gdk_threads_impl_unlock): new,
+	extracted from GTK_THREADS_ENTER/LEAVE macros.
+	(gdk_threads_init): init gtk_threads_[un]lock if not set.
+	(gdk_threads_set_lock_functions): impl.
+
+	* gdk/gdkglobals.c: add definitions of gdk_threads_[un]lock.
+
 2003-12-04  Mark McLoughlin  <mark skynet ie>
 
 	* gtk/gtklabel.c: (gtk_label_set_attributes_internal): Allow
Index: gdk/gdk.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk/gdk.c,v
retrieving revision 1.152
diff -u -p -u -r1.152 gdk.c
--- gdk/gdk.c	3 Nov 2003 20:08:30 -0000	1.152
+++ gdk/gdk.c	5 Dec 2003 00:34:55 -0000
@@ -495,6 +495,20 @@ gdk_threads_leave ()
   GDK_THREADS_LEAVE ();
 }
 
+static void
+gdk_threads_impl_lock (void)
+{
+  if (gdk_threads_mutex)
+    g_mutex_lock (gdk_threads_mutex);
+}
+
+static void
+gdk_threads_impl_unlock (void)
+{
+  if (gdk_threads_mutex)
+    g_mutex_unlock (gdk_threads_mutex);
+}
+
 /**
  * gdk_threads_init:
  * 
@@ -512,6 +526,48 @@ gdk_threads_init ()
     g_error ("g_thread_init() must be called before
gdk_threads_init()");
 
   gdk_threads_mutex = g_mutex_new ();
+  if (!gdk_threads_lock)
+    gdk_threads_lock = gdk_threads_impl_lock;
+  if (!gdk_threads_unlock)
+    gdk_threads_unlock = gdk_threads_impl_unlock;
+}
+
+/**
+ * gdk_threads_set_lock_functions:
+ * @enter_fn:   function called to guard gtk+
+ * @leave_fn: function called to release the guard
+ *
+ * Allows the application to replace the standard method that
+ * GDK uses to protect its data structures. Normally, GDK
+ * creates a single #GMutex that is locked by gdk_threads_enter(),
+ * and released by gdk_threads_leave(); using this function an
+ * application provides, instead, a function @enter_fn that is
+ * called by gdk_threads_enter() and a function @leave_fn that is
+ * called by gdk_threads_leave().
+ *
+ * The functions must provide at least same locking functionality
+ * as the default implementation, but can also do extra application
+ * specific processing.
+ *
+ * As an example, consider an application that has its own recursive
+ * lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
+ * the GTK+ lock when entering a recursive main loop, the application
+ * must temporarily release all its lock as well.
+ *
+ * Most threaded GTK+ apps won't need to use this method.
+ *
+ * This method must be called before gdk_threads_init, and cannot
+ * be called multiple times.
+ **/
+void
+gdk_threads_set_lock_functions (GCallback enter_fn,
+				GCallback leave_fn)
+{
+  g_return_if_fail (gdk_threads_lock == NULL &&
+		    gdk_threads_unlock == NULL);
+
+  gdk_threads_lock = enter_fn;
+  gdk_threads_unlock = leave_fn;
 }
 
 G_CONST_RETURN char *
Index: gdk/gdk.h
===================================================================
RCS file: /cvs/gnome/gtk+/gdk/gdk.h,v
retrieving revision 1.100
diff -u -p -u -r1.100 gdk.h
--- gdk/gdk.h	31 Jan 2003 00:08:32 -0000	1.100
+++ gdk/gdk.h	5 Dec 2003 00:34:56 -0000
@@ -171,20 +171,27 @@ void gdk_notify_startup_complete (void);
 /* Threading
  */
 
-GDKVAR GMutex *gdk_threads_mutex;
+#if !defined (GDK_DISABLE_DEPRECATED) || defined (GDK_COMPILATION)
+GDKVAR GMutex *gdk_threads_mutex; /* private */
+#endif
+
+GDKVAR GCallback gdk_threads_lock;
+GDKVAR GCallback gdk_threads_unlock;
 
 void     gdk_threads_enter                (void);
 void     gdk_threads_leave                (void);
-void     gdk_threads_init                 (void);  
+void     gdk_threads_init                 (void);
+void     gdk_threads_set_lock_functions   (GCallback enter_fn,
+					   GCallback leave_fn);
 
 #ifdef	G_THREADS_ENABLED
 #  define GDK_THREADS_ENTER()	G_STMT_START {	\
-      if (gdk_threads_mutex)                 	\
-        g_mutex_lock (gdk_threads_mutex);   	\
+      if (gdk_threads_lock)                 	\
+        (*gdk_threads_lock) ();			\
    } G_STMT_END
 #  define GDK_THREADS_LEAVE()	G_STMT_START { 	\
-      if (gdk_threads_mutex)                 	\
-        g_mutex_unlock (gdk_threads_mutex); 	\
+      if (gdk_threads_unlock)                 	\
+        (*gdk_threads_unlock) ();		\
    } G_STMT_END
 #else	/* !G_THREADS_ENABLED */
 #  define GDK_THREADS_ENTER()
Index: gdk/gdkglobals.c
===================================================================
RCS file: /cvs/gnome/gtk+/gdk/gdkglobals.c,v
retrieving revision 1.28
diff -u -p -u -r1.28 gdkglobals.c
--- gdk/gdkglobals.c	31 Oct 2002 21:11:13 -0000	1.28
+++ gdk/gdkglobals.c	5 Dec 2003 00:34:56 -0000
@@ -40,5 +40,6 @@ gchar              *_gdk_display_arg_nam
 
 GSList             *_gdk_displays = NULL;
 
-GMutex           *gdk_threads_mutex = NULL;          /* Global GDK lock
*/
-
+GMutex              *gdk_threads_mutex = NULL;          /* Global GDK
lock */
+GCallback            gdk_threads_lock = NULL;
+GCallback            gdk_threads_unlock = NULL;


-- 
 michael ximian com  <><, Pseudo Engineer, itinerant idiot




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