Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,236 @@
//
// Barrier.cs
//
// Author:
// Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
//
// Copyright (c) 2009 Jérémie "Garuma" Laval
//
// 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.
#if NET_4_0
using System;
using System.Diagnostics;
namespace System.Threading
{
[DebuggerDisplay ("Participant Count={ParticipantCount},Participants Remaining={ParticipantsRemaining}")]
public class Barrier : IDisposable
{
const int MaxParticipants = 32767;
Action<Barrier> postPhaseAction;
int participants;
bool cleaned;
CountdownEvent cntd;
ManualResetEventSlim postPhaseEvt = new ManualResetEventSlim ();
long phase;
public Barrier (int participantCount)
: this (participantCount, null)
{
}
public Barrier (int participantCount, Action<Barrier> postPhaseAction)
{
if (participantCount < 0 || participantCount > MaxParticipants)
throw new ArgumentOutOfRangeException ("participantCount");
this.participants = participantCount;
this.postPhaseAction = postPhaseAction;
InitCountdownEvent ();
}
public void Dispose ()
{
Dispose (true);
}
protected virtual void Dispose (bool disposing)
{
if (disposing){
if (cntd != null){
cntd.Dispose ();
cntd = null;
}
postPhaseAction = null;
cleaned = true;
}
}
void InitCountdownEvent ()
{
postPhaseEvt = new ManualResetEventSlim (false);
cntd = new CountdownEvent (participants);
}
public long AddParticipant ()
{
return AddParticipants (1);
}
static Exception GetDisposed ()
{
return new ObjectDisposedException ("Barrier");
}
public long AddParticipants (int participantCount)
{
if (cleaned)
throw GetDisposed ();
if (participantCount < 0)
throw new InvalidOperationException ();
// Basically, we try to add ourselves and return
// the phase. If the call return false, we repeatdly try
// to add ourselves for the next phase
do {
if (cntd.TryAddCount (participantCount)) {
Interlocked.Add (ref participants, participantCount);
return phase;
}
} while (true);
}
public void RemoveParticipant ()
{
RemoveParticipants (1);
}
public void RemoveParticipants (int participantCount)
{
if (cleaned)
throw GetDisposed ();
if (participantCount < 0)
throw new ArgumentOutOfRangeException ("participantCount");
if (cntd.Signal (participantCount))
PostPhaseAction (postPhaseEvt);
Interlocked.Add (ref participants, -participantCount);
}
public void SignalAndWait ()
{
if (cleaned)
throw GetDisposed ();
SignalAndWait ((c) => { c.Wait (); return true; });
}
public void SignalAndWait (CancellationToken cancellationToken)
{
if (cleaned)
throw GetDisposed ();
SignalAndWait ((c) => { c.Wait (cancellationToken); return true; });
}
public bool SignalAndWait (int millisecondsTimeout)
{
if (cleaned)
throw GetDisposed ();
return SignalAndWait ((c) => c.Wait (millisecondsTimeout));
}
public bool SignalAndWait (TimeSpan timeout)
{
if (cleaned)
throw GetDisposed ();
return SignalAndWait ((c) => c.Wait (timeout));
}
public bool SignalAndWait (int millisecondsTimeout, CancellationToken cancellationToken)
{
if (cleaned)
throw GetDisposed ();
return SignalAndWait ((c) => c.Wait (millisecondsTimeout, cancellationToken));
}
public bool SignalAndWait (TimeSpan timeout, CancellationToken cancellationToken)
{
if (cleaned)
throw GetDisposed ();
return SignalAndWait ((c) => c.Wait (timeout, cancellationToken));
}
bool SignalAndWait (Func<CountdownEvent, bool> associate)
{
bool result;
CountdownEvent temp = cntd;
ManualResetEventSlim evt = postPhaseEvt;
if (!temp.Signal ()) {
result = Wait (associate, temp, evt);
} else {
result = true;
PostPhaseAction (evt);
}
return result;
}
bool Wait (Func<CountdownEvent, bool> associate, CountdownEvent temp, ManualResetEventSlim evt)
{
if (!associate (temp))
return false;
evt.Wait ();
return true;
}
void PostPhaseAction (ManualResetEventSlim evt)
{
if (postPhaseAction != null) {
try {
postPhaseAction (this);
} catch (Exception e) {
throw new BarrierPostPhaseException (e);
}
}
InitCountdownEvent ();
phase++;
evt.Set ();
}
public long CurrentPhaseNumber {
get {
return phase;
}
}
public int ParticipantCount {
get {
return participants;
}
}
[MonoTODO]
public int ParticipantsRemaining {
get {
return -1;
}
}
}
}
#endif

View File

@@ -0,0 +1,67 @@
//
// BarrierPostPhaseException.cs
//
// Author:
// Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
//
// Copyright (c) 2010 Jérémie "Garuma" Laval
//
// 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.
#if NET_4_0
using System;
using System.Runtime.Serialization;
namespace System.Threading
{
[Serializable]
public class BarrierPostPhaseException : Exception
{
const string DefaultMessage = "An exception has occured during the postphase action";
public BarrierPostPhaseException () : this (DefaultMessage)
{
}
public BarrierPostPhaseException (Exception innerException)
: this (DefaultMessage, innerException)
{
}
public BarrierPostPhaseException (string message) : base (message)
{
}
public BarrierPostPhaseException (string message, Exception innerException)
: base (message, innerException)
{
}
protected BarrierPostPhaseException (SerializationInfo info, StreamingContext context)
: base (info, context)
{
}
}
}
#endif

View File

@@ -0,0 +1,57 @@
2010-04-15 Jérémie Laval <jeremie.laval@gmail.com>
* Barrier.cs: Add BOOTSTRAP_NET_4_0
2010-04-06 Jb Evain <jbevain@novell.com>
* SemaphoreFullException.cs: moved to corlib in net_4_0.
2010-03-02 Jérémie Laval <jeremie.laval@gmail.com>
* Barrier.cs: Remove dead comments
2010-02-25 Jérémie Laval <jeremie.laval@gmail.com>
* Barrier.cs: Fix NRE with postPhaseAction delegate
Fix possible deadlock when removing participant(s)
2009-12-11 Miguel de Icaza <miguel@novell.com>
* Barrier.cs: Implement IDisposable, add a bunch of IDisposable
checks and some checks from the docs.
2009-08-19 Jérémie Laval <jeremie.laval@gmail.com>
* Barrier.cs: Fix Barrier to be really thread-safe.
Remove deadlocking.
2009-08-11 Jérémie Laval <jeremie.laval@gmail.com>
* Barrier.cs: added.
2006-09-28 Andrew Skiba <andrews@mainsoft.com>
* Semaphore.cs: TARGET_JVM
2005-12-23 Dick Porter <dick@ximian.com>
* Semaphore.cs: Implement OpenExisting
2005-11-26 Dick Porter <dick@ximian.com>
* Semaphore.cs: Implemented with icalls
2005-11-17 Sebastien Pouliot <sebastien@ximian.com>
* Semaphore.cs: New (2.0). Incomplete (missing runtime support).
* SemaphoreFullException.cs: New (2.0).
2001-09-21 Dick Porter <dick@ximian.com>
* ThreadExceptionEventArgs.cs: Implemented
2001-09-13 Dick Porter <dick@ximian.com>
* ThreadExceptionEventArgs.cs, ThreadExceptionEventHandler.cs:
More System.Threading stubs, in the System assembly.

View File

@@ -0,0 +1,221 @@
//
// System.Threading.Semaphore.cs
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// 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.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Permissions;
using System.Runtime.CompilerServices;
using System.IO;
namespace System.Threading {
[ComVisible (false)]
public sealed class Semaphore : WaitHandle {
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern IntPtr CreateSemaphore_internal (
int initialCount, int maximumCount, string name,
out bool createdNew);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern int ReleaseSemaphore_internal (
IntPtr handle, int releaseCount, out bool fail);
[MethodImplAttribute (MethodImplOptions.InternalCall)]
private static extern IntPtr OpenSemaphore_internal (string name, SemaphoreRights rights, out MonoIOError error);
private Semaphore (IntPtr handle)
{
Handle = handle;
}
public Semaphore (int initialCount, int maximumCount)
: this (initialCount, maximumCount, null)
{
}
public Semaphore (int initialCount, int maximumCount, string name)
{
if (initialCount < 0)
throw new ArgumentOutOfRangeException ("initialCount", "< 0");
if (maximumCount < 1)
throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
if (initialCount > maximumCount)
throw new ArgumentException ("initialCount > maximumCount");
bool created;
Handle = CreateSemaphore_internal (initialCount,
maximumCount, name,
out created);
}
public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew)
: this (initialCount, maximumCount, name, out createdNew, null)
{
}
[MonoTODO ("CreateSemaphore_internal does not support access control, semaphoreSecurity is ignored")]
public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew,
SemaphoreSecurity semaphoreSecurity)
{
if (initialCount < 0)
throw new ArgumentOutOfRangeException ("initialCount", "< 0");
if (maximumCount < 1)
throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
if (initialCount > maximumCount)
throw new ArgumentException ("initialCount > maximumCount");
Handle = CreateSemaphore_internal (initialCount,
maximumCount, name,
out createdNew);
}
public SemaphoreSecurity GetAccessControl ()
{
return new SemaphoreSecurity (SafeWaitHandle,
AccessControlSections.Owner |
AccessControlSections.Group |
AccessControlSections.Access);
}
[PrePrepareMethod]
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public int Release ()
{
return (Release (1));
}
[ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
public int Release (int releaseCount)
{
if (releaseCount < 1)
throw new ArgumentOutOfRangeException ("releaseCount");
int ret;
bool fail;
ret = ReleaseSemaphore_internal (Handle, releaseCount,
out fail);
if (fail) {
throw new SemaphoreFullException ();
}
return (ret);
}
public void SetAccessControl (SemaphoreSecurity semaphoreSecurity)
{
if (semaphoreSecurity == null)
throw new ArgumentNullException ("semaphoreSecurity");
semaphoreSecurity.PersistModifications (SafeWaitHandle);
}
// static methods
#if !MOBILE
public static Semaphore OpenExisting (string name)
{
return OpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify);
}
public static Semaphore OpenExisting (string name, SemaphoreRights rights)
{
if (name == null)
throw new ArgumentNullException ("name");
if ((name.Length ==0) || (name.Length > 260))
throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
MonoIOError error;
IntPtr handle = OpenSemaphore_internal (name, rights,
out error);
if (handle == (IntPtr)null) {
if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Semaphore handle does not exist: ") + name);
} else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
throw new UnauthorizedAccessException ();
} else {
throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
}
}
return(new Semaphore (handle));
}
[SecurityPermissionAttribute (SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static bool TryOpenExisting (string name, out Semaphore result)
{
return TryOpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify, out result);
}
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static bool TryOpenExisting (string name, SemaphoreRights rights, out Semaphore result)
{
if (name == null)
throw new ArgumentNullException ("name");
if ((name.Length == 0) || (name.Length > 260))
throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
MonoIOError error;
IntPtr handle = OpenSemaphore_internal (name, rights, out error);
if (handle == (IntPtr)null) {
result = null;
return false;
}
result = new Semaphore (handle);
return true;
}
#else
public static Semaphore OpenExisting (string name)
{
throw new NotSupportedException ();
}
public static Semaphore OpenExisting (string name, SemaphoreRights rights)
{
throw new NotSupportedException ();
}
public static bool TryOpenExisting (string name, out Semaphore result)
{
throw new NotSupportedException ();
}
public static bool TryOpenExisting (string name, SemaphoreRights rights, out Semaphore result)
{
throw new NotSupportedException ();
}
#endif
}
}

View File

@@ -0,0 +1,71 @@
//
// System.Threading.SemaphoreFullException.cs
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// 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.
//
#if (!NET_4_0 && !INSIDE_CORLIB) || (NET_4_0 && INSIDE_CORLIB)
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
namespace System.Threading {
#if NET_4_0 && INSIDE_CORLIB
[TypeForwardedFrom (Consts.AssemblySystem_2_0)]
#endif
[ComVisible (false)]
[Serializable]
public class SemaphoreFullException : SystemException {
public SemaphoreFullException ()
: base (Locale.GetText ("Exceeding maximum."))
{
}
public SemaphoreFullException (string message)
: base (message)
{
}
public SemaphoreFullException (string message, Exception innerException)
: base (message, innerException)
{
}
protected SemaphoreFullException (SerializationInfo info, StreamingContext context)
: base (info, context)
{
}
}
}
#elif NET_4_0 && !INSIDE_CORLIB
using System.Runtime.CompilerServices;
using System.Threading;
[assembly: TypeForwardedTo (typeof (SemaphoreFullException))]
#endif

View File

@@ -0,0 +1,48 @@
//
// System.Threading.ThreadExceptionEventArgs.cs
//
// Author:
// Dick Porter (dick@ximian.com)
//
// (C) Ximian, Inc. http://www.ximian.com
//
//
// 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.
//
namespace System.Threading
{
public class ThreadExceptionEventArgs : EventArgs
{
private Exception exception;
public ThreadExceptionEventArgs(Exception t) {
exception=t;
}
public Exception Exception {
get {
return(exception);
}
}
}
}

View File

@@ -0,0 +1,35 @@
//
// System.Threading.ThreadExceptionEventHandler.cs
//
// Author:
// Dick Porter (dick@ximian.com)
//
// (C) Ximian, Inc. http://www.ximian.com
//
//
// 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.
//
namespace System.Threading
{
public delegate void ThreadExceptionEventHandler(object sender, ThreadExceptionEventArgs e);
}