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