Skip to content

Commit b90980a

Browse files
authored
Merge pull request #1462 from microsoft/updateDocs
Improve docs around `ReentrantSemaphore`'s reentrancy detection
2 parents 5311393 + 8ef5a25 commit b90980a

7 files changed

Lines changed: 181 additions & 171 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"omnisharp.enableEditorConfigSupport": true,
77
"omnisharp.enableRoslynAnalyzers": true,
88
"dotnet.completion.showCompletionItemsFromUnimportedNamespaces": true,
9-
"dotnet.defaultSolution": "Microsoft.VisualStudio.Threading.sln",
9+
"dotnet.defaultSolution": "Microsoft.VisualStudio.Threading.slnx",
1010
"editor.formatOnSave": true,
1111
"[xml]": {
1212
"editor.wordWrap": "off"

Microsoft.VisualStudio.Threading.sln

Lines changed: 0 additions & 170 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<Solution>
2+
<Configurations>
3+
<Platform Name="Any CPU" />
4+
<Platform Name="NonWindows" />
5+
<Platform Name="x64" />
6+
<Platform Name="x86" />
7+
</Configurations>
8+
<Folder Name="/Solution Items/">
9+
<File Path=".editorconfig" />
10+
<File Path="azure-pipelines/build.yml" />
11+
<File Path="Directory.Build.props" />
12+
<File Path="Directory.Build.targets" />
13+
<File Path="Directory.Packages.props" />
14+
<File Path="global.json" />
15+
<File Path="nuget.config" />
16+
<File Path="stylecop.json" />
17+
<File Path="version.json" />
18+
</Folder>
19+
<Folder Name="/src/">
20+
<File Path="src/.editorconfig" />
21+
<File Path="src/AssemblyInfo.cs" />
22+
<File Path="src/Directory.Build.props" />
23+
<File Path="src/Directory.Build.targets" />
24+
<Project Path="src/Microsoft.VisualStudio.Threading.Analyzers.CodeFixes/Microsoft.VisualStudio.Threading.Analyzers.CodeFixes.csproj" />
25+
<Project Path="src/Microsoft.VisualStudio.Threading.Analyzers.CSharp/Microsoft.VisualStudio.Threading.Analyzers.CSharp.csproj" />
26+
<Project Path="src/Microsoft.VisualStudio.Threading.Analyzers.VisualBasic/Microsoft.VisualStudio.Threading.Analyzers.VisualBasic.csproj" />
27+
<Project Path="src/Microsoft.VisualStudio.Threading.Analyzers/Microsoft.VisualStudio.Threading.Analyzers.csproj" />
28+
<Project Path="src/Microsoft.VisualStudio.Threading.JointPackage/Microsoft.VisualStudio.Threading.JointPackage.csproj" />
29+
<Project Path="src/Microsoft.VisualStudio.Threading/Microsoft.VisualStudio.Threading.csproj" />
30+
<Project Path="src/SosThreadingTools/SosThreadingTools.csproj">
31+
<Build Solution="*|NonWindows" Project="false" />
32+
</Project>
33+
</Folder>
34+
<Folder Name="/test/">
35+
<File Path="test/.editorconfig" />
36+
<File Path="test/Directory.Build.props" />
37+
<File Path="test/Directory.Build.targets" />
38+
<Project Path="samples/samples.csproj" />
39+
<Project Path="test/IsolatedTestHost/IsolatedTestHost.csproj" />
40+
<Project Path="test/Microsoft.VisualStudio.Threading.Analyzers.Tests/Microsoft.VisualStudio.Threading.Analyzers.Tests.csproj" />
41+
<Project Path="test/Microsoft.VisualStudio.Threading.Tests.Win7RegistryWatcher/Microsoft.VisualStudio.Threading.Tests.Win7RegistryWatcher.csproj" />
42+
<Project Path="test/Microsoft.VisualStudio.Threading.Tests/Microsoft.VisualStudio.Threading.Tests.csproj" />
43+
</Folder>
44+
</Solution>

samples/.editorconfig

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[*.cs]
2+
3+
indent_style = space
4+
5+
# SA1108: Block statements should not contain embedded comments
6+
dotnet_diagnostic.SA1108.severity = none
7+
8+
# SA1123: Do not place regions within elements
9+
dotnet_diagnostic.SA1123.severity = none
10+
11+
# SA1124: Do not use regions
12+
dotnet_diagnostic.SA1124.severity = none
13+
14+
# SA1200: Using directives should be placed correctly
15+
dotnet_diagnostic.SA1200.severity = none
16+
17+
# SA1201: Elements should appear in the correct order
18+
dotnet_diagnostic.SA1201.severity = silent
19+
20+
# SA1205: Partial elements should declare access
21+
dotnet_diagnostic.SA1205.severity = none
22+
23+
# SA1400: Access modifier should be declared
24+
dotnet_diagnostic.SA1400.severity = none
25+
26+
# SA1402: File may only contains a single type
27+
dotnet_diagnostic.SA1402.severity = none
28+
29+
# SA1403: File may only contain a single namespace
30+
dotnet_diagnostic.SA1403.severity = none
31+
32+
# SA1502: Element should not be on a single line
33+
dotnet_diagnostic.SA1502.severity = none
34+
35+
# SA1515: Single-line comment should be preceded by blank line
36+
dotnet_diagnostic.SA1515.severity = none
37+
38+
# SA1516: Elements should be separated by blank line
39+
dotnet_diagnostic.SA1516.severity = none
40+
41+
# SA1600: Elements should be documented
42+
dotnet_diagnostic.SA1600.severity = silent
43+
44+
# SA1601: Partial elements should be documented
45+
dotnet_diagnostic.SA1601.severity = silent
46+
47+
# SA1649: File name should match first type name
48+
dotnet_diagnostic.SA1649.severity = none
49+
50+
# IDE0051: Remove unused private members
51+
dotnet_diagnostic.IDE0051.severity = none
52+
53+
# CS1591: Missing XML comment for publicly visible type or member
54+
dotnet_diagnostic.CS1591.severity = silent
55+
56+
# CA1822: Mark members as static
57+
dotnet_diagnostic.CA1822.severity = silent
58+
59+
# CA1062: Validate arguments of public methods
60+
dotnet_diagnostic.CA1062.severity = silent

samples/ApiSamples.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Microsoft.VisualStudio.Threading;
7+
8+
public class SuppressRelevanceSample
9+
{
10+
private readonly ReentrantSemaphore semaphore = ReentrantSemaphore.Create(mode: ReentrantSemaphore.ReentrancyMode.NotAllowed);
11+
12+
#region SuppressRelevance
13+
public async Task DoSomethingAsync()
14+
{
15+
await this.semaphore.ExecuteAsync(async delegate
16+
{
17+
// field access under the semaphore
18+
// ...
19+
await Task.Yield(); // represents some async work
20+
21+
// Fire and forget code that uses the semaphore, but should *not*
22+
// inherit our own posession of the semaphore.
23+
using (this.semaphore.SuppressRelevance())
24+
{
25+
this.DoSomethingLaterAsync().Forget(); // Don't await this, or a deadlock will occur.
26+
}
27+
});
28+
}
29+
30+
private async Task DoSomethingLaterAsync()
31+
{
32+
// This semaphore use will not be seen as nested because of our caller's wrapping
33+
// the call in SuppressRelevance.
34+
// So instead of throwing, it will block till its caller releases the semaphore.
35+
await this.semaphore.ExecuteAsync(async delegate
36+
{
37+
// Whatever
38+
await Task.Yield(); // represents some async work
39+
});
40+
}
41+
#endregion
42+
}

samples/samples.csproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net8.0;net472</TargetFrameworks>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\src\Microsoft.VisualStudio.Threading\Microsoft.VisualStudio.Threading.csproj" />
10+
<!-- <ProjectReference Include="$(RepoRootPath)src\Microsoft.VisualStudio.Threading.Analyzers.CodeFixes\Microsoft.VisualStudio.Threading.Analyzers.CodeFixes.csproj">
11+
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
12+
<OutputItemType>Analyzer</OutputItemType>
13+
</ProjectReference>
14+
<ProjectReference Include="$(RepoRootPath)src\Microsoft.VisualStudio.Threading.Analyzers.CSharp\Microsoft.VisualStudio.Threading.Analyzers.CSharp.csproj">
15+
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
16+
<OutputItemType>Analyzer</OutputItemType>
17+
</ProjectReference> -->
18+
</ItemGroup>
19+
20+
</Project>

0 commit comments

Comments
 (0)