@@ -0,0 +1,8 | |||
|
1 | using System; | |
|
2 | ||
|
3 | namespace Implab { | |
|
4 | public interface IComponentContainer { | |
|
5 | void Add(IDisposable component); | |
|
6 | } | |
|
7 | } | |
|
8 |
@@ -0,0 +1,48 | |||
|
1 | using System; | |
|
2 | using Implab.Parallels; | |
|
3 | using System.Threading; | |
|
4 | ||
|
5 | namespace Implab { | |
|
6 | public class MTComponentContainer : IComponentContainer, IDisposable { | |
|
7 | static readonly MTComponentContainer _appContainer; | |
|
8 | ||
|
9 | static MTComponentContainer() { | |
|
10 | _appContainer = new MTComponentContainer(); | |
|
11 | AppDomain.CurrentDomain.ProcessExit += HandleProcessExit; | |
|
12 | } | |
|
13 | ||
|
14 | public static MTComponentContainer AppContainer { | |
|
15 | get { | |
|
16 | return _appContainer; | |
|
17 | } | |
|
18 | } | |
|
19 | ||
|
20 | bool m_disposed; | |
|
21 | readonly MTQueue<IDisposable> m_components = new MTQueue<IDisposable>(); | |
|
22 | ||
|
23 | public void Add(IDisposable item) { | |
|
24 | Safe.ArgumentNotNull(item, "item"); | |
|
25 | Thread.MemoryBarrier(); | |
|
26 | if (m_disposed) { | |
|
27 | item.Dispose(); | |
|
28 | } else { | |
|
29 | m_components.Enqueue(item); | |
|
30 | if (m_disposed && m_components.TryDequeue(out item)) | |
|
31 | item.Dispose(); | |
|
32 | } | |
|
33 | } | |
|
34 | ||
|
35 | public void Dispose() { | |
|
36 | m_disposed = true; | |
|
37 | IDisposable item; | |
|
38 | while (m_components.TryDequeue(out item)) | |
|
39 | item.Dispose(); | |
|
40 | } | |
|
41 | ||
|
42 | static void HandleProcessExit (object sender, EventArgs e) | |
|
43 | { | |
|
44 | _appContainer.Dispose(); | |
|
45 | } | |
|
46 | } | |
|
47 | } | |
|
48 |
@@ -0,0 +1,46 | |||
|
1 | <?xml version="1.0" encoding="utf-8"?> | |
|
2 | <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
|
3 | <PropertyGroup> | |
|
4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | |
|
5 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | |
|
6 | <ProductVersion>8.0.30703</ProductVersion> | |
|
7 | <SchemaVersion>2.0</SchemaVersion> | |
|
8 | <ProjectGuid>{15DD7123-D504-4627-8B4F-D00C7F04D033}</ProjectGuid> | |
|
9 | <OutputType>Exe</OutputType> | |
|
10 | <RootNamespace>MonoPlay</RootNamespace> | |
|
11 | <AssemblyName>MonoPlay</AssemblyName> | |
|
12 | <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | |
|
13 | </PropertyGroup> | |
|
14 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | |
|
15 | <DebugSymbols>true</DebugSymbols> | |
|
16 | <DebugType>full</DebugType> | |
|
17 | <Optimize>false</Optimize> | |
|
18 | <OutputPath>bin\Debug</OutputPath> | |
|
19 | <DefineConstants>DEBUG;TRACE;</DefineConstants> | |
|
20 | <ErrorReport>prompt</ErrorReport> | |
|
21 | <WarningLevel>4</WarningLevel> | |
|
22 | <ConsolePause>false</ConsolePause> | |
|
23 | </PropertyGroup> | |
|
24 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | |
|
25 | <DebugType>full</DebugType> | |
|
26 | <Optimize>true</Optimize> | |
|
27 | <OutputPath>bin\Release</OutputPath> | |
|
28 | <ErrorReport>prompt</ErrorReport> | |
|
29 | <WarningLevel>4</WarningLevel> | |
|
30 | <Externalconsole>true</Externalconsole> | |
|
31 | </PropertyGroup> | |
|
32 | <ItemGroup> | |
|
33 | <Reference Include="System" /> | |
|
34 | </ItemGroup> | |
|
35 | <ItemGroup> | |
|
36 | <Compile Include="Program.cs" /> | |
|
37 | <Compile Include="Properties\AssemblyInfo.cs" /> | |
|
38 | </ItemGroup> | |
|
39 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> | |
|
40 | <ItemGroup> | |
|
41 | <ProjectReference Include="..\Implab\Implab.csproj"> | |
|
42 | <Project>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</Project> | |
|
43 | <Name>Implab</Name> | |
|
44 | </ProjectReference> | |
|
45 | </ItemGroup> | |
|
46 | </Project> No newline at end of file |
@@ -0,0 +1,31 | |||
|
1 | using System; | |
|
2 | using Implab.Diagnostics; | |
|
3 | using Implab.Parallels; | |
|
4 | using Implab; | |
|
5 | ||
|
6 | namespace MonoPlay { | |
|
7 | class MainClass { | |
|
8 | public static void Main(string[] args) { | |
|
9 | var listener = new ConsoleTraceListener(true); | |
|
10 | listener.Subscribe<TraceEvent>(); | |
|
11 | ||
|
12 | MTComponentContainer.AppContainer.Add(listener); | |
|
13 | ||
|
14 | TraceLog.StartLogicalOperation("program"); | |
|
15 | ||
|
16 | Console.WriteLine("Hello World!"); | |
|
17 | ||
|
18 | TraceLog.StartLogicalOperation("async"); | |
|
19 | AsyncPool.Invoke(() => { | |
|
20 | TraceLog.TraceInformation("Hello async"); | |
|
21 | TraceLog.StartLogicalOperation(); | |
|
22 | return 0; | |
|
23 | }) | |
|
24 | .EndLogicalOperation() | |
|
25 | .Join(); | |
|
26 | ||
|
27 | TraceLog.EndLogicalOperation(); | |
|
28 | ||
|
29 | } | |
|
30 | } | |
|
31 | } |
@@ -0,0 +1,27 | |||
|
1 | using System.Reflection; | |
|
2 | using System.Runtime.CompilerServices; | |
|
3 | ||
|
4 | // Information about this assembly is defined by the following attributes. | |
|
5 | // Change them to the values specific to your project. | |
|
6 | ||
|
7 | [assembly: AssemblyTitle("MonoPlay")] | |
|
8 | [assembly: AssemblyDescription("")] | |
|
9 | [assembly: AssemblyConfiguration("")] | |
|
10 | [assembly: AssemblyCompany("")] | |
|
11 | [assembly: AssemblyProduct("")] | |
|
12 | [assembly: AssemblyCopyright("sergey")] | |
|
13 | [assembly: AssemblyTrademark("")] | |
|
14 | [assembly: AssemblyCulture("")] | |
|
15 | ||
|
16 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". | |
|
17 | // The form "{Major}.{Minor}.*" will automatically update the build and revision, | |
|
18 | // and "{Major}.{Minor}.{Build}.*" will update just the revision. | |
|
19 | ||
|
20 | [assembly: AssemblyVersion("1.0.*")] | |
|
21 | ||
|
22 | // The following attributes are used to specify the signing key for the assembly, | |
|
23 | // if desired. See the Mono documentation for more information about signing. | |
|
24 | ||
|
25 | //[assembly: AssemblyDelaySign(false)] | |
|
26 | //[assembly: AssemblyKeyFile("")] | |
|
27 |
@@ -1,15 +1,16 | |||
|
1 | 1 | syntax: glob |
|
2 | 2 | Implab.Test/bin/ |
|
3 | 3 | *.user |
|
4 | 4 | Implab.Test/obj/ |
|
5 | 5 | *.userprefs |
|
6 | 6 | Implab/bin/ |
|
7 | 7 | Implab/obj/ |
|
8 | 8 | TestResults/ |
|
9 | 9 | Implab.Fx/obj/ |
|
10 | 10 | Implab.Fx/bin/ |
|
11 | 11 | Implab.Fx.Test/bin/ |
|
12 | 12 | Implab.Fx.Test/obj/ |
|
13 | 13 | _ReSharper.Implab/ |
|
14 | 14 | Implab.Diagnostics.Interactive/bin/ |
|
15 | 15 | Implab.Diagnostics.Interactive/obj/ |
|
16 | MonoPlay/bin/ |
@@ -1,287 +1,289 | |||
|
1 | 1 | ο»Ώ |
|
2 | 2 | Microsoft Visual Studio Solution File, Format Version 11.00 |
|
3 | 3 | # Visual Studio 2010 |
|
4 | 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab", "Implab\Implab.csproj", "{F550F1F8-8746-4AD0-9614-855F4C4B7F05}" |
|
5 | 5 | EndProject |
|
6 | 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{CE8D8D18-437A-445C-B662-4C2CE79A76F6}" |
|
7 | 7 | ProjectSection(SolutionItems) = preProject |
|
8 | 8 | Implab.vsmdi = Implab.vsmdi |
|
9 | 9 | Local.testsettings = Local.testsettings |
|
10 | 10 | TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings |
|
11 | 11 | EndProjectSection |
|
12 | 12 | EndProject |
|
13 | 13 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab.Fx", "Implab.Fx\Implab.Fx.csproj", "{06E706F8-6881-43EB-927E-FFC503AF6ABC}" |
|
14 | 14 | EndProject |
|
15 | 15 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{BCA337C3-BFDC-4825-BBDB-E6D467E4E452}" |
|
16 | 16 | EndProject |
|
17 | 17 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab.Test.mono", "Implab.Test\Implab.Test.mono.csproj", "{2BD05F84-E067-4B87-9477-FDC2676A21C6}" |
|
18 | 18 | EndProject |
|
19 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoPlay", "MonoPlay\MonoPlay.csproj", "{15DD7123-D504-4627-8B4F-D00C7F04D033}" | |
|
20 | EndProject | |
|
19 | 21 | Global |
|
20 | 22 | GlobalSection(SolutionConfigurationPlatforms) = preSolution |
|
21 | 23 | Debug|Any CPU = Debug|Any CPU |
|
22 | 24 | Release|Any CPU = Release|Any CPU |
|
23 | 25 | Debug 4.5|Any CPU = Debug 4.5|Any CPU |
|
24 | 26 | Release 4.5|Any CPU = Release 4.5|Any CPU |
|
25 | 27 | EndGlobalSection |
|
26 | 28 | GlobalSection(ProjectConfigurationPlatforms) = postSolution |
|
27 | 29 | {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU |
|
28 | 30 | {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU |
|
29 | 31 | {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
|
30 | 32 | {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Debug|Any CPU.Build.0 = Debug|Any CPU |
|
31 | 33 | {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU |
|
32 | 34 | {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU |
|
33 | 35 | {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU |
|
34 | 36 | {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release|Any CPU.Build.0 = Release|Any CPU |
|
35 |
{ |
|
|
37 | {15DD7123-D504-4627-8B4F-D00C7F04D033}.Debug 4.5|Any CPU.ActiveCfg = Debug|Any CPU | |
|
38 | {15DD7123-D504-4627-8B4F-D00C7F04D033}.Debug 4.5|Any CPU.Build.0 = Debug|Any CPU | |
|
39 | {15DD7123-D504-4627-8B4F-D00C7F04D033}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |
|
40 | {15DD7123-D504-4627-8B4F-D00C7F04D033}.Debug|Any CPU.Build.0 = Debug|Any CPU | |
|
41 | {15DD7123-D504-4627-8B4F-D00C7F04D033}.Release 4.5|Any CPU.ActiveCfg = Release|Any CPU | |
|
42 | {15DD7123-D504-4627-8B4F-D00C7F04D033}.Release 4.5|Any CPU.Build.0 = Release|Any CPU | |
|
43 | {15DD7123-D504-4627-8B4F-D00C7F04D033}.Release|Any CPU.ActiveCfg = Release|Any CPU | |
|
44 | {15DD7123-D504-4627-8B4F-D00C7F04D033}.Release|Any CPU.Build.0 = Release|Any CPU | |
|
36 | 45 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU |
|
37 | 46 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU |
|
38 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU | |
|
39 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |
|
40 | 47 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
|
41 | 48 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug|Any CPU.Build.0 = Debug|Any CPU |
|
42 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug|Any CPU.Build.0 = Debug|Any CPU | |
|
43 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU | |
|
44 | 49 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU |
|
45 | 50 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU |
|
46 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU | |
|
47 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release|Any CPU.ActiveCfg = Release|Any CPU | |
|
48 | 51 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release|Any CPU.ActiveCfg = Release|Any CPU |
|
49 | 52 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release|Any CPU.Build.0 = Release|Any CPU |
|
50 | {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release|Any CPU.Build.0 = Release|Any CPU | |
|
53 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU | |
|
54 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU | |
|
55 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |
|
56 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.Build.0 = Debug|Any CPU | |
|
57 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU | |
|
58 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU | |
|
59 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.ActiveCfg = Release|Any CPU | |
|
60 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.Build.0 = Release|Any CPU | |
|
51 | 61 | {2F31E405-E267-4195-A05D-574093C21209}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU |
|
52 | 62 | {2F31E405-E267-4195-A05D-574093C21209}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU |
|
53 | 63 | {2F31E405-E267-4195-A05D-574093C21209}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
|
54 | 64 | {2F31E405-E267-4195-A05D-574093C21209}.Debug|Any CPU.Build.0 = Debug|Any CPU |
|
55 | 65 | {2F31E405-E267-4195-A05D-574093C21209}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU |
|
56 | 66 | {2F31E405-E267-4195-A05D-574093C21209}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU |
|
57 | 67 | {2F31E405-E267-4195-A05D-574093C21209}.Release|Any CPU.ActiveCfg = Release|Any CPU |
|
58 | 68 | {2F31E405-E267-4195-A05D-574093C21209}.Release|Any CPU.Build.0 = Release|Any CPU |
|
59 | 69 | {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU |
|
60 | 70 | {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU |
|
61 | 71 | {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
|
62 | 72 | {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Debug|Any CPU.Build.0 = Debug|Any CPU |
|
63 | 73 | {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU |
|
64 | 74 | {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU |
|
65 | 75 | {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release|Any CPU.ActiveCfg = Release|Any CPU |
|
66 | 76 | {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release|Any CPU.Build.0 = Release|Any CPU |
|
67 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU | |
|
68 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU | |
|
69 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |
|
70 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.Build.0 = Debug|Any CPU | |
|
71 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU | |
|
72 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU | |
|
73 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.ActiveCfg = Release|Any CPU | |
|
74 | {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.Build.0 = Release|Any CPU | |
|
75 | 77 | EndGlobalSection |
|
76 | 78 | GlobalSection(NestedProjects) = preSolution |
|
77 | 79 | {2BD05F84-E067-4B87-9477-FDC2676A21C6} = {BCA337C3-BFDC-4825-BBDB-E6D467E4E452} |
|
78 | 80 | EndGlobalSection |
|
79 | 81 | GlobalSection(MonoDevelopProperties) = preSolution |
|
80 |
StartupItem = |
|
|
82 | StartupItem = MonoPlay\MonoPlay.csproj | |
|
81 | 83 | Policies = $0 |
|
82 | 84 | $0.CSharpFormattingPolicy = $1 |
|
83 | 85 | $1.IndentSwitchBody = True |
|
84 | 86 | $1.NamespaceBraceStyle = EndOfLine |
|
85 | 87 | $1.ClassBraceStyle = EndOfLine |
|
86 | 88 | $1.InterfaceBraceStyle = EndOfLine |
|
87 | 89 | $1.StructBraceStyle = EndOfLine |
|
88 | 90 | $1.EnumBraceStyle = EndOfLine |
|
89 | 91 | $1.MethodBraceStyle = EndOfLine |
|
90 | 92 | $1.ConstructorBraceStyle = EndOfLine |
|
91 | 93 | $1.DestructorBraceStyle = EndOfLine |
|
92 | 94 | $1.BeforeMethodDeclarationParentheses = False |
|
93 | 95 | $1.BeforeMethodCallParentheses = False |
|
94 | 96 | $1.BeforeConstructorDeclarationParentheses = False |
|
95 | 97 | $1.NewLineBeforeConstructorInitializerColon = NewLine |
|
96 | 98 | $1.NewLineAfterConstructorInitializerColon = SameLine |
|
97 | 99 | $1.BeforeIndexerDeclarationBracket = False |
|
98 | 100 | $1.BeforeDelegateDeclarationParentheses = False |
|
99 | 101 | $1.NewParentheses = False |
|
100 | 102 | $1.SpacesBeforeBrackets = False |
|
101 | 103 | $1.inheritsSet = Mono |
|
102 | 104 | $1.inheritsScope = text/x-csharp |
|
103 | 105 | $1.scope = text/x-csharp |
|
104 | 106 | $0.TextStylePolicy = $2 |
|
105 | 107 | $2.FileWidth = 120 |
|
106 | 108 | $2.EolMarker = Unix |
|
107 | 109 | $2.inheritsSet = VisualStudio |
|
108 | 110 | $2.inheritsScope = text/plain |
|
109 | 111 | $2.scope = text/x-csharp |
|
110 | 112 | $0.DotNetNamingPolicy = $3 |
|
111 | 113 | $3.DirectoryNamespaceAssociation = PrefixedHierarchical |
|
112 | 114 | $3.ResourceNamePolicy = MSBuild |
|
113 | 115 | $0.TextStylePolicy = $4 |
|
114 | 116 | $4.FileWidth = 120 |
|
115 | 117 | $4.TabsToSpaces = False |
|
116 | 118 | $4.inheritsSet = VisualStudio |
|
117 | 119 | $4.inheritsScope = text/plain |
|
118 | 120 | $4.scope = application/xml |
|
119 | 121 | $0.XmlFormattingPolicy = $5 |
|
120 | 122 | $5.inheritsSet = Mono |
|
121 | 123 | $5.inheritsScope = application/xml |
|
122 | 124 | $5.scope = application/xml |
|
123 | 125 | $0.TextStylePolicy = $6 |
|
124 | 126 | $6.FileWidth = 120 |
|
125 | 127 | $6.TabsToSpaces = False |
|
126 | 128 | $6.inheritsSet = VisualStudio |
|
127 | 129 | $6.inheritsScope = text/plain |
|
128 | 130 | $6.scope = text/plain |
|
129 | 131 | $0.NameConventionPolicy = $7 |
|
130 | 132 | $7.Rules = $8 |
|
131 | 133 | $8.NamingRule = $9 |
|
132 | 134 | $9.Name = Namespaces |
|
133 | 135 | $9.AffectedEntity = Namespace |
|
134 | 136 | $9.VisibilityMask = VisibilityMask |
|
135 | 137 | $9.NamingStyle = PascalCase |
|
136 | 138 | $9.IncludeInstanceMembers = True |
|
137 | 139 | $9.IncludeStaticEntities = True |
|
138 | 140 | $8.NamingRule = $10 |
|
139 | 141 | $10.Name = Types |
|
140 | 142 | $10.AffectedEntity = Class, Struct, Enum, Delegate |
|
141 | 143 | $10.VisibilityMask = VisibilityMask |
|
142 | 144 | $10.NamingStyle = PascalCase |
|
143 | 145 | $10.IncludeInstanceMembers = True |
|
144 | 146 | $10.IncludeStaticEntities = True |
|
145 | 147 | $8.NamingRule = $11 |
|
146 | 148 | $11.Name = Interfaces |
|
147 | 149 | $11.RequiredPrefixes = $12 |
|
148 | 150 | $12.String = I |
|
149 | 151 | $11.AffectedEntity = Interface |
|
150 | 152 | $11.VisibilityMask = VisibilityMask |
|
151 | 153 | $11.NamingStyle = PascalCase |
|
152 | 154 | $11.IncludeInstanceMembers = True |
|
153 | 155 | $11.IncludeStaticEntities = True |
|
154 | 156 | $8.NamingRule = $13 |
|
155 | 157 | $13.Name = Attributes |
|
156 | 158 | $13.RequiredSuffixes = $14 |
|
157 | 159 | $14.String = Attribute |
|
158 | 160 | $13.AffectedEntity = CustomAttributes |
|
159 | 161 | $13.VisibilityMask = VisibilityMask |
|
160 | 162 | $13.NamingStyle = PascalCase |
|
161 | 163 | $13.IncludeInstanceMembers = True |
|
162 | 164 | $13.IncludeStaticEntities = True |
|
163 | 165 | $8.NamingRule = $15 |
|
164 | 166 | $15.Name = Event Arguments |
|
165 | 167 | $15.RequiredSuffixes = $16 |
|
166 | 168 | $16.String = EventArgs |
|
167 | 169 | $15.AffectedEntity = CustomEventArgs |
|
168 | 170 | $15.VisibilityMask = VisibilityMask |
|
169 | 171 | $15.NamingStyle = PascalCase |
|
170 | 172 | $15.IncludeInstanceMembers = True |
|
171 | 173 | $15.IncludeStaticEntities = True |
|
172 | 174 | $8.NamingRule = $17 |
|
173 | 175 | $17.Name = Exceptions |
|
174 | 176 | $17.RequiredSuffixes = $18 |
|
175 | 177 | $18.String = Exception |
|
176 | 178 | $17.AffectedEntity = CustomExceptions |
|
177 | 179 | $17.VisibilityMask = VisibilityMask |
|
178 | 180 | $17.NamingStyle = PascalCase |
|
179 | 181 | $17.IncludeInstanceMembers = True |
|
180 | 182 | $17.IncludeStaticEntities = True |
|
181 | 183 | $8.NamingRule = $19 |
|
182 | 184 | $19.Name = Methods |
|
183 | 185 | $19.AffectedEntity = Methods |
|
184 | 186 | $19.VisibilityMask = VisibilityMask |
|
185 | 187 | $19.NamingStyle = PascalCase |
|
186 | 188 | $19.IncludeInstanceMembers = True |
|
187 | 189 | $19.IncludeStaticEntities = True |
|
188 | 190 | $8.NamingRule = $20 |
|
189 | 191 | $20.Name = Static Readonly Fields |
|
190 | 192 | $20.AffectedEntity = ReadonlyField |
|
191 | 193 | $20.VisibilityMask = Internal, Protected, Public |
|
192 | 194 | $20.NamingStyle = PascalCase |
|
193 | 195 | $20.IncludeInstanceMembers = False |
|
194 | 196 | $20.IncludeStaticEntities = True |
|
195 | 197 | $8.NamingRule = $21 |
|
196 | 198 | $21.Name = Fields (Non Private) |
|
197 | 199 | $21.AffectedEntity = Field |
|
198 | 200 | $21.VisibilityMask = Internal, Public |
|
199 | 201 | $21.NamingStyle = CamelCase |
|
200 | 202 | $21.IncludeInstanceMembers = True |
|
201 | 203 | $21.IncludeStaticEntities = True |
|
202 | 204 | $8.NamingRule = $22 |
|
203 | 205 | $22.Name = ReadOnly Fields (Non Private) |
|
204 | 206 | $22.AffectedEntity = ReadonlyField |
|
205 | 207 | $22.VisibilityMask = Internal, Public |
|
206 | 208 | $22.NamingStyle = CamelCase |
|
207 | 209 | $22.IncludeInstanceMembers = True |
|
208 | 210 | $22.IncludeStaticEntities = False |
|
209 | 211 | $8.NamingRule = $23 |
|
210 | 212 | $23.Name = Fields (Private) |
|
211 | 213 | $23.RequiredPrefixes = $24 |
|
212 | 214 | $24.String = m_ |
|
213 | 215 | $23.AffectedEntity = Field, ReadonlyField |
|
214 | 216 | $23.VisibilityMask = Private, Protected |
|
215 | 217 | $23.NamingStyle = CamelCase |
|
216 | 218 | $23.IncludeInstanceMembers = True |
|
217 | 219 | $23.IncludeStaticEntities = False |
|
218 | 220 | $8.NamingRule = $25 |
|
219 | 221 | $25.Name = Static Fields (Private) |
|
220 | 222 | $25.RequiredPrefixes = $26 |
|
221 | 223 | $26.String = _ |
|
222 | 224 | $25.AffectedEntity = Field |
|
223 | 225 | $25.VisibilityMask = Private |
|
224 | 226 | $25.NamingStyle = CamelCase |
|
225 | 227 | $25.IncludeInstanceMembers = False |
|
226 | 228 | $25.IncludeStaticEntities = True |
|
227 | 229 | $8.NamingRule = $27 |
|
228 | 230 | $27.Name = ReadOnly Fields (Private) |
|
229 | 231 | $27.RequiredPrefixes = $28 |
|
230 | 232 | $28.String = m_ |
|
231 | 233 | $27.AffectedEntity = ReadonlyField |
|
232 | 234 | $27.VisibilityMask = Private, Protected |
|
233 | 235 | $27.NamingStyle = CamelCase |
|
234 | 236 | $27.IncludeInstanceMembers = True |
|
235 | 237 | $27.IncludeStaticEntities = False |
|
236 | 238 | $8.NamingRule = $29 |
|
237 | 239 | $29.Name = Constant Fields |
|
238 | 240 | $29.AffectedEntity = ConstantField |
|
239 | 241 | $29.VisibilityMask = VisibilityMask |
|
240 | 242 | $29.NamingStyle = AllUpper |
|
241 | 243 | $29.IncludeInstanceMembers = True |
|
242 | 244 | $29.IncludeStaticEntities = True |
|
243 | 245 | $8.NamingRule = $30 |
|
244 | 246 | $30.Name = Properties |
|
245 | 247 | $30.AffectedEntity = Property |
|
246 | 248 | $30.VisibilityMask = VisibilityMask |
|
247 | 249 | $30.NamingStyle = PascalCase |
|
248 | 250 | $30.IncludeInstanceMembers = True |
|
249 | 251 | $30.IncludeStaticEntities = True |
|
250 | 252 | $8.NamingRule = $31 |
|
251 | 253 | $31.Name = Events |
|
252 | 254 | $31.AffectedEntity = Event |
|
253 | 255 | $31.VisibilityMask = VisibilityMask |
|
254 | 256 | $31.NamingStyle = PascalCase |
|
255 | 257 | $31.IncludeInstanceMembers = True |
|
256 | 258 | $31.IncludeStaticEntities = True |
|
257 | 259 | $8.NamingRule = $32 |
|
258 | 260 | $32.Name = Enum Members |
|
259 | 261 | $32.AffectedEntity = EnumMember |
|
260 | 262 | $32.VisibilityMask = VisibilityMask |
|
261 | 263 | $32.NamingStyle = PascalCase |
|
262 | 264 | $32.IncludeInstanceMembers = True |
|
263 | 265 | $32.IncludeStaticEntities = True |
|
264 | 266 | $8.NamingRule = $33 |
|
265 | 267 | $33.Name = Parameters |
|
266 | 268 | $33.AffectedEntity = Parameter, LocalVariable |
|
267 | 269 | $33.VisibilityMask = VisibilityMask |
|
268 | 270 | $33.NamingStyle = CamelCase |
|
269 | 271 | $33.IncludeInstanceMembers = True |
|
270 | 272 | $33.IncludeStaticEntities = True |
|
271 | 273 | $8.NamingRule = $34 |
|
272 | 274 | $34.Name = Type Parameters |
|
273 | 275 | $34.RequiredPrefixes = $35 |
|
274 | 276 | $35.String = T |
|
275 | 277 | $34.AffectedEntity = TypeParameter |
|
276 | 278 | $34.VisibilityMask = VisibilityMask |
|
277 | 279 | $34.NamingStyle = PascalCase |
|
278 | 280 | $34.IncludeInstanceMembers = True |
|
279 | 281 | $34.IncludeStaticEntities = True |
|
280 | 282 | EndGlobalSection |
|
281 | 283 | GlobalSection(TestCaseManagementSettings) = postSolution |
|
282 | 284 | CategoryFile = Implab.vsmdi |
|
283 | 285 | EndGlobalSection |
|
284 | 286 | GlobalSection(SolutionProperties) = preSolution |
|
285 | 287 | HideSolutionNode = FALSE |
|
286 | 288 | EndGlobalSection |
|
287 | 289 | EndGlobal |
@@ -1,49 +1,59 | |||
|
1 | 1 | namespace Implab.Diagnostics { |
|
2 | 2 | struct OperationContext { |
|
3 | 3 | readonly LogicalOperation m_initial; |
|
4 | 4 | public readonly static OperationContext EMPTY = new OperationContext(LogicalOperation.EMPTY, false); |
|
5 | 5 | LogicalOperation m_current; |
|
6 | 6 | readonly bool m_ownership; |
|
7 | 7 | |
|
8 | 8 | public OperationContext(LogicalOperation operation, bool ownership) { |
|
9 | 9 | Safe.ArgumentNotNull(operation, "operation"); |
|
10 | 10 | |
|
11 | 11 | m_initial = operation; |
|
12 | 12 | m_current = operation; |
|
13 | 13 | m_ownership = ownership; |
|
14 | 14 | } |
|
15 | 15 | |
|
16 | 16 | public LogicalOperation CurrentOperation { |
|
17 | 17 | get { return m_current; } |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | public void BeginLogicalOperation(string name) { |
|
21 | 21 | m_current = new LogicalOperation(name, m_current); |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | public LogicalOperation DetachLogicalOperation() { |
|
25 | 25 | var detached = m_current; |
|
26 | 26 | if (m_current != LogicalOperation.EMPTY) { |
|
27 | 27 | if (m_current != m_initial) |
|
28 | 28 | m_current = m_current.Parent; |
|
29 | 29 | else if (m_ownership) |
|
30 | 30 | m_current = LogicalOperation.EMPTY; |
|
31 | else | |
|
31 | else { | |
|
32 | TraceLog.TraceWarning("DetachLogicalOperation can't be applied in the current context"); | |
|
32 | 33 | detached = LogicalOperation.EMPTY; |
|
34 | } | |
|
35 | } else { | |
|
36 | TraceLog.TraceWarning("DetachLogicalOperation can't be applied in the current context"); | |
|
33 | 37 | } |
|
34 | TraceLog.TraceWarning("EndLogicalOperation can't be applied in the current context"); | |
|
38 | ||
|
35 | 39 | return detached; |
|
36 | 40 | } |
|
37 | 41 | |
|
38 | 42 | public void EndLogicalOperation() { |
|
39 | 43 | if (m_current != m_initial) { |
|
40 | 44 | m_current = m_current.Parent; |
|
41 |
} else if (m_current != |
|
|
42 |
m_current = |
|
|
45 | } else if (m_current != LogicalOperation.EMPTY && m_ownership) { | |
|
46 | m_current = LogicalOperation.EMPTY; | |
|
43 | 47 | } else { |
|
44 | 48 | TraceLog.TraceWarning("EndLogicalOperation can't be applied in the current context"); |
|
45 | 49 | } |
|
46 | 50 | } |
|
51 | ||
|
52 | public void Leave() { | |
|
53 | ||
|
54 | if ((m_ownership && m_current != LogicalOperation.EMPTY) || (!m_ownership && m_current != m_initial) ) | |
|
55 | TraceLog.TraceWarning("Trying to leave unfinished logical operation {0}", m_current.Name); | |
|
56 | } | |
|
47 | 57 | } |
|
48 | 58 | } |
|
49 | 59 |
@@ -1,79 +1,80 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Threading; |
|
4 | 4 | |
|
5 | 5 | namespace Implab.Diagnostics { |
|
6 | 6 | /// <summary> |
|
7 | 7 | /// Trace context is bound to the specific thread, each thread has it's own ThreadContext. |
|
8 | 8 | /// </summary> |
|
9 | 9 | /// <remarks> |
|
10 | 10 | /// ThreadContext manages relations between logical operations and threads. |
|
11 | 11 | /// </remarks> |
|
12 | 12 | public class TraceContext { |
|
13 | 13 | |
|
14 | 14 | [ThreadStatic] |
|
15 | 15 | static TraceContext _instance; |
|
16 | 16 | |
|
17 | 17 | OperationContext m_current = OperationContext.EMPTY; |
|
18 | 18 | readonly Stack<OperationContext> m_stack = new Stack<OperationContext>(); |
|
19 | 19 | readonly int m_threadId; |
|
20 | 20 | |
|
21 | 21 | public static TraceContext Instance { |
|
22 | 22 | get { |
|
23 | 23 | if (_instance == null) |
|
24 | 24 | _instance = new TraceContext(); |
|
25 | 25 | return _instance; |
|
26 | 26 | } |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | public TraceContext() { |
|
30 | 30 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | public int ThreadId { |
|
34 | 34 | get { return m_threadId; } |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | public LogicalOperation CurrentOperation { |
|
38 | 38 | get { |
|
39 | 39 | return m_current.CurrentOperation; |
|
40 | 40 | } |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) { |
|
44 | // TODO Emit event | |
|
44 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Attach, String.Format("{0} -> [{1}]", operation.Name, m_threadId))); | |
|
45 | 45 | m_stack.Push(m_current); |
|
46 | 46 | m_current = new OperationContext(operation, takeOwnership); |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | public void StartLogicalOperation(string name) { |
|
50 | 50 | m_current.BeginLogicalOperation(name); |
|
51 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationStarted, String.Format("+{0}",CurrentOperation.Name))); | |
|
51 | 52 | } |
|
52 | 53 | |
|
53 | 54 | public void StartLogicalOperation() { |
|
54 | // TODO Emit Event | |
|
55 | m_current.BeginLogicalOperation(String.Empty); | |
|
55 | StartLogicalOperation(String.Empty); | |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | public void EndLogicalOperation() { |
|
59 | // TODO Emit event | |
|
59 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",CurrentOperation.Name, CurrentOperation.Duration))); | |
|
60 | 60 | m_current.EndLogicalOperation(); |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | public LogicalOperation DetachLogicalOperation() { |
|
64 | // TODO Emit event | |
|
65 | return m_current.DetachLogicalOperation(); | |
|
64 | var op = m_current.DetachLogicalOperation(); | |
|
65 | LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("[{0}] -> {1}", m_threadId, op.Name))); | |
|
66 | return op; | |
|
66 | 67 | } |
|
67 | 68 | |
|
68 | 69 | public void Leave() { |
|
69 | // TODO Emit event | |
|
70 | if (m_stack.Count > 0) | |
|
70 | if (m_stack.Count > 0) { | |
|
71 | m_current.Leave(); | |
|
71 | 72 | m_current = m_stack.Pop(); |
|
72 | else { | |
|
73 | } else { | |
|
73 | 74 | TraceLog.TraceWarning("Attemtp to leave the last operation context"); |
|
74 | 75 | m_current = OperationContext.EMPTY; |
|
75 | 76 | } |
|
76 | 77 | } |
|
77 | 78 | } |
|
78 | 79 | } |
|
79 | 80 |
@@ -1,34 +1,28 | |||
|
1 | 1 | using System; |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Linq; | |
|
4 | using System.Text; | |
|
5 | 2 | |
|
6 | 3 | namespace Implab.Diagnostics { |
|
7 | 4 | public class TraceEvent { |
|
8 | 5 | public string Message { |
|
9 | 6 | get; |
|
10 | 7 | private set; |
|
11 | 8 | } |
|
12 | 9 | |
|
13 | 10 | public TraceEventType EventType { |
|
14 | 11 | get; |
|
15 | 12 | private set; |
|
16 | 13 | } |
|
17 | 14 | |
|
18 | 15 | public TraceEvent(TraceEventType type, string message) { |
|
19 | 16 | EventType = type; |
|
20 | 17 | Message = message; |
|
21 | 18 | } |
|
22 | 19 | |
|
23 | 20 | public override string ToString() { |
|
24 |
|
|
|
25 | return Message; | |
|
26 | else | |
|
27 | return String.Format("{0}: {1}", EventType, Message); | |
|
21 | return EventType == TraceEventType.Information ? Message : String.Format("{0}: {1}", EventType, Message); | |
|
28 | 22 | } |
|
29 | 23 | |
|
30 | 24 | public static TraceEvent Create(TraceEventType type, string format, params object[] args) { |
|
31 | 25 | return new TraceEvent(type, format == null ? String.Empty : String.Format(format, args)); |
|
32 | 26 | } |
|
33 | 27 | } |
|
34 | 28 | } |
@@ -1,19 +1,17 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Threading.Tasks; |
|
6 | 6 | |
|
7 | 7 | namespace Implab.Diagnostics { |
|
8 | 8 | public enum TraceEventType { |
|
9 | 9 | Information = 1, |
|
10 | 10 | Warning, |
|
11 | 11 | Error, |
|
12 | 12 | OperationStarted, |
|
13 | 13 | OperationCompleted, |
|
14 | Fork, | |
|
15 | 14 | Attach, |
|
16 | 15 | Detach, |
|
17 | Created | |
|
18 | 16 | } |
|
19 | 17 | } |
@@ -1,220 +1,224 | |||
|
1 | 1 | ο»Ώ<?xml version="1.0" encoding="utf-8"?> |
|
2 | 2 | <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|
3 | 3 | <PropertyGroup> |
|
4 | 4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
|
5 | 5 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
|
6 | 6 | <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid> |
|
7 | 7 | <OutputType>Library</OutputType> |
|
8 | 8 | <RootNamespace>Implab</RootNamespace> |
|
9 | 9 | <AssemblyName>Implab</AssemblyName> |
|
10 | <ProductVersion>8.0.30703</ProductVersion> | |
|
11 | <SchemaVersion>2.0</SchemaVersion> | |
|
10 | 12 | </PropertyGroup> |
|
11 | 13 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
|
12 | 14 | <DebugSymbols>true</DebugSymbols> |
|
13 | 15 | <DebugType>full</DebugType> |
|
14 | 16 | <Optimize>false</Optimize> |
|
15 | 17 | <OutputPath>bin\Debug</OutputPath> |
|
16 | 18 | <DefineConstants>TRACE;DEBUG;</DefineConstants> |
|
17 | 19 | <ErrorReport>prompt</ErrorReport> |
|
18 | 20 | <WarningLevel>4</WarningLevel> |
|
19 | 21 | <ConsolePause>false</ConsolePause> |
|
20 | 22 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
21 | 23 | </PropertyGroup> |
|
22 | 24 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
|
23 | 25 | <DebugType>full</DebugType> |
|
24 | 26 | <Optimize>true</Optimize> |
|
25 | 27 | <OutputPath>bin\Release</OutputPath> |
|
26 | 28 | <ErrorReport>prompt</ErrorReport> |
|
27 | 29 | <WarningLevel>4</WarningLevel> |
|
28 | 30 | <ConsolePause>false</ConsolePause> |
|
29 | 31 | </PropertyGroup> |
|
30 | 32 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' "> |
|
31 | 33 | <DebugSymbols>true</DebugSymbols> |
|
32 | 34 | <DebugType>full</DebugType> |
|
33 | 35 | <Optimize>false</Optimize> |
|
34 | 36 | <OutputPath>bin\Debug</OutputPath> |
|
35 | 37 | <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants> |
|
36 | 38 | <ErrorReport>prompt</ErrorReport> |
|
37 | 39 | <WarningLevel>4</WarningLevel> |
|
38 | 40 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
39 | 41 | <ConsolePause>false</ConsolePause> |
|
40 | 42 | </PropertyGroup> |
|
41 | 43 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' "> |
|
42 | 44 | <Optimize>true</Optimize> |
|
43 | 45 | <OutputPath>bin\Release</OutputPath> |
|
44 | 46 | <ErrorReport>prompt</ErrorReport> |
|
45 | 47 | <WarningLevel>4</WarningLevel> |
|
46 | 48 | <ConsolePause>false</ConsolePause> |
|
47 | 49 | <DefineConstants>NET_4_5</DefineConstants> |
|
48 | 50 | </PropertyGroup> |
|
49 | 51 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|AnyCPU' "> |
|
50 | 52 | <DebugSymbols>true</DebugSymbols> |
|
51 | 53 | <DebugType>full</DebugType> |
|
52 | 54 | <Optimize>false</Optimize> |
|
53 | 55 | <OutputPath>bin\Debug</OutputPath> |
|
54 | 56 | <DefineConstants>TRACE;DEBUG;NET_4_5;MONO</DefineConstants> |
|
55 | 57 | <ErrorReport>prompt</ErrorReport> |
|
56 | 58 | <WarningLevel>4</WarningLevel> |
|
57 | 59 | <RunCodeAnalysis>true</RunCodeAnalysis> |
|
58 | 60 | <ConsolePause>false</ConsolePause> |
|
59 | 61 | </PropertyGroup> |
|
60 | 62 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|AnyCPU' "> |
|
61 | 63 | <Optimize>true</Optimize> |
|
62 | 64 | <OutputPath>bin\Release</OutputPath> |
|
63 | 65 | <DefineConstants>NET_4_5;MONO;</DefineConstants> |
|
64 | 66 | <ErrorReport>prompt</ErrorReport> |
|
65 | 67 | <WarningLevel>4</WarningLevel> |
|
66 | 68 | <ConsolePause>false</ConsolePause> |
|
67 | 69 | </PropertyGroup> |
|
68 | 70 | <ItemGroup> |
|
69 | 71 | <Reference Include="System" /> |
|
70 | 72 | <Reference Include="System.Xml" /> |
|
71 | 73 | </ItemGroup> |
|
72 | 74 | <ItemGroup> |
|
73 | 75 | <Compile Include="Component.cs" /> |
|
74 | 76 | <Compile Include="CustomEqualityComparer.cs" /> |
|
75 | 77 | <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> |
|
76 | 78 | <Compile Include="Diagnostics\EventText.cs" /> |
|
77 | 79 | <Compile Include="Diagnostics\IEventTextFormatter.cs" /> |
|
78 | 80 | <Compile Include="Diagnostics\LogChannel.cs" /> |
|
79 | 81 | <Compile Include="Diagnostics\LogicalOperation.cs" /> |
|
80 | 82 | <Compile Include="Diagnostics\TextFileListener.cs" /> |
|
81 | 83 | <Compile Include="Diagnostics\TextListenerBase.cs" /> |
|
82 | 84 | <Compile Include="Diagnostics\TraceLog.cs" /> |
|
83 | 85 | <Compile Include="Diagnostics\TraceEvent.cs" /> |
|
84 | 86 | <Compile Include="Diagnostics\TraceEventType.cs" /> |
|
85 | 87 | <Compile Include="Disposable.cs" /> |
|
86 | 88 | <Compile Include="ICancellable.cs" /> |
|
87 | 89 | <Compile Include="IProgressHandler.cs" /> |
|
88 | 90 | <Compile Include="IProgressNotifier.cs" /> |
|
89 | 91 | <Compile Include="IPromiseT.cs" /> |
|
90 | 92 | <Compile Include="IPromise.cs" /> |
|
91 | 93 | <Compile Include="IServiceLocator.cs" /> |
|
92 | 94 | <Compile Include="ITaskController.cs" /> |
|
93 | 95 | <Compile Include="JSON\JSONElementContext.cs" /> |
|
94 | 96 | <Compile Include="JSON\JSONElementType.cs" /> |
|
95 | 97 | <Compile Include="JSON\JSONGrammar.cs" /> |
|
96 | 98 | <Compile Include="JSON\JSONParser.cs" /> |
|
97 | 99 | <Compile Include="JSON\JSONScanner.cs" /> |
|
98 | 100 | <Compile Include="JSON\JsonTokenType.cs" /> |
|
99 | 101 | <Compile Include="JSON\JSONWriter.cs" /> |
|
100 | 102 | <Compile Include="JSON\JSONXmlReader.cs" /> |
|
101 | 103 | <Compile Include="JSON\JSONXmlReaderOptions.cs" /> |
|
102 | 104 | <Compile Include="JSON\StringTranslator.cs" /> |
|
103 | 105 | <Compile Include="Parallels\DispatchPool.cs" /> |
|
104 | 106 | <Compile Include="Parallels\ArrayTraits.cs" /> |
|
105 | 107 | <Compile Include="Parallels\MTQueue.cs" /> |
|
106 | 108 | <Compile Include="Parallels\WorkerPool.cs" /> |
|
107 | 109 | <Compile Include="Parsing\Alphabet.cs" /> |
|
108 | 110 | <Compile Include="Parsing\AlphabetBase.cs" /> |
|
109 | 111 | <Compile Include="Parsing\AltToken.cs" /> |
|
110 | 112 | <Compile Include="Parsing\BinaryToken.cs" /> |
|
111 | 113 | <Compile Include="Parsing\CatToken.cs" /> |
|
112 | 114 | <Compile Include="Parsing\CDFADefinition.cs" /> |
|
113 | 115 | <Compile Include="Parsing\DFABuilder.cs" /> |
|
114 | 116 | <Compile Include="Parsing\DFADefinitionBase.cs" /> |
|
115 | 117 | <Compile Include="Parsing\DFAStateDescriptor.cs" /> |
|
116 | 118 | <Compile Include="Parsing\DFAutomaton.cs" /> |
|
117 | 119 | <Compile Include="Parsing\EDFADefinition.cs" /> |
|
118 | 120 | <Compile Include="Parsing\EmptyToken.cs" /> |
|
119 | 121 | <Compile Include="Parsing\EndToken.cs" /> |
|
120 | 122 | <Compile Include="Parsing\EnumAlphabet.cs" /> |
|
121 | 123 | <Compile Include="Parsing\Grammar.cs" /> |
|
122 | 124 | <Compile Include="Parsing\IAlphabet.cs" /> |
|
123 | 125 | <Compile Include="Parsing\IDFADefinition.cs" /> |
|
124 | 126 | <Compile Include="Parsing\IVisitor.cs" /> |
|
125 | 127 | <Compile Include="Parsing\ParserException.cs" /> |
|
126 | 128 | <Compile Include="Parsing\Scanner.cs" /> |
|
127 | 129 | <Compile Include="Parsing\StarToken.cs" /> |
|
128 | 130 | <Compile Include="Parsing\SymbolToken.cs" /> |
|
129 | 131 | <Compile Include="Parsing\Token.cs" /> |
|
130 | 132 | <Compile Include="SafePool.cs" /> |
|
131 | 133 | <Compile Include="ServiceLocator.cs" /> |
|
132 | 134 | <Compile Include="TaskController.cs" /> |
|
133 | 135 | <Compile Include="ProgressInitEventArgs.cs" /> |
|
134 | 136 | <Compile Include="Properties\AssemblyInfo.cs" /> |
|
135 | 137 | <Compile Include="Promise.cs" /> |
|
136 | 138 | <Compile Include="Parallels\AsyncPool.cs" /> |
|
137 | 139 | <Compile Include="Safe.cs" /> |
|
138 | 140 | <Compile Include="ValueEventArgs.cs" /> |
|
139 | 141 | <Compile Include="PromiseExtensions.cs" /> |
|
140 | 142 | <Compile Include="TransientPromiseException.cs" /> |
|
141 | 143 | <Compile Include="SyncContextPromise.cs" /> |
|
142 | 144 | <Compile Include="ObjectPool.cs" /> |
|
143 | 145 | <Compile Include="Diagnostics\OperationContext.cs" /> |
|
144 | 146 | <Compile Include="Diagnostics\TraceContext.cs" /> |
|
145 | 147 | <Compile Include="Diagnostics\LogEventArgs.cs" /> |
|
146 | 148 | <Compile Include="Diagnostics\LogEventArgsT.cs" /> |
|
147 | 149 | <Compile Include="Diagnostics\Extensions.cs" /> |
|
150 | <Compile Include="IComponentContainer.cs" /> | |
|
151 | <Compile Include="MTComponentContainer.cs" /> | |
|
148 | 152 | </ItemGroup> |
|
149 | 153 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|
150 | 154 | <ItemGroup /> |
|
151 | 155 | <ProjectExtensions> |
|
152 | 156 | <MonoDevelop> |
|
153 | 157 | <Properties> |
|
154 | 158 | <Policies> |
|
155 | 159 | <CSharpFormattingPolicy IndentSwitchBody="True" NamespaceBraceStyle="EndOfLine" ClassBraceStyle="EndOfLine" InterfaceBraceStyle="EndOfLine" StructBraceStyle="EndOfLine" EnumBraceStyle="EndOfLine" MethodBraceStyle="EndOfLine" ConstructorBraceStyle="EndOfLine" DestructorBraceStyle="EndOfLine" BeforeMethodDeclarationParentheses="False" BeforeMethodCallParentheses="False" BeforeConstructorDeclarationParentheses="False" NewLineBeforeConstructorInitializerColon="NewLine" NewLineAfterConstructorInitializerColon="SameLine" BeforeIndexerDeclarationBracket="False" BeforeDelegateDeclarationParentheses="False" NewParentheses="False" SpacesBeforeBrackets="False" inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" /> |
|
156 | 160 | <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" /> |
|
157 | 161 | <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" /> |
|
158 | 162 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" /> |
|
159 | 163 | <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" /> |
|
160 | 164 | <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" /> |
|
161 | 165 | <NameConventionPolicy> |
|
162 | 166 | <Rules> |
|
163 | 167 | <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
164 | 168 | <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
165 | 169 | <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
166 | 170 | <RequiredPrefixes> |
|
167 | 171 | <String>I</String> |
|
168 | 172 | </RequiredPrefixes> |
|
169 | 173 | </NamingRule> |
|
170 | 174 | <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
171 | 175 | <RequiredSuffixes> |
|
172 | 176 | <String>Attribute</String> |
|
173 | 177 | </RequiredSuffixes> |
|
174 | 178 | </NamingRule> |
|
175 | 179 | <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
176 | 180 | <RequiredSuffixes> |
|
177 | 181 | <String>EventArgs</String> |
|
178 | 182 | </RequiredSuffixes> |
|
179 | 183 | </NamingRule> |
|
180 | 184 | <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
181 | 185 | <RequiredSuffixes> |
|
182 | 186 | <String>Exception</String> |
|
183 | 187 | </RequiredSuffixes> |
|
184 | 188 | </NamingRule> |
|
185 | 189 | <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
186 | 190 | <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" /> |
|
187 | 191 | <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
188 | 192 | <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" /> |
|
189 | 193 | <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> |
|
190 | 194 | <RequiredPrefixes> |
|
191 | 195 | <String>m_</String> |
|
192 | 196 | </RequiredPrefixes> |
|
193 | 197 | </NamingRule> |
|
194 | 198 | <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True"> |
|
195 | 199 | <RequiredPrefixes> |
|
196 | 200 | <String>_</String> |
|
197 | 201 | </RequiredPrefixes> |
|
198 | 202 | </NamingRule> |
|
199 | 203 | <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False"> |
|
200 | 204 | <RequiredPrefixes> |
|
201 | 205 | <String>m_</String> |
|
202 | 206 | </RequiredPrefixes> |
|
203 | 207 | </NamingRule> |
|
204 | 208 | <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
205 | 209 | <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
206 | 210 | <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
207 | 211 | <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
208 | 212 | <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" /> |
|
209 | 213 | <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True"> |
|
210 | 214 | <RequiredPrefixes> |
|
211 | 215 | <String>T</String> |
|
212 | 216 | </RequiredPrefixes> |
|
213 | 217 | </NamingRule> |
|
214 | 218 | </Rules> |
|
215 | 219 | </NameConventionPolicy> |
|
216 | 220 | </Policies> |
|
217 | 221 | </Properties> |
|
218 | 222 | </MonoDevelop> |
|
219 | 223 | </ProjectExtensions> |
|
220 | 224 | </Project> No newline at end of file |
@@ -1,199 +1,199 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Threading; |
|
3 | 3 | |
|
4 | 4 | namespace Implab.Parallels { |
|
5 | 5 | public abstract class DispatchPool<TUnit> : IDisposable { |
|
6 | 6 | readonly int m_minThreadsLimit; |
|
7 | 7 | readonly int m_maxThreadsLimit; |
|
8 | 8 | readonly int m_releaseTimeout = 1000; // the timeout while the working thread will wait for the new tasks before exit |
|
9 | 9 | |
|
10 |
int m_threads |
|
|
11 |
int m_maxRunningThreads |
|
|
12 |
int m_exit |
|
|
10 | int m_threads; // the current size of the pool | |
|
11 | int m_maxRunningThreads; // the meximum reached size of the pool | |
|
12 | int m_exit; // the pool is going to shutdown, all unused workers are released | |
|
13 | 13 | |
|
14 | 14 | readonly object m_signal = new object(); // used to pulse waiting threads |
|
15 | 15 | |
|
16 | 16 | protected DispatchPool(int min, int max) { |
|
17 | 17 | if (min < 0) |
|
18 | 18 | throw new ArgumentOutOfRangeException("min"); |
|
19 | 19 | if (max <= 0) |
|
20 | 20 | throw new ArgumentOutOfRangeException("max"); |
|
21 | 21 | |
|
22 | 22 | if (min > max) |
|
23 | 23 | min = max; |
|
24 | 24 | m_minThreadsLimit = min; |
|
25 | 25 | m_maxThreadsLimit = max; |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | 28 | protected DispatchPool(int threads) |
|
29 | 29 | : this(threads, threads) { |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | protected DispatchPool() { |
|
33 | 33 | int maxThreads, maxCP; |
|
34 | 34 | ThreadPool.GetMaxThreads(out maxThreads, out maxCP); |
|
35 | 35 | |
|
36 | 36 | m_minThreadsLimit = 0; |
|
37 | 37 | m_maxThreadsLimit = maxThreads; |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | protected void InitPool() { |
|
41 | 41 | for (int i = 0; i < m_minThreadsLimit; i++) |
|
42 | 42 | StartWorker(); |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | public int PoolSize { |
|
46 | 46 | get { |
|
47 | 47 | Thread.MemoryBarrier(); |
|
48 | 48 | return m_threads; |
|
49 | 49 | } |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | public int MaxRunningThreads { |
|
53 | 53 | get { |
|
54 | 54 | Thread.MemoryBarrier(); |
|
55 | 55 | return m_maxRunningThreads; |
|
56 | 56 | } |
|
57 | 57 | } |
|
58 | 58 | |
|
59 | 59 | protected bool IsDisposed { |
|
60 | 60 | get { |
|
61 | 61 | Thread.MemoryBarrier(); |
|
62 | 62 | return m_exit == 1; |
|
63 | 63 | } |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | protected abstract bool TryDequeue(out TUnit unit); |
|
67 | 67 | |
|
68 | 68 | bool Dequeue(out TUnit unit, int timeout) { |
|
69 | 69 | int ts = Environment.TickCount; |
|
70 | 70 | if (TryDequeue(out unit)) |
|
71 | 71 | return true; |
|
72 | 72 | lock (m_signal) { |
|
73 | 73 | while (!TryDequeue(out unit) && m_exit == 0) |
|
74 | 74 | if(!Monitor.Wait(m_signal, Math.Max(0, ts + timeout - Environment.TickCount))) { |
|
75 | 75 | // timeout |
|
76 | 76 | return false; |
|
77 | 77 | } |
|
78 | 78 | // queue item or terminate |
|
79 | 79 | Monitor.Pulse(m_signal); |
|
80 | 80 | if (m_exit == 1) |
|
81 | 81 | return false; |
|
82 | 82 | } |
|
83 | 83 | return true; |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | 86 | protected void SignalThread() { |
|
87 | 87 | lock (m_signal) { |
|
88 | 88 | Monitor.Pulse(m_signal); |
|
89 | 89 | } |
|
90 | 90 | } |
|
91 | 91 | |
|
92 | 92 | #region thread slots traits |
|
93 | 93 | |
|
94 | 94 | bool AllocateThreadSlot() { |
|
95 | 95 | int current; |
|
96 | 96 | // use spins to allocate slot for the new thread |
|
97 | 97 | do { |
|
98 | 98 | current = m_threads; |
|
99 | 99 | if (current >= m_maxThreadsLimit || m_exit == 1) |
|
100 | 100 | // no more slots left or the pool has been disposed |
|
101 | 101 | return false; |
|
102 | 102 | } while (current != Interlocked.CompareExchange(ref m_threads, current + 1, current)); |
|
103 | 103 | |
|
104 | 104 | UpdateMaxThreads(current + 1); |
|
105 | 105 | |
|
106 | 106 | return true; |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | bool AllocateThreadSlot(int desired) { |
|
110 | 110 | if (desired - 1 != Interlocked.CompareExchange(ref m_threads, desired, desired - 1)) |
|
111 | 111 | return false; |
|
112 | 112 | |
|
113 | 113 | UpdateMaxThreads(desired); |
|
114 | 114 | |
|
115 | 115 | return true; |
|
116 | 116 | } |
|
117 | 117 | |
|
118 | 118 | bool ReleaseThreadSlot(out bool last) { |
|
119 | 119 | last = false; |
|
120 | 120 | int current; |
|
121 | 121 | // use spins to release slot for the new thread |
|
122 | 122 | Thread.MemoryBarrier(); |
|
123 | 123 | do { |
|
124 | 124 | current = m_threads; |
|
125 | 125 | if (current <= m_minThreadsLimit && m_exit == 0) |
|
126 | 126 | // the thread is reserved |
|
127 | 127 | return false; |
|
128 | 128 | } while (current != Interlocked.CompareExchange(ref m_threads, current - 1, current)); |
|
129 | 129 | |
|
130 | 130 | last = (current == 1); |
|
131 | 131 | |
|
132 | 132 | return true; |
|
133 | 133 | } |
|
134 | 134 | |
|
135 | 135 | void UpdateMaxThreads(int count) { |
|
136 | 136 | int max; |
|
137 | 137 | do { |
|
138 | 138 | max = m_maxRunningThreads; |
|
139 | 139 | if (max >= count) |
|
140 | 140 | break; |
|
141 | 141 | } while(max != Interlocked.CompareExchange(ref m_maxRunningThreads, count, max)); |
|
142 | 142 | } |
|
143 | 143 | |
|
144 | 144 | #endregion |
|
145 | 145 | |
|
146 | 146 | protected bool StartWorker() { |
|
147 | 147 | if (AllocateThreadSlot()) { |
|
148 | 148 | // slot successfully allocated |
|
149 | 149 | var worker = new Thread(Worker); |
|
150 | 150 | worker.IsBackground = true; |
|
151 | 151 | worker.Start(); |
|
152 | 152 | |
|
153 | 153 | return true; |
|
154 | 154 | } |
|
155 | 155 | return false; |
|
156 | 156 | } |
|
157 | 157 | |
|
158 | 158 | protected abstract void InvokeUnit(TUnit unit); |
|
159 | 159 | |
|
160 | 160 | protected virtual void Worker() { |
|
161 | 161 | TUnit unit; |
|
162 | 162 | bool last; |
|
163 | 163 | do { |
|
164 | 164 | while (Dequeue(out unit, m_releaseTimeout)) { |
|
165 | 165 | InvokeUnit(unit); |
|
166 | 166 | } |
|
167 | 167 | if(!ReleaseThreadSlot(out last)) |
|
168 | 168 | continue; |
|
169 | 169 | // queue may be not empty |
|
170 | 170 | if (last && TryDequeue(out unit)) { |
|
171 | 171 | InvokeUnit(unit); |
|
172 | 172 | if (AllocateThreadSlot(1)) |
|
173 | 173 | continue; |
|
174 | 174 | // we can safely exit since pool is alive |
|
175 | 175 | } |
|
176 | 176 | break; |
|
177 | 177 | } while(true); |
|
178 | 178 | } |
|
179 | 179 | |
|
180 | 180 | |
|
181 | 181 | protected virtual void Dispose(bool disposing) { |
|
182 | 182 | if (disposing) { |
|
183 | 183 | if (0 == Interlocked.CompareExchange(ref m_exit, 1, 0)) { // implies memory barrier |
|
184 | 184 | // wake sleeping threads |
|
185 | 185 | SignalThread(); |
|
186 | 186 | GC.SuppressFinalize(this); |
|
187 | 187 | } |
|
188 | 188 | } |
|
189 | 189 | } |
|
190 | 190 | |
|
191 | 191 | public void Dispose() { |
|
192 | 192 | Dispose(true); |
|
193 | 193 | } |
|
194 | 194 | |
|
195 | 195 | ~DispatchPool() { |
|
196 | 196 | Dispose(false); |
|
197 | 197 | } |
|
198 | 198 | } |
|
199 | 199 | } |
@@ -1,78 +1,73 | |||
|
1 | using System; | |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Linq; | |
|
4 | using System.Text; | |
|
5 | using System.Threading; | |
|
1 | using System.Threading; | |
|
6 | 2 | |
|
7 | 3 | namespace Implab.Parallels { |
|
8 | 4 | public class MTQueue<T> { |
|
9 | 5 | class Node { |
|
10 | 6 | public Node(T value) { |
|
11 | 7 | this.value = value; |
|
12 | 8 | } |
|
13 | 9 | public readonly T value; |
|
14 | 10 | public Node next; |
|
15 | 11 | } |
|
16 | 12 | |
|
17 | 13 | Node m_first; |
|
18 | 14 | Node m_last; |
|
19 | 15 | |
|
20 | 16 | public void Enqueue(T value) { |
|
21 | 17 | Thread.MemoryBarrier(); |
|
22 | 18 | |
|
23 | 19 | var last = m_last; |
|
24 | 20 | var next = new Node(value); |
|
25 | 21 | |
|
26 | 22 | while (last != Interlocked.CompareExchange(ref m_last, next, last)) |
|
27 | 23 | last = m_last; |
|
28 | 24 | |
|
29 | 25 | if (last != null) |
|
30 | 26 | last.next = next; |
|
31 | 27 | else |
|
32 | 28 | m_first = next; |
|
33 | 29 | } |
|
34 | 30 | |
|
35 | 31 | public bool TryDequeue(out T value) { |
|
36 | 32 | Node first; |
|
37 |
Node next |
|
|
33 | Node next; | |
|
38 | 34 | value = default(T); |
|
39 | 35 | |
|
40 | 36 | Thread.MemoryBarrier(); |
|
41 | 37 | do { |
|
42 | 38 | first = m_first; |
|
43 | 39 | if (first == null) |
|
44 | 40 | return false; |
|
45 | 41 | next = first.next; |
|
46 | 42 | if (next == null) { |
|
47 | 43 | // this is the last element, |
|
48 | 44 | // then try to update the tail |
|
49 | 45 | if (first != Interlocked.CompareExchange(ref m_last, null, first)) { |
|
50 | 46 | // this is the race condition |
|
51 | 47 | if (m_last == null) |
|
52 | 48 | // the queue is empty |
|
53 | 49 | return false; |
|
54 | 50 | // tail has been changed, we need to restart |
|
55 | 51 | continue; |
|
56 | 52 | } |
|
57 | 53 | |
|
58 | 54 | // tail succesfully updated and first.next will never be changed |
|
59 | 55 | // other readers will fail due to inconsistency m_last != m_fist && m_first.next == null |
|
60 | 56 | // however the parallel writer may update the m_first since the m_last is null |
|
61 | 57 | |
|
62 | 58 | // so we need to fix inconsistency by setting m_first to null or if it has been |
|
63 | 59 | // updated by the writer already then we should just to give up |
|
64 | 60 | Interlocked.CompareExchange(ref m_first, null, first); |
|
65 | 61 | break; |
|
66 | 62 | |
|
67 | } else { | |
|
68 | if (first == Interlocked.CompareExchange(ref m_first, next, first)) | |
|
69 | // head succesfully updated | |
|
70 | break; | |
|
71 | 63 | } |
|
64 | if (first == Interlocked.CompareExchange(ref m_first, next, first)) | |
|
65 | // head succesfully updated | |
|
66 | break; | |
|
72 | 67 | } while (true); |
|
73 | 68 | |
|
74 | 69 | value = first.value; |
|
75 | 70 | return true; |
|
76 | 71 | } |
|
77 | 72 | } |
|
78 | 73 | } |
General Comments 0
You need to be logged in to leave comments.
Login now