Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/libraries/System.Threading/tests/MutexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.Win32.SafeHandles;
using Xunit;

namespace System.Threading.Tests
Expand Down Expand Up @@ -758,6 +759,30 @@ public void NamedMutex_DisposeWhenLockedRaceTest()
}
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public void CheckWhichWindowsVersionsDistinguishLocalAndGlobalNamespaces()
{
const int MAXIMUM_ALLOWED = 0x02000000;
const int SYNCHRONIZE = 0x00100000;
const int MUTEX_MODIFY_STATE = 0x00000001;
const int MutexAccessRights = MAXIMUM_ALLOWED | SYNCHRONIZE | MUTEX_MODIFY_STATE;

const int ERROR_ALREADY_EXISTS = 0xB7;

string name = Guid.NewGuid().ToString("N");

using SafeWaitHandle m = CreateMutexEx(lpMutexAttributes: 0, @"Local\" + name, flags: 0, MutexAccessRights);
int errorCode = Marshal.GetLastPInvokeError();
Assert.False(m.IsInvalid);
Assert.NotEqual(ERROR_ALREADY_EXISTS, errorCode);

using SafeWaitHandle m2 = CreateMutexEx(lpMutexAttributes: 0, @"Global\" + name, flags: 0, MutexAccessRights);
errorCode = Marshal.GetLastPInvokeError();
Assert.False(m2.IsInvalid);
Assert.NotEqual(ERROR_ALREADY_EXISTS, errorCode);
}

public static TheoryData<string> GetValidNames()
{
var names = new TheoryData<string>() { Guid.NewGuid().ToString("N") };
Expand All @@ -768,6 +793,9 @@ public static TheoryData<string> GetValidNames()
return names;
}

[DllImport("kernel32.dll", EntryPoint = "CreateMutexExW", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern SafeWaitHandle CreateMutexEx(nint lpMutexAttributes, string? name, uint flags, uint desiredAccess);

[DllImport("kernel32.dll")]
private static extern IntPtr GetCurrentThread();

Expand Down
Loading