2002-08-17 19:53:51 +00:00
|
|
|
//
|
|
|
|
// ThreadNotify.cs: implements a notification for the thread running the Gtk main
|
|
|
|
// loop from another thread
|
|
|
|
//
|
|
|
|
// Author:
|
|
|
|
// Miguel de Icaza (miguel@ximian.com).
|
|
|
|
//
|
|
|
|
// (C) 2002 Ximian, Inc.
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace Gtk {
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Threading;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
// <summary>
|
|
|
|
// This delegate will be invoked on the main Gtk thread.
|
|
|
|
// </summary>
|
2002-10-27 02:30:51 +00:00
|
|
|
public delegate void ReadyEvent ();
|
2002-08-17 19:53:51 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Utility class to help writting multi-threaded Gtk applications
|
|
|
|
/// </summary>
|
2003-08-30 00:26:38 +00:00
|
|
|
/// <remarks/>
|
2002-08-17 19:53:51 +00:00
|
|
|
///
|
2003-10-17 19:17:19 +00:00
|
|
|
public class ThreadNotify : IDisposable {
|
2002-08-17 19:53:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// DllImport functions from Gtk
|
|
|
|
//
|
2003-02-22 04:34:56 +00:00
|
|
|
[DllImport ("libgtk-win32-2.0-0.dll")]
|
2003-10-17 19:17:19 +00:00
|
|
|
private static extern uint gdk_input_add (int s, int cond, GdkInputFunction f, IntPtr data);
|
2002-08-17 19:53:51 +00:00
|
|
|
public delegate void GdkInputFunction (IntPtr data, int source, int cond);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Libc stuff
|
|
|
|
//
|
|
|
|
[DllImport ("libc.so.6")]
|
2003-08-27 02:26:05 +00:00
|
|
|
private static extern int pipe (int [] fd);
|
2002-08-17 19:53:51 +00:00
|
|
|
|
|
|
|
[DllImport ("libc.so.6")]
|
2003-08-27 02:26:05 +00:00
|
|
|
private static extern unsafe int read (int fd, byte *b, int count);
|
2002-08-17 19:53:51 +00:00
|
|
|
|
|
|
|
[DllImport ("libc.so.6")]
|
2003-08-27 02:26:05 +00:00
|
|
|
private static extern unsafe int write (int fd, byte *b, int count);
|
2002-08-17 19:53:51 +00:00
|
|
|
|
2003-10-17 19:17:19 +00:00
|
|
|
[DllImport ("libc.so.6")]
|
|
|
|
private static extern int close (int fd);
|
2002-08-17 19:53:51 +00:00
|
|
|
|
|
|
|
GdkInputFunction notify_pipe;
|
|
|
|
int [] pipes;
|
2003-10-17 19:17:19 +00:00
|
|
|
bool disposed;
|
|
|
|
uint tag;
|
2002-08-17 19:53:51 +00:00
|
|
|
|
|
|
|
ReadyEvent re;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The ReadyEvent delegate will be invoked on the current thread (which should
|
|
|
|
/// be the Gtk thread) when another thread wakes us up by calling WakeupMain
|
|
|
|
/// </summary>
|
|
|
|
public ThreadNotify (ReadyEvent re)
|
|
|
|
{
|
|
|
|
notify_pipe = new GdkInputFunction (NotifyPipe);
|
|
|
|
pipes = new int [2];
|
|
|
|
pipe (pipes);
|
2003-10-17 19:17:19 +00:00
|
|
|
tag = gdk_input_add (pipes [0], 1, notify_pipe, (IntPtr) 0);
|
2002-08-17 19:53:51 +00:00
|
|
|
this.re = re;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyPipe (IntPtr data, int source, int cond)
|
|
|
|
{
|
|
|
|
byte s;
|
|
|
|
|
|
|
|
unsafe {
|
2002-10-10 19:28:23 +00:00
|
|
|
lock (this) {
|
|
|
|
read (pipes [0], &s, 1);
|
|
|
|
notified = false;
|
|
|
|
}
|
2002-08-17 19:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
re ();
|
|
|
|
}
|
|
|
|
|
2002-10-10 19:28:23 +00:00
|
|
|
bool notified = false;
|
|
|
|
|
2002-08-17 19:53:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Invoke this function from a thread to call the `ReadyEvent'
|
|
|
|
/// delegate provided in the constructor on the Main Gtk thread
|
|
|
|
/// </summary>
|
|
|
|
public void WakeupMain ()
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
byte s;
|
2002-10-10 19:28:23 +00:00
|
|
|
|
|
|
|
lock (this){
|
|
|
|
if (notified)
|
|
|
|
return;
|
|
|
|
write (pipes [1], &s, 1);
|
|
|
|
notified = true;
|
|
|
|
}
|
2002-08-17 19:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
2003-10-17 19:17:19 +00:00
|
|
|
|
|
|
|
public void Close ()
|
|
|
|
{
|
|
|
|
Dispose (true);
|
|
|
|
GC.SuppressFinalize (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ThreadNotify ()
|
|
|
|
{
|
|
|
|
Dispose (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IDisposable.Dispose ()
|
|
|
|
{
|
|
|
|
Close ();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose (bool disposing)
|
|
|
|
{
|
|
|
|
if (!disposed) {
|
|
|
|
disposed = true;
|
|
|
|
GLib.Source.Remove (tag);
|
|
|
|
close (pipes [1]);
|
|
|
|
close (pipes [0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pipes = null;
|
|
|
|
notify_pipe = null;
|
|
|
|
}
|
2002-08-17 19:53:51 +00:00
|
|
|
}
|
|
|
|
}
|