Skip to content

API Proposal: Port OpenExisting and TryOpenExisting methods for EventWaitHandle/Mutex/Semaphore from .NET Framework #42710

@carlossanlop

Description

@carlossanlop

Background and Motivation

We have customers showing interest in getting the TryOpenExisting methods that take a *Rights parameter ported from .NET Framework (EventWaitHandleRights, MutexRights, SemaphoreRights).

We currently only provide the OpenExisting and TryOpenExisting methods that take a name string as the only parameter, but none that allow specifying rights.

In 3.1, we approved this proposal to port from .NET Framework the methods that could create EventWaitHandle, Mutex and Semaphore objects and take an ACL security object.

Since the original .NET Framework Create methods were static and Windows-specific, we decided to port them into 3.1 inside new static, Windows-specific and ACL-specific classes inside the System.Threading.AccessControl assembly and as part of the System.Threading namespace.

Since the OpenExisting and TryOpenExisting methods are also static in .NET Framework, we could reuse these classes and add the new methods there.

Proposed APIs

namespace System.Threading
{
    public static partial class EventWaitHandleAcl
    {
        public static EventWaitHandle OpenExisting(string name, EventWaitHandleRights rights);
        public static bool TryOpenExisting(string name, EventWaitHandleRights rights, out EventWaitHandle result);
    }
    public static partial class MutexAcl
    {
        public static Mutex OpenExisting(string name, MutexRights rights);
        public static bool TryOpenExisting(string name, MutexRights rights, out Mutex result);
    }
    public static partial class SemaphoreAcl
    {
        public static Semaphore OpenExisting(string name, SemaphoreRights rights);
        public static bool TryOpenExisting(string name, SemaphoreRights rights, out Semaphore result);
    }
}

Usage

using System.Security.AccessControl;
using System.Threading;

...

// EventWaitHandle

EventWaitHandle eventWaitHandle1 = EventWaitHandleAcl.OpenExisting("eventWaitHandle", EventWaitHandleRights.FullControl);

if (EventWaitHandleAcl.TryOpenExisting("eventWaitHandle", EventWaitHandleRights.FullControl, out EventWaitHandle eventWaitHandle2)) { /*...*/ }

// Mutex

Mutex mutex1 = MutexAcl.OpenExisting("mutexName", MutexRights.FullControl);

if (MutexAcl.TryOpenExisting("mutexName", MutexRights.FullControl, out Mutex mutex2)) { /*...*/ }

// Semaphore

Semaphore semaphore1 = SemaphoreAcl.OpenExisting("semaphoreName", SemaphoreRights.FullControl);

if (SemaphoreAcl.TryOpenExisting("semaphoreName", MutexRights.FullControl, out Semaphore semaphore2)) { /*...*/ }

Alternative designs

We also have the ThreadingAclExtensions static class in the System.Threading.AccessControl, where we have the GetAccessControl and SetAccessControl static methods defined for EventWaitHandle/Mutex/Semaphore, which is the class that the original 3.1 proposal was suggesting to use for adding the new APIs, but we opted to create the new classes instead.

Risks

Not sure if a risk, but just in case, I thought it was worth pointing out that the System.Threading.AccessControl assembly is a .NET Standard 2.0 OOB package and Windows specific.

runtime/src/libraries/System.Threading.AccessControl/src/System.Threading.AccessControl.csproj

Sources

Links to docs and source code

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions