[Gtk-sharp-list] ThreadNotify and windows

Gonzalo Paniagua Javier gonzalo@ximian.com
Thu, 04 Mar 2004 23:49:02 -0500


--=-yzAkkv2s6iIjNeKl0UQw
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi there!

I've just committed a patch that should make ThreadNotify work on
windows (attached). It's tested to work on linux but I couldn't test it
on windows. Anyone?

-Gonzalo


--=-yzAkkv2s6iIjNeKl0UQw
Content-Disposition: attachment; filename=tnotify.patch
Content-Type: text/x-patch; name=tnotify.patch; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

? .swp
Index: glue/Makefile.am
===================================================================
RCS file: /cvs/public/gtk-sharp/glue/Makefile.am,v
retrieving revision 1.35
diff -u -r1.35 Makefile.am
--- glue/Makefile.am	29 Jan 2004 21:20:59 -0000	1.35
+++ glue/Makefile.am	5 Mar 2004 02:57:50 -0000
@@ -20,6 +20,7 @@
 	selectiondata.c		\
 	slist.c			\
 	style.c			\
+	thread-notify.c		\
 	time_t.c		\
 	type.c			\
 	value.c			\
Index: glue/makefile.win32
===================================================================
RCS file: /cvs/public/gtk-sharp/glue/makefile.win32,v
retrieving revision 1.9
diff -u -r1.9 makefile.win32
--- glue/makefile.win32	29 Jan 2004 21:20:59 -0000	1.9
+++ glue/makefile.win32	5 Mar 2004 02:57:50 -0000
@@ -22,6 +22,7 @@
         selectiondata.o         \
         slist.o                 \
         style.o                 \
+	thread-notify.o		\
         time_t.o                \
         type.o                  \
         value.o                 \
Index: glue/thread-notify.c
===================================================================
RCS file: glue/thread-notify.c
diff -N glue/thread-notify.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ glue/thread-notify.c	5 Mar 2004 02:57:50 -0000
@@ -0,0 +1,66 @@
+/*
+ * Pipe for ThreadNotify
+ *
+ * (C) 2004 Gonzalo Paniagua Javier (gonzalo@ximian.com)
+ */
+
+#include <glib.h>
+#ifdef G_OS_WIN32
+#include <windows.h>
+#else
+#include <stdio.h>
+#include <unistd.h>
+#endif
+
+gint pipe_create (gint *fds);
+
+gint
+pipe_create (gint *fds)
+{
+#ifdef G_OS_WIN32
+	return !CreatePipe ((PHANDLE) fds, (PHANDLE) &fds [1], NULL, 1024);
+#else
+	return pipe (fds);
+#endif
+}
+
+gint pipe_read (gint fd, gchar *buffer, gint maxcount);
+
+gint
+pipe_read (gint fd, gchar *buffer, gint maxcount)
+{
+#ifdef G_OS_WIN32
+	glong dummy;
+	return !ReadFile ((HANDLE) fd, buffer, maxcount, &dummy, NULL);
+#else
+	return (read (fd, buffer, maxcount) < 0);
+#endif
+}
+
+gint pipe_write (gint fd, gchar *buffer, gint maxcount);
+
+gint
+pipe_write (gint fd, gchar *buffer, gint maxcount)
+{
+#ifdef G_OS_WIN32
+	glong dummy;
+	return !WriteFile ((HANDLE) fd, buffer, maxcount, &dummy, NULL);
+#else
+	return (write (fd, buffer, maxcount) < 0);
+#endif
+}
+
+void pipe_close (gint *fds);
+
+void
+pipe_close (gint *fds)
+{
+#ifdef G_OS_WIN32
+	CloseHandle ((HANDLE) fds [0]);
+	CloseHandle ((HANDLE) fds [1]);
+#else
+	close (fds [0]);
+	close (fds [1]);
+#endif
+}
+
Index: gtk/ThreadNotify.cs
===================================================================
RCS file: /cvs/public/gtk-sharp/gtk/ThreadNotify.cs,v
retrieving revision 1.7
diff -u -r1.7 ThreadNotify.cs
--- gtk/ThreadNotify.cs	17 Oct 2003 19:17:19 -0000	1.7
+++ gtk/ThreadNotify.cs	5 Mar 2004 02:57:50 -0000
@@ -2,10 +2,12 @@
 // ThreadNotify.cs: implements a notification for the thread running the Gtk main
 // loop from another thread
 //
-// Author:
+// Authors:
 //    Miguel de Icaza (miguel@ximian.com).
+//    Gonzalo Paniagua Javier (gonzalo@ximian.com)
 //
 // (C) 2002 Ximian, Inc.
+// (C) 2004 Novell, Inc.
 //
 
 namespace Gtk {
@@ -29,24 +31,21 @@
 		// DllImport functions from Gtk
 		//
 		[DllImport ("libgtk-win32-2.0-0.dll")]
-		private static extern uint gdk_input_add (int s, int cond, GdkInputFunction f, IntPtr data);
+		static extern uint gdk_input_add (int s, int cond, GdkInputFunction f, IntPtr data);
 		public delegate void GdkInputFunction (IntPtr data, int source, int cond);
 
-		//
-		// Libc stuff
-		//
-		[DllImport ("libc.so.6")]
-		private static extern int pipe (int [] fd);
+		[DllImport ("gtksharpglue")]
+		static extern int pipe_create (int [] fd);
 		
-		[DllImport ("libc.so.6")]
-		private static extern unsafe int read (int fd, byte *b, int count);
+		[DllImport ("gtksharpglue")]
+		static extern unsafe int pipe_read (int fd, byte *b, int count);
 		
-		[DllImport ("libc.so.6")]
-		private static extern unsafe int write (int fd, byte *b, int count);
+		[DllImport ("gtksharpglue")]
+		static extern unsafe int pipe_write (int fd, byte *b, int count);
+
+		[DllImport ("gtksharpglue")]
+		static extern int pipe_close (int [] fd);
 
-		[DllImport ("libc.so.6")]
-		private static extern int close (int fd);
-		
 		GdkInputFunction notify_pipe;
 		int [] pipes;
 		bool disposed;
@@ -62,7 +61,7 @@
 		{
 			notify_pipe = new GdkInputFunction (NotifyPipe);
 			pipes = new int [2];
-			pipe (pipes);
+			pipe_create (pipes);
 			tag = gdk_input_add (pipes [0], 1, notify_pipe, (IntPtr) 0);
 			this.re = re;
 		}
@@ -73,7 +72,7 @@
 			
 			unsafe {
 				lock (this) {
-					read (pipes [0], &s, 1);
+					pipe_read (pipes [0], &s, 1);
 					notified = false;
 				}
 			}
@@ -95,7 +94,8 @@
 				lock (this){
 					if (notified)
 						return;
-					write (pipes [1], &s, 1);
+					
+					pipe_write (pipes [1], &s, 1);
 					notified = true;
 				}
 			}
@@ -122,8 +122,7 @@
 			if (!disposed) {
 				disposed = true;
 				GLib.Source.Remove (tag);
-				close (pipes [1]);
-				close (pipes [0]);
+				pipe_close (pipes);
 			}
 
 			pipes = null;
@@ -131,3 +130,4 @@
 		}
 	}
 }
+

--=-yzAkkv2s6iIjNeKl0UQw--