public const int WAIT_ABANDONED = 0x00000080;
public const int WAIT_ABANDONED_0 = WAIT_ABANDONED;
+ public const int ERROR_FILE_NOT_FOUND = 2;
+ public const int ERROR_PATH_NOT_FOUND = 3;
+ public const int ERROR_ACCESS_DENIED = 5;
+ public const int ERROR_INVALID_HANDLE = 6;
+ public const int ERROR_SHARING_VIOLATION = 32;
+ public const int ERROR_INVALID_NAME = 0x7B;
+ public const int ERROR_ALREADY_EXISTS = 183;
+ public const int ERROR_FILENAME_EXCED_RANGE = 0xCE;
+
public static bool DuplicateHandle(HandleRef hSourceProcessHandle, SafeHandle hSourceHandle, HandleRef hTargetProcess,
out SafeWaitHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions)
{
SetAuditRule((AuditRule)rule);
}
- internal new void PersistModifications (SafeHandle handle)
+ internal new void Persist (SafeHandle handle)
{
WriteLock();
try {
+++ /dev/null
-//
-// 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)]
- internal static extern IntPtr CreateSemaphore_internal (
- int initialCount, int maximumCount, string name,
- out bool createdNew);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- internal 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
- }
-}
-
System.Security.Permissions/StorePermission.cs
System.Security.Permissions/StorePermissionFlags.cs
System/SRDescriptionAttribute.cs
-System.Threading/Semaphore.cs
System.Threading/ThreadExceptionEventArgs.cs
System.Threading/ThreadExceptionEventHandler.cs
System.Timers/ElapsedEventArgs.cs
../referencesource/System/net/System/Net/NetworkInformation/nodetype.cs
../referencesource/System/net/System/Net/NetworkInformation/pingexception.cs
+../referencesource/System/sys/system/IO/ports/InternalResources.cs
+
../referencesource/System/sys/system/runtime/interopservices/DefaultParameterValueAttribute.cs
../referencesource/System/sys/system/runtime/interopservices/handlecollector.cs
../referencesource/System/sys/system/runtime/versioning/FrameworkName.cs
../referencesource/System/sys/system/threading/Barrier.cs
+../referencesource/System/sys/system/threading/semaphore.cs
+
../referencesource/System/sys/system/windows/markup/ValueSerializerAttribute.cs
../referencesource/System/security/system/security/Authentication/ExtendedProtection/TokenBinding.cs
Assert.IsTrue (created, "Created");
}
+ [Test]
+ public void Constructor_CreatedWithSameName ()
+ {
+ var s1 = new Semaphore(1, 5, "name");
+ bool created;
+ var s2 = new Semaphore(1, 5, "name", out created);
+ Assert.IsFalse (created);
+ }
+
[Test]
[Category ("MobileNotWorking")]
public void Constructor_IntIntStringBoolSecurity ()
System.Security.Cryptography/AsnEncodedData.cs
System.Security.Cryptography/AsnEncodedDataCollection.cs
System.Security.Cryptography/AsnEncodedDataEnumerator.cs
-System.Threading/Semaphore.cs
System.Threading/ThreadExceptionEventArgs.cs
System.Threading/ThreadExceptionEventHandler.cs
System.Timers/ElapsedEventArgs.cs
../referencesource/System/net/System/Net/NetworkInformation/nodetype.cs
../referencesource/System/net/System/Net/NetworkInformation/pingexception.cs
+../referencesource/System/sys/system/IO/ports/InternalResources.cs
+
../referencesource/System/sys/system/runtime/interopservices/DefaultParameterValueAttribute.cs
../referencesource/System/sys/system/runtime/interopservices/handlecollector.cs
../referencesource/System/sys/system/runtime/versioning/FrameworkName.cs
../referencesource/System/sys/system/threading/Barrier.cs
+../referencesource/System/sys/system/threading/semaphore.cs
../referencesource/System/security/system/security/Authentication/ExtendedProtection/TokenBinding.cs
* of icalls, do not require an increment.
*/
#pragma warning disable 169
- private const int mono_corlib_version = 148;
+ private const int mono_corlib_version = 149;
#pragma warning restore 169
[ComVisible (true)]
internal Semaphore(int initialCount, int maxCount) : base() {
lock (this) {
#if MONO
- bool created;
- Handle = System.Threading.Semaphore.CreateSemaphore_internal(initialCount, maxCount, null, out created);
+ int errorCode;
+ Handle = System.Threading.Semaphore.CreateSemaphore_internal(initialCount, maxCount, null, out errorCode);
#else
//
Handle = UnsafeNclNativeMethods.CreateSemaphore(IntPtr.Zero, initialCount, maxCount, IntPtr.Zero);
internal bool ReleaseSemaphore() {
#if MONO
- bool fail;
- var ret = System.Threading.Semaphore.ReleaseSemaphore_internal (Handle, 1, out fail);
- return !fail;
+ int previousCount;
+ return System.Threading.Semaphore.ReleaseSemaphore_internal (Handle, 1, out previousCount);
#else
#if DEBUG
int previousCount;
#endif
internal static String GetMessage(int errorCode)
{
+#if !MONO
StringBuilder sb = new StringBuilder(512);
int result = SafeNativeMethods.FormatMessage(NativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS |
NativeMethods.FORMAT_MESSAGE_FROM_SYSTEM | NativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY,
return s;
}
else
+#endif
{
return SR.GetString(SR.IO_UnknownError, errorCode);
}
+#if MONO
+#undef FEATURE_PAL
+#endif
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
#endif
using System.Runtime.Versioning;
using System.Runtime.ConstrainedExecution;
+ using System.Runtime.CompilerServices;
[HostProtection(Synchronization=true, ExternalThreading=true)]
{
throw new ArgumentException(SR.GetString(SR.Argument_WaitHandleNameTooLong));
}
+
+#if MONO
+ int errorCode;
+ var myHandle = new SafeWaitHandle (CreateSemaphore_internal (initialCount, maximumCount, name, out errorCode), true);
+#else
SafeWaitHandle myHandle = SafeNativeMethods.CreateSemaphore(null, initialCount, maximumCount, name);
+#endif
if (myHandle.IsInvalid)
{
+#if !MONO
int errorCode = Marshal.GetLastWin32Error();
+#endif
if(null != name && 0 != name.Length && NativeMethods.ERROR_INVALID_HANDLE == errorCode)
throw new WaitHandleCannotBeOpenedException(SR.GetString(SR.WaitHandleCannotBeOpenedException_InvalidHandle,name));
throw new ArgumentException(SR.GetString(SR.Argument_WaitHandleNameTooLong));
}
SafeWaitHandle myHandle;
+
+#if MONO
+ int errorCode;
+ myHandle = new SafeWaitHandle (CreateSemaphore_internal (initialCount, maximumCount, name, out errorCode), true);
+#else
#if !FEATURE_PAL && !FEATURE_NETCORE
// For ACL's, get the security descriptor from the SemaphoreSecurity.
if (semaphoreSecurity != null) {
#if !FEATURE_PAL && !FEATURE_NETCORE
}
#endif
+
int errorCode = Marshal.GetLastWin32Error();
+#endif
if (myHandle.IsInvalid)
{
if(null != name && 0 != name.Length && NativeMethods.ERROR_INVALID_HANDLE == errorCode)
}
result = null;
+#if MOBILE
+ throw new NotSupportedException ();
+#else
+#if MONO
+ int errorCode;
+ var myHandle = new SafeWaitHandle (OpenSemaphore_internal (name, rights, out errorCode), true);
+#else
//Pass false to OpenSemaphore to prevent inheritedHandles
#if FEATURE_PAL || FEATURE_NETCORE
const int SYNCHRONIZE = 0x00100000;
SafeWaitHandle myHandle = SafeNativeMethods.OpenSemaphore(SEMAPHORE_MODIFY_STATE | SYNCHRONIZE, false, name);
#else
SafeWaitHandle myHandle = SafeNativeMethods.OpenSemaphore((int) rights, false, name);
+#endif
#endif
if (myHandle.IsInvalid)
{
+#if !MONO
int errorCode = Marshal.GetLastWin32Error();
+#endif
if (NativeMethods.ERROR_FILE_NOT_FOUND == errorCode || NativeMethods.ERROR_INVALID_NAME == errorCode)
return OpenExistingResult.NameNotFound;
}
result = new Semaphore(myHandle);
return OpenExistingResult.Success;
+#endif
}
// the semaphore's count to exceed the maximum count set when Semaphore was created
//Non-Zero return
+#if MONO
+ if (!ReleaseSemaphore_internal(Handle, releaseCount, out previousCount))
+#else
if (!SafeNativeMethods.ReleaseSemaphore(SafeWaitHandle, releaseCount, out previousCount))
+#endif
{
throw new SemaphoreFullException();
}
semaphoreSecurity.Persist(SafeWaitHandle);
}
#endif
+
+#if MONO
+ [MethodImplAttribute(MethodImplOptions.InternalCall)]
+ internal static extern IntPtr CreateSemaphore_internal (
+ int initialCount, int maximumCount, string name, out int errorCode);
+
+ [MethodImplAttribute(MethodImplOptions.InternalCall)]
+ internal static extern bool ReleaseSemaphore_internal (
+ IntPtr handle, int releaseCount, out int previousCount);
+
+ [MethodImplAttribute (MethodImplOptions.InternalCall)]
+ private static extern IntPtr OpenSemaphore_internal (string name, SemaphoreRights rights, out int errorCode);
+#endif
}
}
* Changes which are already detected at runtime, like the addition
* of icalls, do not require an increment.
*/
-#define MONO_CORLIB_VERSION 148
+#define MONO_CORLIB_VERSION 149
typedef struct
{
ICALL(NATIVEC_5, "SetEvent_internal", ves_icall_System_Threading_Events_SetEvent_internal)
ICALL_TYPE(SEMA, "System.Threading.Semaphore", SEMA_1)
-ICALL(SEMA_1, "CreateSemaphore_internal(int,int,string,bool&)", ves_icall_System_Threading_Semaphore_CreateSemaphore_internal)
-ICALL(SEMA_2, "OpenSemaphore_internal(string,System.Security.AccessControl.SemaphoreRights,System.IO.MonoIOError&)", ves_icall_System_Threading_Semaphore_OpenSemaphore_internal)
-ICALL(SEMA_3, "ReleaseSemaphore_internal(intptr,int,bool&)", ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal)
+ICALL(SEMA_1, "CreateSemaphore_internal(int,int,string,int&)", ves_icall_System_Threading_Semaphore_CreateSemaphore_internal)
+ICALL(SEMA_2, "OpenSemaphore_internal(string,System.Security.AccessControl.SemaphoreRights,int&)", ves_icall_System_Threading_Semaphore_OpenSemaphore_internal)
+ICALL(SEMA_3, "ReleaseSemaphore_internal(intptr,int,int&)", ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal)
ICALL_TYPE(THREAD, "System.Threading.Thread", THREAD_1)
ICALL(THREAD_1, "Abort_internal(System.Threading.InternalThread,object)", ves_icall_System_Threading_Thread_Abort)
HANDLE ves_icall_System_Threading_Mutex_CreateMutex_internal(MonoBoolean owned, MonoString *name, MonoBoolean *created);
MonoBoolean ves_icall_System_Threading_Mutex_ReleaseMutex_internal (HANDLE handle );
HANDLE ves_icall_System_Threading_Mutex_OpenMutex_internal (MonoString *name, gint32 rights, gint32 *error);
-HANDLE ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, MonoBoolean *created);
-gint32 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (HANDLE handle, gint32 releaseCount, MonoBoolean *fail);
+HANDLE ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, gint32 *error);
+MonoBoolean ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (HANDLE handle, gint32 releaseCount, gint32 *prevcount);
HANDLE ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error);
HANDLE ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name, MonoBoolean *created);
gboolean ves_icall_System_Threading_Events_SetEvent_internal (HANDLE handle);
}
-HANDLE ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, MonoBoolean *created)
+HANDLE ves_icall_System_Threading_Semaphore_CreateSemaphore_internal (gint32 initialCount, gint32 maximumCount, MonoString *name, gint32 *error)
{
HANDLE sem;
- *created = TRUE;
-
if (name == NULL) {
sem = CreateSemaphore (NULL, initialCount, maximumCount, NULL);
} else {
sem = CreateSemaphore (NULL, initialCount, maximumCount,
mono_string_chars (name));
-
- if (GetLastError () == ERROR_ALREADY_EXISTS) {
- *created = FALSE;
- }
}
+ *error = GetLastError ();
return(sem);
}
-gint32 ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (HANDLE handle, gint32 releaseCount, MonoBoolean *fail)
+MonoBoolean ves_icall_System_Threading_Semaphore_ReleaseSemaphore_internal (HANDLE handle, gint32 releaseCount, gint32 *prevcount)
{
- gint32 prevcount;
-
- *fail = !ReleaseSemaphore (handle, releaseCount, &prevcount);
-
- return (prevcount);
+ return ReleaseSemaphore (handle, releaseCount, prevcount);
}
HANDLE ves_icall_System_Threading_Semaphore_OpenSemaphore_internal (MonoString *name, gint32 rights, gint32 *error)
{
- HANDLE ret;
-
- *error = ERROR_SUCCESS;
-
- ret = OpenSemaphore (rights, FALSE, mono_string_chars (name));
- if (ret == NULL) {
- *error = GetLastError ();
- }
-
- return(ret);
+ HANDLE sem;
+
+ sem = OpenSemaphore (rights, FALSE, mono_string_chars (name));
+ *error = GetLastError ();
+
+ return(sem);
}
HANDLE ves_icall_System_Threading_Events_CreateEvent_internal (MonoBoolean manual, MonoBoolean initial, MonoString *name, MonoBoolean *created)