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

Changes to properly support Mutexes on NET6.0 #2206

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
78 changes: 78 additions & 0 deletions LiteDB/Client/Shared/MutexGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;

using System.Text;
using System.Threading;
using System.Diagnostics;

#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER
using System.Security.AccessControl;
using System.Security.Principal;
#endif

namespace LiteDB.Client.Shared;

internal static class MutexGenerator
{
#if NET6_0_OR_GREATER
private static Mutex CreateMutexForNet6OrGreater(string name)
{
if (!OperatingSystem.IsWindows())
{
return new Mutex(false, "Global\\" + name + ".Mutex");
}

var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
MutexRights.FullControl, AccessControlType.Allow);

var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);

return MutexAcl.Create(false, "Global\\" + name + ".Mutex", out _, securitySettings);
}
#endif

#if NETSTANDARD2_0_OR_GREATER
private static Mutex CreateMutexForNetStandard(string name)
{
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
MutexRights.FullControl, AccessControlType.Allow);

var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);

var mutex = new Mutex(false, "Global\\" + name + ".Mutex");
ThreadingAclExtensions.SetAccessControl(mutex, securitySettings);

return mutex;
}
#endif

#if NETFRAMEWORK
private static Mutex CreateMutexForNetFramework(string name)
{
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
MutexRights.FullControl, AccessControlType.Allow);

var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);

return new Mutex(false, "Global\\" + name + ".Mutex", out _, securitySettings);
}
#endif

public static Mutex CreateMutex(string name)
{
#if NET6_0_OR_GREATER
return CreateMutexForNet6OrGreater(name);
#endif
#if NETSTANDARD2_0_OR_GREATER
return CreateMutexForNetStandard(name);
#endif
#if NETFRAMEWORK
return CreateMutexForNetFramework(name);
#endif

return new Mutex(false, "Global\\" + name + ".Mutex");
}
}
22 changes: 7 additions & 15 deletions LiteDB/Client/Shared/SharedEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
#if NETFRAMEWORK
using LiteDB.Client.Shared;

#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER
using System.Security.AccessControl;
using System.Security.Principal;
#endif
Expand All @@ -27,17 +29,7 @@ public SharedEngine(EngineSettings settings)

try
{
#if NETFRAMEWORK
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
MutexRights.FullControl, AccessControlType.Allow);

var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);

_mutex = new Mutex(false, "Global\\" + name + ".Mutex", out _, securitySettings);
#else
_mutex = new Mutex(false, "Global\\" + name + ".Mutex");
#endif
_mutex = MutexGenerator.CreateMutex(name);
}
catch (NotSupportedException ex)
{
Expand Down Expand Up @@ -147,7 +139,7 @@ public bool Rollback()
}
}

#endregion
#endregion Transaction Operations

#region Read Operation

Expand Down Expand Up @@ -188,7 +180,7 @@ public bool Pragma(string name, BsonValue value)
}
}

#endregion
#endregion Read Operation

#region Write Operations

Expand Down Expand Up @@ -360,7 +352,7 @@ public bool EnsureIndex(string collection, string name, BsonExpression expressio
}
}

#endregion
#endregion Write Operations

public void Dispose()
{
Expand Down
22 changes: 14 additions & 8 deletions LiteDB/LiteDB.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;netstandard1.3;netstandard2.0</TargetFrameworks>
<AssemblyVersion>5.0.12</AssemblyVersion>
<FileVersion>5.0.12</FileVersion>
<VersionPrefix>5.0.12</VersionPrefix>
<TargetFrameworks>net45;netstandard1.3;netstandard2.0</TargetFrameworks>
<AssemblyVersion>5.0.14</AssemblyVersion>
<FileVersion>5.0.14</FileVersion>
<VersionPrefix>5.0.14</VersionPrefix>
<Authors>Maurício David</Authors>
<Product>LiteDB</Product>
<Description>LiteDB - A lightweight embedded .NET NoSQL document store in a single datafile</Description>
<Copyright>MIT</Copyright>
<NeutralLanguage>en-US</NeutralLanguage>
<Title>LiteDB</Title>
<PackageId>LiteDB</PackageId>
<PackageVersion>5.0.12</PackageVersion>
<PackageVersion>5.0.14</PackageVersion>
<PackageTags>database nosql embedded</PackageTags>
<PackageIcon>icon_64x64.png</PackageIcon>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand All @@ -25,9 +25,9 @@
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.3' ">1.6.1</NetStandardImplicitPackageVersion>
<NoWarn>1701;1702;1705;1591;0618</NoWarn>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\LiteDB.xml</DocumentationFile>
<SignAssembly Condition="'$(OS)'=='Windows_NT'">true</SignAssembly>
<AssemblyOriginatorKeyFile Condition="'$(Configuration)' == 'Release'">LiteDB.snk</AssemblyOriginatorKeyFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<SignAssembly>True</SignAssembly>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<!--
Expand Down Expand Up @@ -65,7 +65,13 @@
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.5.1" />
<PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Threading.AccessControl" Version="4.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="System.Threading.AccessControl" Version="6.0.0" />
</ItemGroup>

<!-- End References -->

</Project>