Author: jonpryor
Date: 2008-02-08 10:14:30 -0500 (Fri, 08 Feb 2008)
New Revision: 95262

Added:
   branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix/UnixSignal.cs
Modified:
   branches/mono-1-9/mcs/class/Mono.Posix/ChangeLog
   branches/mono-1-9/mcs/class/Mono.Posix/Mono.Posix.dll.sources
   branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix.Native/ChangeLog
   branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix.Native/Stdlib.cs
   branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix/ChangeLog
Log:
        * Mono.Posix.dll.sources: Add Mono.Unix/UnixSignal.cs.
        * Mono.Unix/UnixSignal.cs: Added; Polling and blocking based Unix 
signal 
          mechanism.
          - 
http://lists.ximian.com/pipermail/mono-devel-list/2008-January/026501.html
                - Modification of Info->count should be thread safe.
        * Mono.Unix.Native/Stdlib.cs: Obsolete Stdlib.signal(), as it's not 
safe; 
          see also:
          
http://lists.ximian.com/pipermail/mono-devel-list/2008-January/026501.html
          
http://lists.ximian.com/pipermail/mono-devel-list/2008-January/026503.html
          Add SetSignalAction() as a replacement.


Modified: branches/mono-1-9/mcs/class/Mono.Posix/ChangeLog
===================================================================
--- branches/mono-1-9/mcs/class/Mono.Posix/ChangeLog    2008-02-08 14:52:39 UTC 
(rev 95261)
+++ branches/mono-1-9/mcs/class/Mono.Posix/ChangeLog    2008-02-08 15:14:30 UTC 
(rev 95262)
@@ -1,3 +1,7 @@
+2008-01-28  Jonathan Pryor  <[EMAIL PROTECTED]>
+
+       * Mono.Posix.dll.sources: Add Mono.Unix/UnixSignal.cs.
+
 2006-10-24  Jonathan Pryor  <[EMAIL PROTECTED]>
 
        * Makefile: Don't build make-map.exe.

Modified: branches/mono-1-9/mcs/class/Mono.Posix/Mono.Posix.dll.sources
===================================================================
--- branches/mono-1-9/mcs/class/Mono.Posix/Mono.Posix.dll.sources       
2008-02-08 14:52:39 UTC (rev 95261)
+++ branches/mono-1-9/mcs/class/Mono.Posix/Mono.Posix.dll.sources       
2008-02-08 15:14:30 UTC (rev 95262)
@@ -25,6 +25,7 @@
 ./Mono.Unix/UnixPath.cs
 ./Mono.Unix/UnixPipes.cs
 ./Mono.Unix/UnixProcess.cs
+./Mono.Unix/UnixSignal.cs
 ./Mono.Unix/UnixStream.cs
 ./Mono.Unix/UnixSymbolicLinkInfo.cs
 ./Mono.Unix/UnixUserInfo.cs

Modified: branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix/ChangeLog
===================================================================
--- branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix/ChangeLog  2008-02-08 
14:52:39 UTC (rev 95261)
+++ branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix/ChangeLog  2008-02-08 
15:14:30 UTC (rev 95262)
@@ -1,3 +1,12 @@
+2008-02-08  Jonathan Pryor  <[EMAIL PROTECTED]>
+
+       * UnixSignal.cs: Modification of Info->count should be thread safe.
+
+2008-01-28  Jonathan Pryor  <[EMAIL PROTECTED]>
+
+       * UnixSignal.cs: Added; Polling and blocking based Unix signal 
mechanism.
+         
http://lists.ximian.com/pipermail/mono-devel-list/2008-January/026501.html
+
 2008-01-22  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * UnixPipes.cs: Fix Equals to avoid NRE (and return false if object 

Added: branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix/UnixSignal.cs
===================================================================
--- branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix/UnixSignal.cs      
2008-02-08 14:52:39 UTC (rev 95261)
+++ branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix/UnixSignal.cs      
2008-02-08 15:14:30 UTC (rev 95262)
@@ -0,0 +1,166 @@
+//
+// Mono.Unix/UnixSignal.cs
+//
+// Authors:
+//   Jonathan Pryor ([EMAIL PROTECTED])
+//
+// (C) 2008 Novell, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Runtime.InteropServices;
+using System.Threading;
+
+using Mono.Unix.Native;
+
+namespace Mono.Unix {
+       public class UnixSignal : WaitHandle {
+               private Signum signum;
+               private IntPtr signal_info;
+
+               public UnixSignal (Signum signum)
+               {
+                       this.signum = signum;
+                       // ensure signum is a valid signal
+                       int _signum = NativeConvert.FromSignum (signum);
+                       this.signal_info = install (_signum);
+                       if (this.signal_info == IntPtr.Zero) {
+                               throw new ArgumentException ("Unable to handle 
signal", "signum");
+                       }
+               }
+
+               public Signum Signum {
+                       get {
+                               AssertValid ();
+                               return signum; 
+                       }
+               }
+
+               [DllImport (Stdlib.MPH, 
CallingConvention=CallingConvention.Cdecl,
+                               EntryPoint="Mono_Unix_UnixSignal_install")]
+               private static extern IntPtr install (int signum);
+
+               [DllImport (Stdlib.MPH, 
CallingConvention=CallingConvention.Cdecl,
+                               EntryPoint="Mono_Unix_UnixSignal_uninstall")]
+               private static extern int uninstall (IntPtr info);
+
+               [DllImport (Stdlib.MPH, 
CallingConvention=CallingConvention.Cdecl,
+                               EntryPoint="Mono_Unix_UnixSignal_WaitAny")]
+               private static extern int WaitAny (IntPtr[] infos, int count, 
int timeout);
+
+               private void AssertValid ()
+               {
+                       if (signal_info == IntPtr.Zero)
+                               throw new ObjectDisposedException 
(GetType().FullName);
+               }
+
+               private unsafe SignalInfo* Info {
+                       get {
+                               AssertValid ();
+                               return (SignalInfo*) signal_info;
+                       }
+               }
+
+               public bool IsSet {
+                       get {
+                               return Count > 0;
+                       }
+               }
+
+               public unsafe bool Reset ()
+               {
+                       int n = Interlocked.Exchange (ref Info->count, 0);
+                       return n != 0;
+               }
+
+               public unsafe int Count {
+                       get {return Info->count;}
+                       set {Interlocked.Exchange (ref Info->count, value);}
+               }
+
+               [Map]
+               struct SignalInfo {
+                       public int signum, count, read_fd, write_fd, 
have_handler;
+                       public IntPtr handler;
+               }
+
+               #region WaitHandle overrides
+               protected unsafe override void Dispose (bool disposing)
+               {
+                       base.Dispose (disposing);
+                       if (signal_info == IntPtr.Zero)
+                               return;
+                       uninstall (signal_info);
+                       signal_info = IntPtr.Zero;
+               }
+
+               public override bool WaitOne ()
+               {
+                       return WaitOne (-1, false);
+               }
+
+               public override bool WaitOne (TimeSpan timeout, bool 
exitContext)
+               {
+                       long ms = (long) timeout.TotalMilliseconds;
+                       if (ms < -1 || ms > Int32.MaxValue)
+                               throw new ArgumentOutOfRangeException 
("timeout");
+                       return WaitOne ((int) ms, exitContext);
+               }
+
+               public override bool WaitOne (int millisecondsTimeout, bool 
exitContext)
+               {
+                       AssertValid ();
+                       if (exitContext)
+                               throw new InvalidOperationException 
("exitContext is not supported");
+                       return WaitAny (new UnixSignal[]{this}, 
millisecondsTimeout);
+               }
+               #endregion
+
+               public static bool WaitAny (UnixSignal[] signals)
+               {
+                       return WaitAny (signals, -1);
+               }
+
+               public static bool WaitAny (UnixSignal[] signals, TimeSpan 
timeout)
+               {
+                       long ms = (long) timeout.TotalMilliseconds;
+                       if (ms < -1 || ms > Int32.MaxValue)
+                               throw new ArgumentOutOfRangeException 
("timeout");
+                       return WaitAny (signals, (int) ms);
+               }
+
+               public static unsafe bool WaitAny (UnixSignal[] signals, int 
millisecondsTimeout)
+               {
+                       IntPtr[] infos = new IntPtr [signals.Length];
+                       for (int i = 0; i < signals.Length; ++i) {
+                               infos [i] = signals [i].signal_info;
+                               if (infos [i] == IntPtr.Zero)
+                                       throw new InvalidOperationException 
("Disposed UnixSignal");
+                       }
+                       int r = WaitAny (infos, infos.Length, 
millisecondsTimeout);
+                       if (r > 0)
+                               return true;
+                       return false;
+               }
+       }
+}
+

Modified: branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix.Native/ChangeLog
===================================================================
--- branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix.Native/ChangeLog   
2008-02-08 14:52:39 UTC (rev 95261)
+++ branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix.Native/ChangeLog   
2008-02-08 15:14:30 UTC (rev 95262)
@@ -1,3 +1,10 @@
+2008-01-28  Jonathan Pryor  <[EMAIL PROTECTED]>
+
+       * Stdlib.cs: Obsolete Stdlib.signal(), as it's not safe; see also:
+         
http://lists.ximian.com/pipermail/mono-devel-list/2008-January/026501.html
+         
http://lists.ximian.com/pipermail/mono-devel-list/2008-January/026503.html
+         Add SetSignalAction() as a replacement.
+
 2008-01-22  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * Syscall.cs: Fix Flock.Equals to avoid NRE (and return false if object 

Modified: branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix.Native/Stdlib.cs
===================================================================
--- branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix.Native/Stdlib.cs   
2008-02-08 14:52:39 UTC (rev 95261)
+++ branches/mono-1-9/mcs/class/Mono.Posix/Mono.Unix.Native/Stdlib.cs   
2008-02-08 15:14:30 UTC (rev 95262)
@@ -267,6 +267,12 @@
        }
 
 
+       public enum SignalAction {
+               Default,
+               Ignore,
+               Error
+       }
+
        //
        // Right now using this attribute gives an assert because it
        // isn't implemented.
@@ -454,6 +460,8 @@
                private static extern IntPtr sys_signal (int signum, IntPtr 
handler);
 
                [CLSCompliant (false)]
+               [Obsolete ("This is not safe; " + 
+                               "use Mono.Unix.UnixSignal for signal delivery 
or SetSignalAction()")]
                public static SignalHandler signal (Signum signum, 
SignalHandler handler)
                {
                        int _sig = NativeConvert.FromSignum (signum);
@@ -494,6 +502,28 @@
 #endif
                }
 
+               public static int SetSignalAction (Signum signal, SignalAction 
action)
+               {
+                       IntPtr handler = IntPtr.Zero;
+                       switch (action) {
+                               case SignalAction.Default:
+                                       handler = _SIG_DFL;
+                                       break;
+                               case SignalAction.Ignore:
+                                       handler = _SIG_IGN;
+                                       break;
+                               case SignalAction.Error:
+                                       handler = _SIG_ERR;
+                                       break;
+                               default:
+                                       throw new ArgumentException ("Invalid 
action value.", "action");
+                       }
+                       IntPtr r = sys_signal (NativeConvert.FromSignum 
(signal), handler);
+                       if (r == _SIG_ERR)
+                               return -1;
+                       return 0;
+               }
+
                [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, 
EntryPoint="raise")]
                private static extern int sys_raise (int sig);
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to