beagle r4596 - in trunk/beagle: . beagled beagled/IndexHelper



Author: dbera
Date: Sun Mar  9 16:33:09 2008
New Revision: 4596
URL: http://svn.gnome.org/viewvc/beagle?rev=4596&view=rev

Log:
Add new signal handler based on mono-1.9 UnixSignal. Enable this code if mono-1.9 is detected during configure. Then we dont need to enforce mono-1.9 on the users.


Modified:
   trunk/beagle/beagled/BeagleDaemon.cs
   trunk/beagle/beagled/IndexHelper/IndexHelper.cs
   trunk/beagle/configure.in

Modified: trunk/beagle/beagled/BeagleDaemon.cs
==============================================================================
--- trunk/beagle/beagled/BeagleDaemon.cs	(original)
+++ trunk/beagle/beagled/BeagleDaemon.cs	Sun Mar  9 16:33:09 2008
@@ -33,6 +33,7 @@
 using System.Threading;
 using Thread = System.Threading.Thread;
 using GLib;
+using Mono.Unix;
 
 using Beagle.Util;
 using Log = Beagle.Util.Log;
@@ -552,7 +553,108 @@
 		}
 
 		/////////////////////////////////////////////////////////////////////////////
+#if MONO_1_9
+		private static void SetupSignalHandlers ()
+		{
+			// Force OurSignalHandler to be JITed
+			OurSignalHandler (-1);
+
+			UnixSignal[] signals = new UnixSignal [] {
+				new UnixSignal (Mono.Unix.Native.Signum.SIGINT),
+				new UnixSignal (Mono.Unix.Native.Signum.SIGTERM),
+				new UnixSignal (Mono.Unix.Native.Signum.SIGUSR1),
+				new UnixSignal (Mono.Unix.Native.Signum.SIGUSR2),
+				new UnixSignal (Mono.Unix.Native.Signum.SIGHUP) /* Use to break the signal listener itself */
+			};
+
+			// Ignore SIGPIPE
+			Mono.Unix.Native.Stdlib.SetSignalAction (Mono.Unix.Native.Signum.SIGPIPE, Mono.Unix.Native.SignalAction.Ignore);
+
+			Thread signal_thread = new Thread (delegate () {
+				Log.Debug ("Starting signal handler thread");
+				bool stop = false;
+				while (! stop) {
+					int signal = UnixSignal.WaitAny (signals, -1);
+					stop = OurSignalHandler (signal);
+				}
+				Log.Debug ("Exiting signal handler thread");
+			});
+
+			signal_thread.Start ();
+		}
+
+		// Mono signal handler allows setting of global variables;
+		// anything else e.g. function calls, even reentrant native methods are risky
+		// Returns true if the signal handler should not continue to listen to more signals
+		private static bool OurSignalHandler (int signal)
+		{
+			// This allows us to call OurSignalHandler w/o doing anything.
+			// We want to call it once to ensure that it is pre-JITed.
+			if (signal < 0 || (Mono.Unix.Native.Signum) signal == Mono.Unix.Native.Signum.SIGHUP)
+				return true;
+
+			bool ret = false;
+
+			// Set shutdown flag to true so that other threads can stop initializing
+			if ((Mono.Unix.Native.Signum) signal != Mono.Unix.Native.Signum.SIGUSR1) {
+				Shutdown.ShutdownRequested = true;
+				ret = true;
+			}
+
+			// Do all signal handling work in the main loop and not in the signal handler.
+			GLib.Idle.Add (new GLib.IdleHandler (delegate () { HandleSignal (signal); return false; }));
+
+			return ret;
+		}
+
+		private static void HandleSignal (int signal)
+		{
+			Logger.Log.Debug ("Handling signal {0} ({1})", signal, (Mono.Unix.Native.Signum) signal);
+
+			// If we get SIGUSR1, turn the debugging level up.
+			if ((Mono.Unix.Native.Signum) signal == Mono.Unix.Native.Signum.SIGUSR1) {
+				LogLevel old_level = Log.Level;
+				Log.Level = LogLevel.Debug;
+				Log.Debug ("Moving from log level {0} to Debug", old_level);
+			}
+
+			// Send informational signals to the helper too.
+			if ((Mono.Unix.Native.Signum) signal == Mono.Unix.Native.Signum.SIGUSR1 ||
+			    (Mono.Unix.Native.Signum) signal == Mono.Unix.Native.Signum.SIGUSR2) {
+				GLib.Idle.Add (new GLib.IdleHandler (delegate () { RemoteIndexer.SignalRemoteIndexer ((Mono.Unix.Native.Signum) signal); return false; }));
+				return;
+			}
+
+			Logger.Log.Debug ("Initiating shutdown in response to signal.");
+			Shutdown.BeginShutdown ();
+		}
+
+		/////////////////////////////////////////////////////////////////////////////
+
+		private static void OnShutdown ()
+		{
+#if ENABLE_AVAHI
+			zeroconf.Dispose ();
+#endif			
+			// Send a signal to index-helper (if we miss this, even then index-helper will see the daemon going away and kill itself)
+			RemoteIndexer.SignalRemoteIndexer (Mono.Unix.Native.Signum.SIGINT);
+
+			// Stop our Inotify threads
+			Inotify.Stop ();
+
+			// Stop the global scheduler and ask it to shutdown
+			Scheduler.Global.Stop (true);
 
+			// Stop the messaging server
+			if (server != null)
+				server.Stop ();
+
+			// Stop the signal handler thread by sending a fake SIGHUP signal
+			Mono.Unix.Native.Syscall.kill (Process.GetCurrentProcess ().Id, Mono.Unix.Native.Signum.SIGHUP);
+		}
+
+		/////////////////////////////////////////////////////////////////////////////
+#else
 		private static void SetupSignalHandlers ()
 		{
 			// Force OurSignalHandler to be JITed
@@ -624,10 +726,10 @@
 			if (server != null)
 				server.Stop ();
 		}
+#endif
 
 		/////////////////////////////////////////////////////////////////////////////
 
-
 		private static ArrayList exercise_files = new ArrayList ();
 
 		private static void ExerciseTheDogHarder ()

Modified: trunk/beagle/beagled/IndexHelper/IndexHelper.cs
==============================================================================
--- trunk/beagle/beagled/IndexHelper/IndexHelper.cs	(original)
+++ trunk/beagle/beagled/IndexHelper/IndexHelper.cs	Sun Mar  9 16:33:09 2008
@@ -34,6 +34,7 @@
 using System.Threading;
 
 using GLib;
+using Mono.Unix;
 
 using Beagle.Daemon;
 using Beagle.Util;
@@ -280,6 +281,100 @@
 		}
 
 		/////////////////////////////////////////////////////////////////////////////
+#if MONO_1_9
+		private static void SetupSignalHandlers ()
+		{
+			// Force OurSignalHandler to be JITed
+			OurSignalHandler (-1);
+
+			UnixSignal[] signals = new UnixSignal [] {
+				new UnixSignal (Mono.Unix.Native.Signum.SIGINT),
+				new UnixSignal (Mono.Unix.Native.Signum.SIGTERM),
+				new UnixSignal (Mono.Unix.Native.Signum.SIGUSR1),
+				new UnixSignal (Mono.Unix.Native.Signum.SIGUSR2),
+				new UnixSignal (Mono.Unix.Native.Signum.SIGHUP) /* Use to break the signal listener itself */
+			};
+
+			// Ignore SIGPIPE
+			Mono.Unix.Native.Stdlib.SetSignalAction (Mono.Unix.Native.Signum.SIGPIPE, Mono.Unix.Native.SignalAction.Ignore);
+
+			Thread signal_thread = new Thread (delegate () {
+				Log.Debug ("Starting signal handler thread");
+				bool shutdown = false;
+				while (! shutdown) {
+					int signal = UnixSignal.WaitAny (signals, -1);
+					shutdown = OurSignalHandler (signal);
+				}
+				Log.Debug ("Exiting signal handler thread");
+			});
+
+			signal_thread.Start ();
+		}
+
+		// Returns true if the signal handler should not continue to listen to more signals
+		private static bool OurSignalHandler (int signal)
+		{
+			// This allows us to call OurSignalHandler w/o doing anything.
+			// We want to call it once to ensure that it is pre-JITed.
+			if (signal < 0 || (Mono.Unix.Native.Signum) signal == Mono.Unix.Native.Signum.SIGHUP)
+				return true;
+
+			bool ret = false;
+
+			// SIGUSR1 and SIGUSR2 are informational signals.  For other
+			// signals that we handle, set the shutdown flag to true so
+			// that other threads can stop initializing
+			if ((Mono.Unix.Native.Signum) signal != Mono.Unix.Native.Signum.SIGUSR1 &&
+			    (Mono.Unix.Native.Signum) signal != Mono.Unix.Native.Signum.SIGUSR2) {
+				Shutdown.ShutdownRequested = true;
+				ret = true;
+			}
+
+			// Do all signal handling work in the main loop and not in the signal handler.
+			GLib.Idle.Add (new GLib.IdleHandler (delegate () { HandleSignal (signal); return false; }));
+
+			return ret;
+		}
+
+		private static void HandleSignal (int signal)
+		{
+			Log.Warn ("Handling signal {0} ({1})", signal, (Mono.Unix.Native.Signum) signal);
+
+			// If we get SIGUSR1, turn the debugging level up.
+			if ((Mono.Unix.Native.Signum) signal == Mono.Unix.Native.Signum.SIGUSR1) {
+				LogLevel old_level = Log.Level;
+				Log.Level = LogLevel.Debug;
+				Log.Debug ("Moving from log level {0} to Debug", old_level);
+			}
+
+			string span = StringFu.TimeSpanToString (DateTime.Now - last_activity);
+
+			if (CurrentDisplayUri == null)
+				Log.Warn ("Filtering status ({0} ago): no document is currently being filtered.", span);
+			else if (CurrentFilter == null)
+				Log.Warn ("Filtering status ({0} ago): determining filter and extracting properties for {1} ({2})", span, CurrentDisplayUri, CurrentContentUri);
+			else
+				Log.Warn ("Filtering status ({0} ago): extracting text from {1} ({2}) with {3}", span, CurrentDisplayUri, CurrentContentUri, CurrentFilter);
+
+			// Don't shut down on information signals (SIGUSR1 and SIGUSR2)
+			if ((Mono.Unix.Native.Signum) signal == Mono.Unix.Native.Signum.SIGUSR1 ||
+			    (Mono.Unix.Native.Signum) signal == Mono.Unix.Native.Signum.SIGUSR2)
+				return;
+
+			Logger.Log.Debug ("Initiating shutdown in response to signal.");
+			Shutdown.BeginShutdown ();
+		}
+
+		private static void OnShutdown ()
+		{
+			if (server != null)
+				server.Stop ();
+
+			// Stop the signal handler thread by sending a fake SIGHUP signal
+			Mono.Unix.Native.Syscall.kill (Process.GetCurrentProcess ().Id, Mono.Unix.Native.Signum.SIGHUP);
+		}
+
+#else
 
 		private static void SetupSignalHandlers ()
 		{
@@ -348,6 +443,7 @@
 			if (server != null)
 				server.Stop ();
 		}
+#endif
 	}
 
 }

Modified: trunk/beagle/configure.in
==============================================================================
--- trunk/beagle/configure.in	(original)
+++ trunk/beagle/configure.in	Sun Mar  9 16:33:09 2008
@@ -66,10 +66,17 @@
   AC_MSG_RESULT([found])
 fi
 
+BEAGLE_DEFINES=""
+
 # check that we have the require version of mono
-PKG_CHECK_MODULES(MONO, mono >= $MONO_REQUIRED) 
 
-BEAGLE_DEFINES=""
+# Temporary: check for mono-1.9
+PKG_CHECK_MODULES(MONO_1_9, mono >= 1.9.0, mono_1_9=yes, mono_1_9=no) 
+if test "x$mono_1_9" = "xno"; then
+	PKG_CHECK_MODULES(MONO, mono >= $MONO_REQUIRED) 
+else
+	BEAGLE_DEFINES="$BEAGLE_DEFINES -define:MONO_1_9"
+fi
 
 # check for various mono DLLs that we need.
 needed_dlls="Mono.Data.Sqlite Mono.Posix System.Data System.Web ICSharpCode.SharpZipLib"



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