##// END OF EJS Templates
Added SharedLock to synchronization routines
cin -
r129:471f596b2603 v2
parent child
Show More
@@ -0,0 +1,75
1 using System;
2 using System.Threading;
3 using System.Diagnostics;
4
5 namespace Implab.Parallels {
6 /// <summary>
7 /// Implements a lightweight mechanism to aquire a shared or an exclusive lock.
8 /// </summary>
9 public class SharedLock {
10 readonly object m_lock = new object();
11 int m_locks;
12 bool m_exclusive;
13
14 public bool LockExclusive(int timeout) {
15 lock (m_lock) {
16 if (m_locks > 0 && !Monitor.Wait(m_lock, timeout))
17 return false;
18 m_exclusive = true;
19 m_locks = 1;
20 }
21 }
22
23 public void LockExclusive() {
24 LockExclusive(-1);
25 }
26
27 public bool LockShared(int timeout) {
28 lock (m_lock) {
29 if (!m_exclusive) {
30 m_locks++;
31 return true;
32 }
33
34 if (m_lock == 0) {
35 m_exclusive = false;
36 m_locks = 1;
37 return true;
38 }
39
40 if (Monitor.Wait(m_lock, timeout)) {
41 Debug.Assert(m_locks == 0);
42 m_locks = 1;
43 m_exclusive = false;
44 return true;
45 }
46 return false;
47 }
48 }
49
50 public void LockShared() {
51 LockShared(-1);
52 }
53
54 public void ReleaseShared() {
55 lock (m_lock) {
56 if (m_exclusive || m_locks <= 0)
57 throw new InvalidOperationException();
58 m_locks--;
59 if (m_locks == 0)
60 Monitor.PulseAll(m_lock);
61 }
62 }
63
64 public void ReleaseExclusive() {
65 lock (m_lock) {
66 if (!m_exclusive && m_locks != 1)
67 throw new InvalidOperationException();
68 m_locks = 0;
69 Monitor.PulseAll(m_lock);
70 }
71 }
72
73 }
74 }
75
@@ -1,231 +1,230
1 <?xml version="1.0" encoding="utf-8"?>
1 <?xml version="1.0" encoding="utf-8"?>
2 <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2 <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3 <PropertyGroup>
3 <PropertyGroup>
4 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
4 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
5 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6 <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid>
6 <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid>
7 <OutputType>Library</OutputType>
7 <OutputType>Library</OutputType>
8 <RootNamespace>Implab</RootNamespace>
8 <RootNamespace>Implab</RootNamespace>
9 <AssemblyName>Implab</AssemblyName>
9 <AssemblyName>Implab</AssemblyName>
10 </PropertyGroup>
10 </PropertyGroup>
11 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
11 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
12 <DebugSymbols>true</DebugSymbols>
12 <DebugSymbols>true</DebugSymbols>
13 <DebugType>full</DebugType>
13 <DebugType>full</DebugType>
14 <Optimize>false</Optimize>
14 <Optimize>false</Optimize>
15 <OutputPath>bin\Debug</OutputPath>
15 <OutputPath>bin\Debug</OutputPath>
16 <DefineConstants>TRACE;DEBUG;</DefineConstants>
16 <DefineConstants>TRACE;DEBUG;</DefineConstants>
17 <ErrorReport>prompt</ErrorReport>
17 <ErrorReport>prompt</ErrorReport>
18 <WarningLevel>4</WarningLevel>
18 <WarningLevel>4</WarningLevel>
19 <ConsolePause>false</ConsolePause>
19 <ConsolePause>false</ConsolePause>
20 <RunCodeAnalysis>true</RunCodeAnalysis>
20 <RunCodeAnalysis>true</RunCodeAnalysis>
21 </PropertyGroup>
21 </PropertyGroup>
22 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
22 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
23 <DebugType>full</DebugType>
23 <DebugType>full</DebugType>
24 <Optimize>true</Optimize>
24 <Optimize>true</Optimize>
25 <OutputPath>bin\Release</OutputPath>
25 <OutputPath>bin\Release</OutputPath>
26 <ErrorReport>prompt</ErrorReport>
26 <ErrorReport>prompt</ErrorReport>
27 <WarningLevel>4</WarningLevel>
27 <WarningLevel>4</WarningLevel>
28 <ConsolePause>false</ConsolePause>
28 <ConsolePause>false</ConsolePause>
29 </PropertyGroup>
29 </PropertyGroup>
30 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' ">
30 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' ">
31 <DebugSymbols>true</DebugSymbols>
31 <DebugSymbols>true</DebugSymbols>
32 <DebugType>full</DebugType>
32 <DebugType>full</DebugType>
33 <Optimize>false</Optimize>
33 <Optimize>false</Optimize>
34 <OutputPath>bin\Debug</OutputPath>
34 <OutputPath>bin\Debug</OutputPath>
35 <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants>
35 <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants>
36 <ErrorReport>prompt</ErrorReport>
36 <ErrorReport>prompt</ErrorReport>
37 <WarningLevel>4</WarningLevel>
37 <WarningLevel>4</WarningLevel>
38 <RunCodeAnalysis>true</RunCodeAnalysis>
38 <RunCodeAnalysis>true</RunCodeAnalysis>
39 <ConsolePause>false</ConsolePause>
39 <ConsolePause>false</ConsolePause>
40 </PropertyGroup>
40 </PropertyGroup>
41 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' ">
41 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' ">
42 <Optimize>true</Optimize>
42 <Optimize>true</Optimize>
43 <OutputPath>bin\Release</OutputPath>
43 <OutputPath>bin\Release</OutputPath>
44 <ErrorReport>prompt</ErrorReport>
44 <ErrorReport>prompt</ErrorReport>
45 <WarningLevel>4</WarningLevel>
45 <WarningLevel>4</WarningLevel>
46 <ConsolePause>false</ConsolePause>
46 <ConsolePause>false</ConsolePause>
47 <DefineConstants>NET_4_5</DefineConstants>
47 <DefineConstants>NET_4_5</DefineConstants>
48 </PropertyGroup>
48 </PropertyGroup>
49 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|AnyCPU' ">
49 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|AnyCPU' ">
50 <DebugSymbols>true</DebugSymbols>
50 <DebugSymbols>true</DebugSymbols>
51 <DebugType>full</DebugType>
51 <DebugType>full</DebugType>
52 <Optimize>false</Optimize>
52 <Optimize>false</Optimize>
53 <OutputPath>bin\Debug</OutputPath>
53 <OutputPath>bin\Debug</OutputPath>
54 <DefineConstants>TRACE;DEBUG;NET_4_5;MONO</DefineConstants>
54 <DefineConstants>TRACE;DEBUG;NET_4_5;MONO</DefineConstants>
55 <ErrorReport>prompt</ErrorReport>
55 <ErrorReport>prompt</ErrorReport>
56 <WarningLevel>4</WarningLevel>
56 <WarningLevel>4</WarningLevel>
57 <RunCodeAnalysis>true</RunCodeAnalysis>
57 <RunCodeAnalysis>true</RunCodeAnalysis>
58 <ConsolePause>false</ConsolePause>
58 <ConsolePause>false</ConsolePause>
59 </PropertyGroup>
59 </PropertyGroup>
60 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|AnyCPU' ">
60 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|AnyCPU' ">
61 <Optimize>true</Optimize>
61 <Optimize>true</Optimize>
62 <OutputPath>bin\Release</OutputPath>
62 <OutputPath>bin\Release</OutputPath>
63 <DefineConstants>NET_4_5;MONO;</DefineConstants>
63 <DefineConstants>NET_4_5;MONO;</DefineConstants>
64 <ErrorReport>prompt</ErrorReport>
64 <ErrorReport>prompt</ErrorReport>
65 <WarningLevel>4</WarningLevel>
65 <WarningLevel>4</WarningLevel>
66 <ConsolePause>false</ConsolePause>
66 <ConsolePause>false</ConsolePause>
67 </PropertyGroup>
67 </PropertyGroup>
68 <ItemGroup>
68 <ItemGroup>
69 <Reference Include="System" />
69 <Reference Include="System" />
70 <Reference Include="System.Xml" />
70 <Reference Include="System.Xml" />
71 </ItemGroup>
71 </ItemGroup>
72 <ItemGroup>
72 <ItemGroup>
73 <Compile Include="Component.cs" />
73 <Compile Include="Component.cs" />
74 <Compile Include="CustomEqualityComparer.cs" />
74 <Compile Include="CustomEqualityComparer.cs" />
75 <Compile Include="Diagnostics\ConsoleTraceListener.cs" />
75 <Compile Include="Diagnostics\ConsoleTraceListener.cs" />
76 <Compile Include="Diagnostics\EventText.cs" />
76 <Compile Include="Diagnostics\EventText.cs" />
77 <Compile Include="Diagnostics\IEventTextFormatter.cs" />
77 <Compile Include="Diagnostics\IEventTextFormatter.cs" />
78 <Compile Include="Diagnostics\LogChannel.cs" />
78 <Compile Include="Diagnostics\LogChannel.cs" />
79 <Compile Include="Diagnostics\LogicalOperation.cs" />
79 <Compile Include="Diagnostics\LogicalOperation.cs" />
80 <Compile Include="Diagnostics\TextFileListener.cs" />
80 <Compile Include="Diagnostics\TextFileListener.cs" />
81 <Compile Include="Diagnostics\TextListenerBase.cs" />
81 <Compile Include="Diagnostics\TextListenerBase.cs" />
82 <Compile Include="Diagnostics\TraceLog.cs" />
82 <Compile Include="Diagnostics\TraceLog.cs" />
83 <Compile Include="Diagnostics\TraceEvent.cs" />
83 <Compile Include="Diagnostics\TraceEvent.cs" />
84 <Compile Include="Diagnostics\TraceEventType.cs" />
84 <Compile Include="Diagnostics\TraceEventType.cs" />
85 <Compile Include="Disposable.cs" />
85 <Compile Include="Disposable.cs" />
86 <Compile Include="ICancellable.cs" />
86 <Compile Include="ICancellable.cs" />
87 <Compile Include="IProgressHandler.cs" />
87 <Compile Include="IProgressHandler.cs" />
88 <Compile Include="IProgressNotifier.cs" />
88 <Compile Include="IProgressNotifier.cs" />
89 <Compile Include="IPromiseT.cs" />
89 <Compile Include="IPromiseT.cs" />
90 <Compile Include="IPromise.cs" />
90 <Compile Include="IPromise.cs" />
91 <Compile Include="IServiceLocator.cs" />
91 <Compile Include="IServiceLocator.cs" />
92 <Compile Include="ITaskController.cs" />
92 <Compile Include="ITaskController.cs" />
93 <Compile Include="JSON\JSONElementContext.cs" />
93 <Compile Include="JSON\JSONElementContext.cs" />
94 <Compile Include="JSON\JSONElementType.cs" />
94 <Compile Include="JSON\JSONElementType.cs" />
95 <Compile Include="JSON\JSONGrammar.cs" />
95 <Compile Include="JSON\JSONGrammar.cs" />
96 <Compile Include="JSON\JSONParser.cs" />
96 <Compile Include="JSON\JSONParser.cs" />
97 <Compile Include="JSON\JSONScanner.cs" />
97 <Compile Include="JSON\JSONScanner.cs" />
98 <Compile Include="JSON\JsonTokenType.cs" />
98 <Compile Include="JSON\JsonTokenType.cs" />
99 <Compile Include="JSON\JSONWriter.cs" />
99 <Compile Include="JSON\JSONWriter.cs" />
100 <Compile Include="JSON\JSONXmlReader.cs" />
100 <Compile Include="JSON\JSONXmlReader.cs" />
101 <Compile Include="JSON\JSONXmlReaderOptions.cs" />
101 <Compile Include="JSON\JSONXmlReaderOptions.cs" />
102 <Compile Include="JSON\StringTranslator.cs" />
102 <Compile Include="JSON\StringTranslator.cs" />
103 <Compile Include="Parallels\DispatchPool.cs" />
103 <Compile Include="Parallels\DispatchPool.cs" />
104 <Compile Include="Parallels\ArrayTraits.cs" />
104 <Compile Include="Parallels\ArrayTraits.cs" />
105 <Compile Include="Parallels\MTQueue.cs" />
105 <Compile Include="Parallels\MTQueue.cs" />
106 <Compile Include="Parallels\WorkerPool.cs" />
106 <Compile Include="Parallels\WorkerPool.cs" />
107 <Compile Include="Parsing\Alphabet.cs" />
107 <Compile Include="Parsing\Alphabet.cs" />
108 <Compile Include="Parsing\AlphabetBase.cs" />
108 <Compile Include="Parsing\AlphabetBase.cs" />
109 <Compile Include="Parsing\AltToken.cs" />
109 <Compile Include="Parsing\AltToken.cs" />
110 <Compile Include="Parsing\BinaryToken.cs" />
110 <Compile Include="Parsing\BinaryToken.cs" />
111 <Compile Include="Parsing\CatToken.cs" />
111 <Compile Include="Parsing\CatToken.cs" />
112 <Compile Include="Parsing\CDFADefinition.cs" />
112 <Compile Include="Parsing\CDFADefinition.cs" />
113 <Compile Include="Parsing\DFABuilder.cs" />
113 <Compile Include="Parsing\DFABuilder.cs" />
114 <Compile Include="Parsing\DFADefinitionBase.cs" />
114 <Compile Include="Parsing\DFADefinitionBase.cs" />
115 <Compile Include="Parsing\DFAStateDescriptor.cs" />
115 <Compile Include="Parsing\DFAStateDescriptor.cs" />
116 <Compile Include="Parsing\DFAutomaton.cs" />
116 <Compile Include="Parsing\DFAutomaton.cs" />
117 <Compile Include="Parsing\EDFADefinition.cs" />
117 <Compile Include="Parsing\EDFADefinition.cs" />
118 <Compile Include="Parsing\EmptyToken.cs" />
118 <Compile Include="Parsing\EmptyToken.cs" />
119 <Compile Include="Parsing\EndToken.cs" />
119 <Compile Include="Parsing\EndToken.cs" />
120 <Compile Include="Parsing\EnumAlphabet.cs" />
120 <Compile Include="Parsing\EnumAlphabet.cs" />
121 <Compile Include="Parsing\Grammar.cs" />
121 <Compile Include="Parsing\Grammar.cs" />
122 <Compile Include="Parsing\IAlphabet.cs" />
122 <Compile Include="Parsing\IAlphabet.cs" />
123 <Compile Include="Parsing\IDFADefinition.cs" />
123 <Compile Include="Parsing\IDFADefinition.cs" />
124 <Compile Include="Parsing\IVisitor.cs" />
124 <Compile Include="Parsing\IVisitor.cs" />
125 <Compile Include="Parsing\ParserException.cs" />
125 <Compile Include="Parsing\ParserException.cs" />
126 <Compile Include="Parsing\Scanner.cs" />
126 <Compile Include="Parsing\Scanner.cs" />
127 <Compile Include="Parsing\StarToken.cs" />
127 <Compile Include="Parsing\StarToken.cs" />
128 <Compile Include="Parsing\SymbolToken.cs" />
128 <Compile Include="Parsing\SymbolToken.cs" />
129 <Compile Include="Parsing\Token.cs" />
129 <Compile Include="Parsing\Token.cs" />
130 <Compile Include="ServiceLocator.cs" />
130 <Compile Include="ServiceLocator.cs" />
131 <Compile Include="TaskController.cs" />
131 <Compile Include="TaskController.cs" />
132 <Compile Include="ProgressInitEventArgs.cs" />
132 <Compile Include="ProgressInitEventArgs.cs" />
133 <Compile Include="Properties\AssemblyInfo.cs" />
133 <Compile Include="Properties\AssemblyInfo.cs" />
134 <Compile Include="Parallels\AsyncPool.cs" />
134 <Compile Include="Parallels\AsyncPool.cs" />
135 <Compile Include="Safe.cs" />
135 <Compile Include="Safe.cs" />
136 <Compile Include="ValueEventArgs.cs" />
136 <Compile Include="ValueEventArgs.cs" />
137 <Compile Include="PromiseExtensions.cs" />
137 <Compile Include="PromiseExtensions.cs" />
138 <Compile Include="SyncContextPromise.cs" />
138 <Compile Include="SyncContextPromise.cs" />
139 <Compile Include="Diagnostics\OperationContext.cs" />
139 <Compile Include="Diagnostics\OperationContext.cs" />
140 <Compile Include="Diagnostics\TraceContext.cs" />
140 <Compile Include="Diagnostics\TraceContext.cs" />
141 <Compile Include="Diagnostics\LogEventArgs.cs" />
141 <Compile Include="Diagnostics\LogEventArgs.cs" />
142 <Compile Include="Diagnostics\LogEventArgsT.cs" />
142 <Compile Include="Diagnostics\LogEventArgsT.cs" />
143 <Compile Include="Diagnostics\Extensions.cs" />
143 <Compile Include="Diagnostics\Extensions.cs" />
144 <Compile Include="IComponentContainer.cs" />
144 <Compile Include="IComponentContainer.cs" />
145 <Compile Include="PromiseEventType.cs" />
145 <Compile Include="PromiseEventType.cs" />
146 <Compile Include="Parallels\MTCustomQueue.cs" />
147 <Compile Include="Parallels\MTCustomQueueNode.cs" />
148 <Compile Include="ComponentContainer.cs" />
146 <Compile Include="ComponentContainer.cs" />
149 <Compile Include="DisposablePool.cs" />
147 <Compile Include="DisposablePool.cs" />
150 <Compile Include="ObjectPool.cs" />
148 <Compile Include="ObjectPool.cs" />
151 <Compile Include="Parallels\AsyncQueue.cs" />
149 <Compile Include="Parallels\AsyncQueue.cs" />
152 <Compile Include="PromiseT.cs" />
150 <Compile Include="PromiseT.cs" />
153 <Compile Include="IDeferred.cs" />
151 <Compile Include="IDeferred.cs" />
154 <Compile Include="IDeferredT.cs" />
152 <Compile Include="IDeferredT.cs" />
155 <Compile Include="AbstractPromise.cs" />
153 <Compile Include="AbstractPromise.cs" />
156 <Compile Include="Promise.cs" />
154 <Compile Include="Promise.cs" />
157 <Compile Include="PromiseTransientException.cs" />
155 <Compile Include="PromiseTransientException.cs" />
158 <Compile Include="Parallels\Signal.cs" />
156 <Compile Include="Parallels\Signal.cs" />
157 <Compile Include="Parallels\SharedLock.cs" />
159 </ItemGroup>
158 </ItemGroup>
160 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
159 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
161 <ItemGroup />
160 <ItemGroup />
162 <ProjectExtensions>
161 <ProjectExtensions>
163 <MonoDevelop>
162 <MonoDevelop>
164 <Properties>
163 <Properties>
165 <Policies>
164 <Policies>
166 <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" />
165 <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" />
167 <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" />
166 <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" />
168 <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" />
167 <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" />
169 <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" />
168 <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" />
170 <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" />
169 <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" />
171 <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" />
170 <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" />
172 <NameConventionPolicy>
171 <NameConventionPolicy>
173 <Rules>
172 <Rules>
174 <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
173 <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
175 <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
174 <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
176 <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
175 <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
177 <RequiredPrefixes>
176 <RequiredPrefixes>
178 <String>I</String>
177 <String>I</String>
179 </RequiredPrefixes>
178 </RequiredPrefixes>
180 </NamingRule>
179 </NamingRule>
181 <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
180 <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
182 <RequiredSuffixes>
181 <RequiredSuffixes>
183 <String>Attribute</String>
182 <String>Attribute</String>
184 </RequiredSuffixes>
183 </RequiredSuffixes>
185 </NamingRule>
184 </NamingRule>
186 <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
185 <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
187 <RequiredSuffixes>
186 <RequiredSuffixes>
188 <String>EventArgs</String>
187 <String>EventArgs</String>
189 </RequiredSuffixes>
188 </RequiredSuffixes>
190 </NamingRule>
189 </NamingRule>
191 <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
190 <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
192 <RequiredSuffixes>
191 <RequiredSuffixes>
193 <String>Exception</String>
192 <String>Exception</String>
194 </RequiredSuffixes>
193 </RequiredSuffixes>
195 </NamingRule>
194 </NamingRule>
196 <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
195 <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
197 <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" />
196 <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" />
198 <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
197 <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
199 <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" />
198 <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" />
200 <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False">
199 <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False">
201 <RequiredPrefixes>
200 <RequiredPrefixes>
202 <String>m_</String>
201 <String>m_</String>
203 </RequiredPrefixes>
202 </RequiredPrefixes>
204 </NamingRule>
203 </NamingRule>
205 <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True">
204 <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True">
206 <RequiredPrefixes>
205 <RequiredPrefixes>
207 <String>_</String>
206 <String>_</String>
208 </RequiredPrefixes>
207 </RequiredPrefixes>
209 </NamingRule>
208 </NamingRule>
210 <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False">
209 <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False">
211 <RequiredPrefixes>
210 <RequiredPrefixes>
212 <String>m_</String>
211 <String>m_</String>
213 </RequiredPrefixes>
212 </RequiredPrefixes>
214 </NamingRule>
213 </NamingRule>
215 <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
214 <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
216 <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
215 <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
217 <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
216 <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
218 <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
217 <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
219 <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
218 <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
220 <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
219 <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
221 <RequiredPrefixes>
220 <RequiredPrefixes>
222 <String>T</String>
221 <String>T</String>
223 </RequiredPrefixes>
222 </RequiredPrefixes>
224 </NamingRule>
223 </NamingRule>
225 </Rules>
224 </Rules>
226 </NameConventionPolicy>
225 </NameConventionPolicy>
227 </Policies>
226 </Policies>
228 </Properties>
227 </Properties>
229 </MonoDevelop>
228 </MonoDevelop>
230 </ProjectExtensions>
229 </ProjectExtensions>
231 </Project> No newline at end of file
230 </Project>
@@ -1,629 +1,629
1 using System.Threading;
1 using System.Threading;
2 using System.Collections.Generic;
2 using System.Collections.Generic;
3 using System;
3 using System;
4 using System.Collections;
4 using System.Collections;
5 using System.Diagnostics;
5 using System.Diagnostics;
6
6
7 namespace Implab.Parallels {
7 namespace Implab.Parallels {
8 public class AsyncQueue<T> : IEnumerable<T> {
8 public class AsyncQueue<T> : IEnumerable<T> {
9 class Chunk {
9 class Chunk {
10 public Chunk next;
10 public Chunk next;
11
11
12 int m_low;
12 int m_low;
13 int m_hi;
13 int m_hi;
14 int m_alloc;
14 int m_alloc;
15 readonly int m_size;
15 readonly int m_size;
16 readonly T[] m_data;
16 readonly T[] m_data;
17
17
18 public Chunk(int size) {
18 public Chunk(int size) {
19 m_size = size;
19 m_size = size;
20 m_data = new T[size];
20 m_data = new T[size];
21 }
21 }
22
22
23 public Chunk(int size, T value) {
23 public Chunk(int size, T value) {
24 m_size = size;
24 m_size = size;
25 m_hi = 1;
25 m_hi = 1;
26 m_alloc = 1;
26 m_alloc = 1;
27 m_data = new T[size];
27 m_data = new T[size];
28 m_data[0] = value;
28 m_data[0] = value;
29 }
29 }
30
30
31 public Chunk(int size, T[] data, int offset, int length, int alloc) {
31 public Chunk(int size, T[] data, int offset, int length, int alloc) {
32 m_size = size;
32 m_size = size;
33 m_hi = length;
33 m_hi = length;
34 m_alloc = alloc;
34 m_alloc = alloc;
35 m_data = new T[size];
35 m_data = new T[size];
36 Array.Copy(data, offset, m_data, 0, length);
36 Array.Copy(data, offset, m_data, 0, length);
37 }
37 }
38
38
39 public int Low {
39 public int Low {
40 get { return m_low; }
40 get { return m_low; }
41 }
41 }
42
42
43 public int Hi {
43 public int Hi {
44 get { return m_hi; }
44 get { return m_hi; }
45 }
45 }
46
46
47 public int Size {
47 public int Size {
48 get { return m_size; }
48 get { return m_size; }
49 }
49 }
50
50
51 public bool TryEnqueue(T value, out bool extend) {
51 public bool TryEnqueue(T value, out bool extend) {
52 var alloc = Interlocked.Increment(ref m_alloc) - 1;
52 var alloc = Interlocked.Increment(ref m_alloc) - 1;
53
53
54 if (alloc >= m_size) {
54 if (alloc >= m_size) {
55 extend = alloc == m_size;
55 extend = alloc == m_size;
56 return false;
56 return false;
57 }
57 }
58
58
59 extend = false;
59 extend = false;
60 m_data[alloc] = value;
60 m_data[alloc] = value;
61
61
62 while (alloc != Interlocked.CompareExchange(ref m_hi, alloc + 1, alloc)) {
62 while (alloc != Interlocked.CompareExchange(ref m_hi, alloc + 1, alloc)) {
63 // spin wait for commit
63 // spin wait for commit
64 }
64 }
65 return true;
65 return true;
66 }
66 }
67
67
68 /// <summary>
68 /// <summary>
69 /// Prevents from allocating new space in the chunk and waits for all write operations to complete
69 /// Prevents from allocating new space in the chunk and waits for all write operations to complete
70 /// </summary>
70 /// </summary>
71 public void Commit() {
71 public void Commit() {
72 var actual = Math.Min(Interlocked.Exchange(ref m_alloc, m_size + 1), m_size);
72 var actual = Math.Min(Interlocked.Exchange(ref m_alloc, m_size + 1), m_size);
73
73
74 while (m_hi != actual)
74 while (m_hi != actual)
75 Thread.MemoryBarrier();
75 Thread.MemoryBarrier();
76 }
76 }
77
77
78 public bool TryDequeue(out T value, out bool recycle) {
78 public bool TryDequeue(out T value, out bool recycle) {
79 int low;
79 int low;
80 do {
80 do {
81 low = m_low;
81 low = m_low;
82 if (low >= m_hi) {
82 if (low >= m_hi) {
83 value = default(T);
83 value = default(T);
84 recycle = (low == m_size);
84 recycle = (low == m_size);
85 return false;
85 return false;
86 }
86 }
87 } while(low != Interlocked.CompareExchange(ref m_low, low + 1, low));
87 } while(low != Interlocked.CompareExchange(ref m_low, low + 1, low));
88
88
89 recycle = (low == m_size - 1);
89 recycle = (low == m_size - 1);
90 value = m_data[low];
90 value = m_data[low];
91
91
92 return true;
92 return true;
93 }
93 }
94
94
95 public bool TryEnqueueBatch(T[] batch, int offset, int length, out int enqueued, out bool extend) {
95 public bool TryEnqueueBatch(T[] batch, int offset, int length, out int enqueued, out bool extend) {
96 //int alloc;
96 //int alloc;
97 //int allocSize;
97 //int allocSize;
98
98
99 var alloc = Interlocked.Add(ref m_alloc, length) - length;
99 var alloc = Interlocked.Add(ref m_alloc, length) - length;
100 if (alloc > m_size) {
100 if (alloc > m_size) {
101 // the chunk is full and someone already
101 // the chunk is full and someone already
102 // creating the new one
102 // creating the new one
103 enqueued = 0; // nothing was added
103 enqueued = 0; // nothing was added
104 extend = false; // the caller shouldn't try to extend the queue
104 extend = false; // the caller shouldn't try to extend the queue
105 return false; // nothing was added
105 return false; // nothing was added
106 }
106 }
107
107
108 enqueued = Math.Min(m_size - alloc, length);
108 enqueued = Math.Min(m_size - alloc, length);
109 extend = length > enqueued;
109 extend = length > enqueued;
110
110
111 if (enqueued == 0)
111 if (enqueued == 0)
112 return false;
112 return false;
113
113
114
114
115 Array.Copy(batch, offset, m_data, alloc, enqueued);
115 Array.Copy(batch, offset, m_data, alloc, enqueued);
116
116
117 while (alloc != Interlocked.CompareExchange(ref m_hi, alloc + enqueued, alloc)) {
117 while (alloc != Interlocked.CompareExchange(ref m_hi, alloc + enqueued, alloc)) {
118 // spin wait for commit
118 // spin wait for commit
119 }
119 }
120
120
121 return true;
121 return true;
122 }
122 }
123
123
124 public bool TryDequeueBatch(T[] buffer, int offset, int length,out int dequeued, out bool recycle) {
124 public bool TryDequeueBatch(T[] buffer, int offset, int length,out int dequeued, out bool recycle) {
125 int low, hi, batchSize;
125 int low, hi, batchSize;
126
126
127 do {
127 do {
128 low = m_low;
128 low = m_low;
129 hi = m_hi;
129 hi = m_hi;
130 if (low >= hi) {
130 if (low >= hi) {
131 dequeued = 0;
131 dequeued = 0;
132 recycle = (low == m_size); // recycling could be restarted and we need to signal again
132 recycle = (low == m_size); // recycling could be restarted and we need to signal again
133 return false;
133 return false;
134 }
134 }
135 batchSize = Math.Min(hi - low, length);
135 batchSize = Math.Min(hi - low, length);
136 } while(low != Interlocked.CompareExchange(ref m_low, low + batchSize, low));
136 } while(low != Interlocked.CompareExchange(ref m_low, low + batchSize, low));
137
137
138 recycle = (low == m_size - batchSize);
138 recycle = (low == m_size - batchSize);
139 dequeued = batchSize;
139 dequeued = batchSize;
140
140
141 Array.Copy(m_data, low, buffer, offset, batchSize);
141 Array.Copy(m_data, low, buffer, offset, batchSize);
142
142
143 return true;
143 return true;
144 }
144 }
145
145
146 public T GetAt(int pos) {
146 public T GetAt(int pos) {
147 return m_data[pos];
147 return m_data[pos];
148 }
148 }
149 }
149 }
150
150
151 public const int DEFAULT_CHUNK_SIZE = 32;
151 public const int DEFAULT_CHUNK_SIZE = 32;
152 public const int MAX_CHUNK_SIZE = 262144;
152 public const int MAX_CHUNK_SIZE = 262144;
153
153
154 Chunk m_first;
154 Chunk m_first;
155 Chunk m_last;
155 Chunk m_last;
156
156
157 /// <summary>
157 /// <summary>
158 /// Adds the specified value to the queue.
158 /// Adds the specified value to the queue.
159 /// </summary>
159 /// </summary>
160 /// <param name="value">Tha value which will be added to the queue.</param>
160 /// <param name="value">Tha value which will be added to the queue.</param>
161 public void Enqueue(T value) {
161 public void Enqueue(T value) {
162 var last = m_last;
162 var last = m_last;
163 // spin wait to the new chunk
163 // spin wait to the new chunk
164 bool extend = true;
164 bool extend = true;
165 while (last == null || !last.TryEnqueue(value, out extend)) {
165 while (last == null || !last.TryEnqueue(value, out extend)) {
166 // try to extend queue
166 // try to extend queue
167 if (extend || last == null) {
167 if (extend || last == null) {
168 var chunk = new Chunk(DEFAULT_CHUNK_SIZE, value);
168 var chunk = new Chunk(DEFAULT_CHUNK_SIZE, value);
169 if (EnqueueChunk(last, chunk))
169 if (EnqueueChunk(last, chunk))
170 break; // success! exit!
170 break; // success! exit!
171 last = m_last;
171 last = m_last;
172 } else {
172 } else {
173 while (last == m_last) {
173 while (last == m_last) {
174 Thread.MemoryBarrier();
174 Thread.MemoryBarrier();
175 }
175 }
176 last = m_last;
176 last = m_last;
177 }
177 }
178 }
178 }
179 }
179 }
180
180
181 /// <summary>
181 /// <summary>
182 /// Adds the specified data to the queue.
182 /// Adds the specified data to the queue.
183 /// </summary>
183 /// </summary>
184 /// <param name="data">The buffer which contains the data to be enqueued.</param>
184 /// <param name="data">The buffer which contains the data to be enqueued.</param>
185 /// <param name="offset">The offset of the data in the buffer.</param>
185 /// <param name="offset">The offset of the data in the buffer.</param>
186 /// <param name="length">The size of the data to read from the buffer.</param>
186 /// <param name="length">The size of the data to read from the buffer.</param>
187 public void EnqueueRange(T[] data, int offset, int length) {
187 public void EnqueueRange(T[] data, int offset, int length) {
188 if (data == null)
188 if (data == null)
189 throw new ArgumentNullException("data");
189 throw new ArgumentNullException("data");
190 if (offset < 0)
190 if (offset < 0)
191 throw new ArgumentOutOfRangeException("offset");
191 throw new ArgumentOutOfRangeException("offset");
192 if (length < 1 || offset + length > data.Length)
192 if (length < 1 || offset + length > data.Length)
193 throw new ArgumentOutOfRangeException("length");
193 throw new ArgumentOutOfRangeException("length");
194
194
195 var last = m_last;
195 var last = m_last;
196
196
197 bool extend;
197 bool extend;
198 int enqueued;
198 int enqueued;
199
199
200 while (length > 0) {
200 while (length > 0) {
201 extend = true;
201 extend = true;
202 if (last != null && last.TryEnqueueBatch(data, offset, length, out enqueued, out extend)) {
202 if (last != null && last.TryEnqueueBatch(data, offset, length, out enqueued, out extend)) {
203 length -= enqueued;
203 length -= enqueued;
204 offset += enqueued;
204 offset += enqueued;
205 }
205 }
206
206
207 if (extend) {
207 if (extend) {
208 // there was no enough space in the chunk
208 // there was no enough space in the chunk
209 // or there was no chunks in the queue
209 // or there was no chunks in the queue
210
210
211 while (length > 0) {
211 while (length > 0) {
212
212
213 var size = Math.Min(length, MAX_CHUNK_SIZE);
213 var size = Math.Min(length, MAX_CHUNK_SIZE);
214
214
215 var chunk = new Chunk(
215 var chunk = new Chunk(
216 Math.Max(size, DEFAULT_CHUNK_SIZE),
216 Math.Max(size, DEFAULT_CHUNK_SIZE),
217 data,
217 data,
218 offset,
218 offset,
219 size,
219 size,
220 length // length >= size
220 length // length >= size
221 );
221 );
222
222
223 if (!EnqueueChunk(last, chunk)) {
223 if (!EnqueueChunk(last, chunk)) {
224 // looks like the queue has been updated then proceed from the beginning
224 // looks like the queue has been updated then proceed from the beginning
225 last = m_last;
225 last = m_last;
226 break;
226 break;
227 }
227 }
228
228
229 // we have successfully added the new chunk
229 // we have successfully added the new chunk
230 last = chunk;
230 last = chunk;
231 length -= size;
231 length -= size;
232 offset += size;
232 offset += size;
233 }
233 }
234 } else {
234 } else {
235 // we don't need to extend the queue, if we successfully enqueued data
235 // we don't need to extend the queue, if we successfully enqueued data
236 if (length == 0)
236 if (length == 0)
237 break;
237 break;
238
238
239 // if we need to wait while someone is extending the queue
239 // if we need to wait while someone is extending the queue
240 // spinwait
240 // spinwait
241 while (last == m_last) {
241 while (last == m_last) {
242 Thread.MemoryBarrier();
242 Thread.MemoryBarrier();
243 }
243 }
244
244
245 last = m_last;
245 last = m_last;
246 }
246 }
247 }
247 }
248 }
248 }
249
249
250 /// <summary>
250 /// <summary>
251 /// Tries to retrieve the first element from the queue.
251 /// Tries to retrieve the first element from the queue.
252 /// </summary>
252 /// </summary>
253 /// <returns><c>true</c>, if element is dequeued, <c>false</c> otherwise.</returns>
253 /// <returns><c>true</c>, if element is dequeued, <c>false</c> otherwise.</returns>
254 /// <param name="value">The value of the dequeued element.</param>
254 /// <param name="value">The value of the dequeued element.</param>
255 public bool TryDequeue(out T value) {
255 public bool TryDequeue(out T value) {
256 var chunk = m_first;
256 var chunk = m_first;
257 bool recycle;
257 bool recycle;
258 while (chunk != null) {
258 while (chunk != null) {
259
259
260 var result = chunk.TryDequeue(out value, out recycle);
260 var result = chunk.TryDequeue(out value, out recycle);
261
261
262 if (recycle) // this chunk is waste
262 if (recycle) // this chunk is waste
263 RecycleFirstChunk(chunk);
263 RecycleFirstChunk(chunk);
264 else
264 else
265 return result; // this chunk is usable and returned actual result
265 return result; // this chunk is usable and returned actual result
266
266
267 if (result) // this chunk is waste but the true result is always actual
267 if (result) // this chunk is waste but the true result is always actual
268 return true;
268 return true;
269
269
270 // try again
270 // try again
271 chunk = m_first;
271 chunk = m_first;
272 }
272 }
273
273
274 // the queue is empty
274 // the queue is empty
275 value = default(T);
275 value = default(T);
276 return false;
276 return false;
277 }
277 }
278
278
279 /// <summary>
279 /// <summary>
280 /// Tries to dequeue the specified amount of data from the queue.
280 /// Tries to dequeue the specified amount of data from the queue.
281 /// </summary>
281 /// </summary>
282 /// <returns><c>true</c>, if data was deuqueued, <c>false</c> otherwise.</returns>
282 /// <returns><c>true</c>, if data was deuqueued, <c>false</c> otherwise.</returns>
283 /// <param name="buffer">The buffer to which the data will be written.</param>
283 /// <param name="buffer">The buffer to which the data will be written.</param>
284 /// <param name="offset">The offset in the buffer at which the data will be written.</param>
284 /// <param name="offset">The offset in the buffer at which the data will be written.</param>
285 /// <param name="length">The maximum amount of data to be retrieved.</param>
285 /// <param name="length">The maximum amount of data to be retrieved.</param>
286 /// <param name="dequeued">The actual amout of the retrieved data.</param>
286 /// <param name="dequeued">The actual amout of the retrieved data.</param>
287 public bool TryDequeueRange(T[] buffer, int offset, int length, out int dequeued) {
287 public bool TryDequeueRange(T[] buffer, int offset, int length, out int dequeued) {
288 if (buffer == null)
288 if (buffer == null)
289 throw new ArgumentNullException("buffer");
289 throw new ArgumentNullException("buffer");
290 if (offset < 0)
290 if (offset < 0)
291 throw new ArgumentOutOfRangeException("offset");
291 throw new ArgumentOutOfRangeException("offset");
292 if (length < 1 || offset + length > buffer.Length)
292 if (length < 1 || offset + length > buffer.Length)
293 throw new ArgumentOutOfRangeException("length");
293 throw new ArgumentOutOfRangeException("length");
294
294
295 var chunk = m_first;
295 var chunk = m_first;
296 bool recycle;
296 bool recycle;
297 dequeued = 0;
297 dequeued = 0;
298 while (chunk != null) {
298 while (chunk != null) {
299
299
300 int actual;
300 int actual;
301 if (chunk.TryDequeueBatch(buffer, offset, length, out actual, out recycle)) {
301 if (chunk.TryDequeueBatch(buffer, offset, length, out actual, out recycle)) {
302 offset += actual;
302 offset += actual;
303 length -= actual;
303 length -= actual;
304 dequeued += actual;
304 dequeued += actual;
305 }
305 }
306
306
307 if (recycle) // this chunk is waste
307 if (recycle) // this chunk is waste
308 RecycleFirstChunk(chunk);
308 RecycleFirstChunk(chunk);
309 else if (actual == 0)
309 else if (actual == 0)
310 break; // the chunk is usable, but doesn't contain any data (it's the last chunk in the queue)
310 break; // the chunk is usable, but doesn't contain any data (it's the last chunk in the queue)
311
311
312 if (length == 0)
312 if (length == 0)
313 return true;
313 return true;
314
314
315 // we still may dequeue something
315 // we still may dequeue something
316 // try again
316 // try again
317 chunk = m_first;
317 chunk = m_first;
318 }
318 }
319
319
320 return dequeued != 0;
320 return dequeued != 0;
321 }
321 }
322
322
323 /// <summary>
323 /// <summary>
324 /// Tries to dequeue all remaining data in the first chunk.
324 /// Tries to dequeue all remaining data in the first chunk.
325 /// </summary>
325 /// </summary>
326 /// <returns><c>true</c>, if data was dequeued, <c>false</c> otherwise.</returns>
326 /// <returns><c>true</c>, if data was dequeued, <c>false</c> otherwise.</returns>
327 /// <param name="buffer">The buffer to which the data will be written.</param>
327 /// <param name="buffer">The buffer to which the data will be written.</param>
328 /// <param name="offset">The offset in the buffer at which the data will be written.</param>
328 /// <param name="offset">The offset in the buffer at which the data will be written.</param>
329 /// <param name="length">Tha maximum amount of the data to be dequeued.</param>
329 /// <param name="length">Tha maximum amount of the data to be dequeued.</param>
330 /// <param name="dequeued">The actual amount of the dequeued data.</param>
330 /// <param name="dequeued">The actual amount of the dequeued data.</param>
331 public bool TryDequeueChunk(T[] buffer, int offset, int length, out int dequeued) {
331 public bool TryDequeueChunk(T[] buffer, int offset, int length, out int dequeued) {
332 if (buffer == null)
332 if (buffer == null)
333 throw new ArgumentNullException("buffer");
333 throw new ArgumentNullException("buffer");
334 if (offset < 0)
334 if (offset < 0)
335 throw new ArgumentOutOfRangeException("offset");
335 throw new ArgumentOutOfRangeException("offset");
336 if (length < 1 || offset + length > buffer.Length)
336 if (length < 1 || offset + length > buffer.Length)
337 throw new ArgumentOutOfRangeException("length");
337 throw new ArgumentOutOfRangeException("length");
338
338
339 var chunk = m_first;
339 var chunk = m_first;
340 bool recycle;
340 bool recycle;
341 dequeued = 0;
341 dequeued = 0;
342
342
343 while (chunk != null) {
343 while (chunk != null) {
344
344
345 int actual;
345 int actual;
346 if (chunk.TryDequeueBatch(buffer, offset, length, out actual, out recycle)) {
346 if (chunk.TryDequeueBatch(buffer, offset, length, out actual, out recycle)) {
347 dequeued = actual;
347 dequeued = actual;
348 }
348 }
349
349
350 if (recycle) // this chunk is waste
350 if (recycle) // this chunk is waste
351 RecycleFirstChunk(chunk);
351 RecycleFirstChunk(chunk);
352
352
353 // if we have dequeued any data, then return
353 // if we have dequeued any data, then return
354 if (dequeued != 0)
354 if (dequeued != 0)
355 return true;
355 return true;
356
356
357 // we still may dequeue something
357 // we still may dequeue something
358 // try again
358 // try again
359 chunk = m_first;
359 chunk = m_first;
360 }
360 }
361
361
362 return false;
362 return false;
363 }
363 }
364
364
365 bool EnqueueChunk(Chunk last, Chunk chunk) {
365 bool EnqueueChunk(Chunk last, Chunk chunk) {
366 if (Interlocked.CompareExchange(ref m_last, chunk, last) != last)
366 if (Interlocked.CompareExchange(ref m_last, chunk, last) != last)
367 return false;
367 return false;
368
368
369 if (last != null)
369 if (last != null)
370 last.next = chunk;
370 last.next = chunk;
371 else {
371 else {
372 m_first = chunk;
372 m_first = chunk;
373 }
373 }
374 return true;
374 return true;
375 }
375 }
376
376
377 void RecycleFirstChunk(Chunk first) {
377 void RecycleFirstChunk(Chunk first) {
378 var next = first.next;
378 var next = first.next;
379
379
380 if (first != Interlocked.CompareExchange(ref m_first, next, first))
380 if (first != Interlocked.CompareExchange(ref m_first, next, first))
381 return;
381 return;
382
382
383 if (next == null) {
383 if (next == null) {
384
384
385 if (first != Interlocked.CompareExchange(ref m_last, null, first)) {
385 if (first != Interlocked.CompareExchange(ref m_last, null, first)) {
386 /*while (first.next == null)
386 /*while (first.next == null)
387 Thread.MemoryBarrier();*/
387 Thread.MemoryBarrier();*/
388
388
389 // race
389 // race
390 // someone already updated the tail, restore the pointer to the queue head
390 // someone already updated the tail, restore the pointer to the queue head
391 m_first = first;
391 m_first = first;
392 }
392 }
393 // the tail is updated
393 // the tail is updated
394 }
394 }
395
395
396 // we need to update the head
396 // we need to update the head
397 //Interlocked.CompareExchange(ref m_first, next, first);
397 //Interlocked.CompareExchange(ref m_first, next, first);
398 // if the head is already updated then give up
398 // if the head is already updated then give up
399 //return;
399 //return;
400
400
401 }
401 }
402
402
403 public void Clear() {
403 public void Clear() {
404 // start the new queue
404 // start the new queue
405 var chunk = new Chunk(DEFAULT_CHUNK_SIZE);
405 var chunk = new Chunk(DEFAULT_CHUNK_SIZE);
406
406
407 do {
407 do {
408 Thread.MemoryBarrier();
408 Thread.MemoryBarrier();
409 var first = m_first;
409 var first = m_first;
410 var last = m_last;
410 var last = m_last;
411
411
412 if (last == null) // nothing to clear
412 if (last == null) // nothing to clear
413 return;
413 return;
414
414
415 if (first == null || (first.next == null && first != last)) // inconcistency
415 if (first == null || (first.next == null && first != last)) // inconcistency
416 continue;
416 continue;
417
417
418 // here we will create inconsistency which will force others to spin
418 // here we will create inconsistency which will force others to spin
419 // and prevent from fetching. chunk.next = null
419 // and prevent from fetching. chunk.next = null
420 if (first != Interlocked.CompareExchange(ref m_first, chunk, first))
420 if (first != Interlocked.CompareExchange(ref m_first, chunk, first))
421 continue;// inconsistent
421 continue;// inconsistent
422
422
423 m_last = chunk;
423 m_last = chunk;
424
424
425 return;
425 return;
426
426
427 } while(true);
427 } while(true);
428 }
428 }
429
429
430 public T[] Drain() {
430 public T[] Drain() {
431 // start the new queue
431 // start the new queue
432 var chunk = new Chunk(DEFAULT_CHUNK_SIZE);
432 var chunk = new Chunk(DEFAULT_CHUNK_SIZE);
433
433
434 do {
434 do {
435 Thread.MemoryBarrier();
435 Thread.MemoryBarrier();
436 var first = m_first;
436 var first = m_first;
437 var last = m_last;
437 var last = m_last;
438
438
439 if (last == null)
439 if (last == null)
440 return new T[0];
440 return new T[0];
441
441
442 if (first == null || (first.next == null && first != last))
442 if (first == null || (first.next == null && first != last))
443 continue;
443 continue;
444
444
445 // here we will create inconsistency which will force others to spin
445 // here we will create inconsistency which will force others to spin
446 // and prevent from fetching. chunk.next = null
446 // and prevent from fetching. chunk.next = null
447 if (first != Interlocked.CompareExchange(ref m_first, chunk, first))
447 if (first != Interlocked.CompareExchange(ref m_first, chunk, first))
448 continue;// inconsistent
448 continue;// inconsistent
449
449
450 last = Interlocked.Exchange(ref m_last, chunk);
450 last = Interlocked.Exchange(ref m_last, chunk);
451
451
452 return ReadChunks(first, last);
452 return ReadChunks(first, last);
453
453
454 } while(true);
454 } while(true);
455 }
455 }
456
456
457 static T[] ReadChunks(Chunk chunk, object last) {
457 static T[] ReadChunks(Chunk chunk, object last) {
458 var result = new List<T>();
458 var result = new List<T>();
459 var buffer = new T[DEFAULT_CHUNK_SIZE];
459 var buffer = new T[DEFAULT_CHUNK_SIZE];
460 int actual;
460 int actual;
461 bool recycle;
461 bool recycle;
462 while (chunk != null) {
462 while (chunk != null) {
463 // ensure all write operations on the chunk are complete
463 // ensure all write operations on the chunk are complete
464 chunk.Commit();
464 chunk.Commit();
465
465
466 // we need to read the chunk using this way
466 // we need to read the chunk using this way
467 // since some client still may completing the dequeue
467 // since some client still may completing the dequeue
468 // operation, such clients most likely won't get results
468 // operation, such clients most likely won't get results
469 while (chunk.TryDequeueBatch(buffer, 0, buffer.Length, out actual, out recycle))
469 while (chunk.TryDequeueBatch(buffer, 0, buffer.Length, out actual, out recycle))
470 result.AddRange(new ArraySegmentCollection(buffer, 0, actual));
470 result.AddRange(new ArraySegmentCollection(buffer, 0, actual));
471
471
472 if (chunk == last) {
472 if (chunk == last) {
473 chunk = null;
473 chunk = null;
474 } else {
474 } else {
475 while (chunk.next == null)
475 while (chunk.next == null)
476 Thread.MemoryBarrier();
476 Thread.MemoryBarrier();
477 chunk = chunk.next;
477 chunk = chunk.next;
478 }
478 }
479 }
479 }
480
480
481 return result.ToArray();
481 return result.ToArray();
482 }
482 }
483
483
484 struct ArraySegmentCollection : ICollection<T> {
484 struct ArraySegmentCollection : ICollection<T> {
485 readonly T[] m_data;
485 readonly T[] m_data;
486 readonly int m_offset;
486 readonly int m_offset;
487 readonly int m_length;
487 readonly int m_length;
488
488
489 public ArraySegmentCollection(T[] data, int offset, int length) {
489 public ArraySegmentCollection(T[] data, int offset, int length) {
490 m_data = data;
490 m_data = data;
491 m_offset = offset;
491 m_offset = offset;
492 m_length = length;
492 m_length = length;
493 }
493 }
494
494
495 #region ICollection implementation
495 #region ICollection implementation
496
496
497 public void Add(T item) {
497 public void Add(T item) {
498 throw new InvalidOperationException();
498 throw new NotSupportedException();
499 }
499 }
500
500
501 public void Clear() {
501 public void Clear() {
502 throw new InvalidOperationException();
502 throw new NotSupportedException();
503 }
503 }
504
504
505 public bool Contains(T item) {
505 public bool Contains(T item) {
506 return false;
506 return false;
507 }
507 }
508
508
509 public void CopyTo(T[] array, int arrayIndex) {
509 public void CopyTo(T[] array, int arrayIndex) {
510 Array.Copy(m_data,m_offset,array,arrayIndex, m_length);
510 Array.Copy(m_data,m_offset,array,arrayIndex, m_length);
511 }
511 }
512
512
513 public bool Remove(T item) {
513 public bool Remove(T item) {
514 throw new NotImplementedException();
514 throw new NotSupportedException();
515 }
515 }
516
516
517 public int Count {
517 public int Count {
518 get {
518 get {
519 return m_length;
519 return m_length;
520 }
520 }
521 }
521 }
522
522
523 public bool IsReadOnly {
523 public bool IsReadOnly {
524 get {
524 get {
525 return true;
525 return true;
526 }
526 }
527 }
527 }
528
528
529 #endregion
529 #endregion
530
530
531 #region IEnumerable implementation
531 #region IEnumerable implementation
532
532
533 public IEnumerator<T> GetEnumerator() {
533 public IEnumerator<T> GetEnumerator() {
534 for (int i = m_offset; i < m_length + m_offset; i++)
534 for (int i = m_offset; i < m_length + m_offset; i++)
535 yield return m_data[i];
535 yield return m_data[i];
536 }
536 }
537
537
538 #endregion
538 #endregion
539
539
540 #region IEnumerable implementation
540 #region IEnumerable implementation
541
541
542 IEnumerator IEnumerable.GetEnumerator() {
542 IEnumerator IEnumerable.GetEnumerator() {
543 return GetEnumerator();
543 return GetEnumerator();
544 }
544 }
545
545
546 #endregion
546 #endregion
547 }
547 }
548
548
549 #region IEnumerable implementation
549 #region IEnumerable implementation
550
550
551 class Enumerator : IEnumerator<T> {
551 class Enumerator : IEnumerator<T> {
552 Chunk m_current;
552 Chunk m_current;
553 int m_pos = -1;
553 int m_pos = -1;
554
554
555 public Enumerator(Chunk fisrt) {
555 public Enumerator(Chunk fisrt) {
556 m_current = fisrt;
556 m_current = fisrt;
557 }
557 }
558
558
559 #region IEnumerator implementation
559 #region IEnumerator implementation
560
560
561 public bool MoveNext() {
561 public bool MoveNext() {
562 if (m_current == null)
562 if (m_current == null)
563 return false;
563 return false;
564
564
565 if (m_pos == -1)
565 if (m_pos == -1)
566 m_pos = m_current.Low;
566 m_pos = m_current.Low;
567 else
567 else
568 m_pos++;
568 m_pos++;
569
569
570 if (m_pos == m_current.Hi) {
570 if (m_pos == m_current.Hi) {
571
571
572 m_current = m_pos == m_current.Size ? m_current.next : null;
572 m_current = m_pos == m_current.Size ? m_current.next : null;
573
573
574 m_pos = 0;
574 m_pos = 0;
575
575
576 if (m_current == null)
576 if (m_current == null)
577 return false;
577 return false;
578 }
578 }
579
579
580 return true;
580 return true;
581 }
581 }
582
582
583 public void Reset() {
583 public void Reset() {
584 throw new NotSupportedException();
584 throw new NotSupportedException();
585 }
585 }
586
586
587 object IEnumerator.Current {
587 object IEnumerator.Current {
588 get {
588 get {
589 return Current;
589 return Current;
590 }
590 }
591 }
591 }
592
592
593 #endregion
593 #endregion
594
594
595 #region IDisposable implementation
595 #region IDisposable implementation
596
596
597 public void Dispose() {
597 public void Dispose() {
598 }
598 }
599
599
600 #endregion
600 #endregion
601
601
602 #region IEnumerator implementation
602 #region IEnumerator implementation
603
603
604 public T Current {
604 public T Current {
605 get {
605 get {
606 if (m_pos == -1 || m_current == null)
606 if (m_pos == -1 || m_current == null)
607 throw new InvalidOperationException();
607 throw new InvalidOperationException();
608 return m_current.GetAt(m_pos);
608 return m_current.GetAt(m_pos);
609 }
609 }
610 }
610 }
611
611
612 #endregion
612 #endregion
613 }
613 }
614
614
615 public IEnumerator<T> GetEnumerator() {
615 public IEnumerator<T> GetEnumerator() {
616 return new Enumerator(m_first);
616 return new Enumerator(m_first);
617 }
617 }
618
618
619 #endregion
619 #endregion
620
620
621 #region IEnumerable implementation
621 #region IEnumerable implementation
622
622
623 IEnumerator IEnumerable.GetEnumerator() {
623 IEnumerator IEnumerable.GetEnumerator() {
624 return GetEnumerator();
624 return GetEnumerator();
625 }
625 }
626
626
627 #endregion
627 #endregion
628 }
628 }
629 }
629 }
@@ -1,31 +1,31
1 using System;
1 using System;
2 using System.Threading;
2 using System.Threading;
3
3
4 namespace Implab.Parallels {
4 namespace Implab.Parallels {
5 /// <summary>
5 /// <summary>
6 /// Implements simple signalling logic using <see cref="Monitor.PulseAll(object)"/>.
6 /// Implements a simple signalling logic using <see cref="Monitor.PulseAll(object)"/>.
7 /// </summary>
7 /// </summary>
8 public class Signal {
8 public class Signal {
9 readonly object m_lock = new object();
9 readonly object m_lock = new object();
10 bool m_state;
10 bool m_state;
11
11
12 public void Set() {
12 public void Set() {
13 lock(m_lock) {
13 lock(m_lock) {
14 m_state = true;
14 m_state = true;
15 Monitor.PulseAll(m_lock);
15 Monitor.PulseAll(m_lock);
16 }
16 }
17 }
17 }
18
18
19 public void Wait() {
19 public void Wait() {
20 lock (m_lock)
20 lock (m_lock)
21 if (!m_state)
21 if (!m_state)
22 Monitor.Wait(m_lock);
22 Monitor.Wait(m_lock);
23 }
23 }
24
24
25 public bool Wait(int timeout) {
25 public bool Wait(int timeout) {
26 lock (m_lock)
26 lock (m_lock)
27 return m_state || Monitor.Wait(m_lock, timeout);
27 return m_state || Monitor.Wait(m_lock, timeout);
28 }
28 }
29 }
29 }
30 }
30 }
31
31
@@ -1,82 +1,82
1 using System;
1 using System;
2 using System.Threading;
2 using System.Threading;
3 using System.Diagnostics;
3 using System.Diagnostics;
4 using Implab.Diagnostics;
4 using Implab.Diagnostics;
5
5
6 namespace Implab.Parallels {
6 namespace Implab.Parallels {
7 public class WorkerPool : DispatchPool<Action> {
7 public class WorkerPool : DispatchPool<Action> {
8
8
9 AsyncQueue<Action> m_queue = new AsyncQueue<Action>();
9 AsyncQueue<Action> m_queue = new AsyncQueue<Action>();
10 int m_queueLength = 0;
10 int m_queueLength;
11 readonly int m_threshold = 1;
11 readonly int m_threshold = 1;
12
12
13 public WorkerPool(int minThreads, int maxThreads, int threshold)
13 public WorkerPool(int minThreads, int maxThreads, int threshold)
14 : base(minThreads, maxThreads) {
14 : base(minThreads, maxThreads) {
15 m_threshold = threshold;
15 m_threshold = threshold;
16 InitPool();
16 InitPool();
17 }
17 }
18
18
19 public WorkerPool(int minThreads, int maxThreads) :
19 public WorkerPool(int minThreads, int maxThreads) :
20 base(minThreads, maxThreads) {
20 base(minThreads, maxThreads) {
21 InitPool();
21 InitPool();
22 }
22 }
23
23
24 public WorkerPool(int threads)
24 public WorkerPool(int threads)
25 : base(threads) {
25 : base(threads) {
26 InitPool();
26 InitPool();
27 }
27 }
28
28
29 public WorkerPool() {
29 public WorkerPool() {
30 InitPool();
30 InitPool();
31 }
31 }
32
32
33 public Promise<T> Invoke<T>(Func<T> task) {
33 public Promise<T> Invoke<T>(Func<T> task) {
34 if (task == null)
34 if (task == null)
35 throw new ArgumentNullException("task");
35 throw new ArgumentNullException("task");
36 if (IsDisposed)
36 if (IsDisposed)
37 throw new ObjectDisposedException(ToString());
37 throw new ObjectDisposedException(ToString());
38
38
39 var promise = new Promise<T>();
39 var promise = new Promise<T>();
40
40
41 var lop = TraceContext.Instance.CurrentOperation;
41 var lop = TraceContext.Instance.CurrentOperation;
42
42
43 EnqueueTask(delegate() {
43 EnqueueTask(delegate {
44 TraceContext.Instance.EnterLogicalOperation(lop, false);
44 TraceContext.Instance.EnterLogicalOperation(lop, false);
45 try {
45 try {
46 promise.Resolve(task());
46 promise.Resolve(task());
47 } catch (Exception e) {
47 } catch (Exception e) {
48 promise.Reject(e);
48 promise.Reject(e);
49 } finally {
49 } finally {
50 TraceContext.Instance.Leave();
50 TraceContext.Instance.Leave();
51 }
51 }
52 });
52 });
53
53
54 return promise;
54 return promise;
55 }
55 }
56
56
57 protected void EnqueueTask(Action unit) {
57 protected void EnqueueTask(Action unit) {
58 Debug.Assert(unit != null);
58 Debug.Assert(unit != null);
59 var len = Interlocked.Increment(ref m_queueLength);
59 var len = Interlocked.Increment(ref m_queueLength);
60 m_queue.Enqueue(unit);
60 m_queue.Enqueue(unit);
61
61
62 if (len > m_threshold * PoolSize) {
62 if (len > m_threshold * PoolSize) {
63 StartWorker();
63 StartWorker();
64 }
64 }
65
65
66 SignalThread();
66 SignalThread();
67 }
67 }
68
68
69 protected override bool TryDequeue(out Action unit) {
69 protected override bool TryDequeue(out Action unit) {
70 if (m_queue.TryDequeue(out unit)) {
70 if (m_queue.TryDequeue(out unit)) {
71 Interlocked.Decrement(ref m_queueLength);
71 Interlocked.Decrement(ref m_queueLength);
72 return true;
72 return true;
73 }
73 }
74 return false;
74 return false;
75 }
75 }
76
76
77 protected override void InvokeUnit(Action unit) {
77 protected override void InvokeUnit(Action unit) {
78 unit();
78 unit();
79 }
79 }
80
80
81 }
81 }
82 }
82 }
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now