gdk_threads_enter/leave & OO.o ...



Hi Owen,

	As per our summit discussion, here's a re-hashed patched with a setter
method - and a chunk of comment to partially explain why it's necessary.

	Comments / may I commit ?

	Thanks,

		Michael.

Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gtk+/ChangeLog,v
retrieving revision 1.4678
diff -u -p -u -r1.4678 ChangeLog
--- ChangeLog	2 Dec 2003 04:23:00 -0000	1.4678
+++ ChangeLog	2 Dec 2003 14:53:06 -0000
@@ -1,3 +1,23 @@
+2003-12-02  Michael Meeks  <michael ximian com>
+
+	* gdk/gdk.c (gdk_threads_set_guard_hooks): impl.
+
+Tue Sep 16 15:46:31  Martin Kretzschmar  <m_kretzschmar gmx net>
+
+	* 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.
+
+	* gdk/gdkglobals.c: add definitions of gdk_threads_[un]lock.
+
+	* gdk/gdktypes.h: new GdkGuardFunction typedef for
+	gdk_threads_[un]lock.
+
 2003-12-01  Federico Mena Quintero  <federico ximian com>
 
 	Decouple impl->current_folder from the selection in the folder
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	2 Dec 2003 14:53:07 -0000
@@ -495,6 +495,22 @@ gdk_threads_leave ()
   GDK_THREADS_LEAVE ();
 }
 
+GDKVAR GMutex *gdk_threads_mutex;
+
+static void
+gdk_threads_impl_lock ()
+{
+  if (gdk_threads_mutex)
+    g_mutex_lock (gdk_threads_mutex);
+}
+
+static void
+gdk_threads_impl_unlock ()
+{
+  if (gdk_threads_mutex)
+    g_mutex_unlock (gdk_threads_mutex);
+}
+
 /**
  * gdk_threads_init:
  * 
@@ -512,6 +528,36 @@ gdk_threads_init ()
     g_error ("g_thread_init() must be called before
gdk_threads_init()");
 
   gdk_threads_mutex = g_mutex_new ();
+  gdk_threads_lock = gdk_threads_impl_lock;
+  gdk_threads_unlock = gdk_threads_impl_unlock;
+}
+
+/**
+ * gdk_threads_set_guard_hooks:
+ * @fn_lock:   function called to guard gtk+
+ * @fn_unlock: function called to release the guard
+ * 
+ * This method allows a one-shot override of the global
+ * gtk lock used to ensure that only one thread can be
+ * using gtk/X. This allows an application implementing
+ * it's own threading model to synchronize it's own internal
+ * locking scheme with that of gtk+.
+ *
+ * This is particularly useful for allowing gtk+ code that
+ * enters a new mainloop, eg. gtk_dialog_run to drop the
+ * application's own lock, such that it can perform other
+ * graphical methods on other threads while waiting for a
+ * response.
+ *
+ * This method should not be used in normal applications,
+ * It must be called after gdk_threads_init.
+ **/
+void
+gdk_threads_set_guard_hooks (GdkGuardFunction fn_lock,
+			     GdkGuardFunction fn_unlock)
+{
+	gdk_threads_lock = fn_lock;
+	gdk_threads_unlock = fn_unlock;
 }
 
 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	2 Dec 2003 14:53:07 -0000
@@ -171,20 +171,27 @@ void gdk_notify_startup_complete (void);
 /* Threading
  */
 
-GDKVAR GMutex *gdk_threads_mutex;
+#ifndef GDK_DISABLE_DEPRECATED
+GDKVAR GMutex *gdk_threads_mutex; /* private */
+#endif
+
+GDKVAR GdkGuardFunction gdk_threads_lock;
+GDKVAR GdkGuardFunction 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_guard_hooks      (GdkGuardFunction fn_lock,
+					   GdkGuardFunction fn_unlock);
 
 #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	2 Dec 2003 14:53:07 -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; deprecated */
+GdkGuardFunction     gdk_threads_lock = NULL;
+GdkGuardFunction     gdk_threads_unlock = NULL;
Index: gdk/gdktypes.h
===================================================================
RCS file: /cvs/gnome/gtk+/gdk/gdktypes.h,v
retrieving revision 1.63
diff -u -p -u -r1.63 gdktypes.h
--- gdk/gdktypes.h	25 Apr 2002 22:29:10 -0000	1.63
+++ gdk/gdktypes.h	2 Dec 2003 14:53:07 -0000
@@ -200,6 +200,8 @@ struct _GdkSpan
   gint width;
 };
 
+typedef void (*GdkGuardFunction) (void);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */

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




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