Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
carlossanlop opened this issue Sep 24, 2020 · 1 comment · Fixed by #43134
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Threading
Milestone

Comments

@carlossanlop
Copy link
Member

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
@carlossanlop carlossanlop added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading labels Sep 24, 2020
@carlossanlop carlossanlop added this to the 6.0.0 milestone Sep 24, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Sep 24, 2020
@jeffschwMSFT jeffschwMSFT removed the untriaged New issue has not been triaged by the area owner label Sep 25, 2020
@carlossanlop carlossanlop added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Sep 25, 2020
@terrajobst terrajobst added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Oct 6, 2020
@terrajobst
Copy link
Member

terrajobst commented Oct 6, 2020

Video

  • Looks good as proposed, follows the pattern
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);
    }
}

@carlossanlop carlossanlop self-assigned this Oct 7, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Threading
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants