annotate Implab/Parallels/AsyncQueue.cs @ 120:f1b897999260 v2

improved asyncpool usability working on batch operations on asyncqueue
author cin
date Mon, 12 Jan 2015 05:19:52 +0300
parents 2573b562e328
children 62d2f1e98c4e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
1 using System.Threading;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
2 using System.Collections.Generic;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
3 using System;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
4 using System.Collections;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
5
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
6 namespace Implab.Parallels {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
7 public class AsyncQueue<T> : IEnumerable<T> {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
8 class Chunk {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
9 public Chunk next;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
10
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
11 int m_low;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
12 int m_hi;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
13 int m_alloc;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
14 readonly int m_size;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
15 readonly T[] m_data;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
16
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
17 public Chunk(int size) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
18 m_size = size;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
19 m_data = new T[size];
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
20 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
21
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
22 public Chunk(int size, T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
23 m_size = size;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
24 m_hi = 1;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
25 m_alloc = 1;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
26 m_data = new T[size];
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
27 m_data[0] = value;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
28 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
29
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
30 public int Low {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
31 get { return m_low; }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
32 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
33
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
34 public int Hi {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
35 get { return m_hi; }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
36 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
37
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
38 public bool TryEnqueue(T value,out bool extend) {
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
39 var alloc = Interlocked.Increment(ref m_alloc) - 1;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
40
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
41 if (alloc >= m_size) {
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
42 extend = alloc == m_size;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
43 return false;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
44 }
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
45
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
46 extend = false;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
47 m_data[alloc] = value;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
48
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
49 while (alloc != Interlocked.CompareExchange(ref m_hi, alloc + 1, alloc)) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
50 // spin wait for commit
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
51 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
52 return true;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
53 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
54
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
55 public bool TryDequeue(out T value,out bool recycle) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
56 int low;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
57 do {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
58 low = m_low;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
59 if (low >= m_hi) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
60 value = default(T);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
61 recycle = (low == m_size);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
62 return false;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
63 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
64 } while(low != Interlocked.CompareExchange(ref m_low, low + 1, low));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
65
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
66 recycle = (low == m_size - 1);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
67 value = m_data[low];
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
68
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
69 return true;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
70 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
71
120
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
72 public bool TryEnqueueBatch(T[] batch, int offset, int length, out int enqueued, out bool extend) {
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
73 int alloc;
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
74 int allocSize;
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
75
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
76 do {
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
77 alloc = m_alloc;
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
78
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
79 if (alloc > m_size) {
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
80 enqueued = 0;
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
81 extend = false;
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
82 return false;
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
83 }
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
84
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
85 allocSize = Math.Min(m_size - m_alloc, length);
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
86 } while(alloc != Interlocked.CompareExchange(ref m_alloc, alloc + allocSize, alloc));
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
87
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
88 if (alloc == m_size) {
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
89 enqueued = 0;
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
90 extend = true;
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
91 return false;
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
92 }
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
93
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
94 Array.Copy(batch, offset, m_data, alloc, allocSize);
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
95 enqueued = allocSize;
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
96 extend = false;
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
97
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
98 while (alloc != Interlocked.CompareExchange(ref m_hi, alloc + allocSize, alloc)) {
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
99 // spin wait for commit
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
100 }
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
101 return true;
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
102 }
f1b897999260 improved asyncpool usability
cin
parents: 119
diff changeset
103
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
104 public T GetAt(int pos) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
105 return m_data[pos];
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
106 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
107 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
108
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
109 public const int DEFAULT_CHUNK_SIZE = 32;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
110
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
111 readonly int m_chunkSize = DEFAULT_CHUNK_SIZE;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
112
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
113 Chunk m_first;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
114 Chunk m_last;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
115
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
116 public AsyncQueue() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
117 m_last = m_first = new Chunk(m_chunkSize);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
118 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
120 public void Enqueue(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
121 var last = m_last;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
122 // spin wait to the new chunk
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
123 bool extend = true;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
124 while(last == null || !last.TryEnqueue(value, out extend)) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
125 // try to extend queue
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
126 if (extend || last == null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
127 var chunk = new Chunk(m_chunkSize, value);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
128 if (EnqueueChunk(last, chunk))
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
129 break;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
130 last = m_last;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
131 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
132 while (last != m_last) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
133 Thread.MemoryBarrier();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
134 last = m_last;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
135 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
136 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
137 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
138 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
139
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
140 public bool TryDequeue(out T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
141 var chunk = m_first;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
142 bool recycle;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
143 while (chunk != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
144
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
145 var result = chunk.TryDequeue(out value, out recycle);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
146
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
147 if (recycle) // this chunk is waste
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
148 RecycleFirstChunk(chunk);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
149 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
150 return result; // this chunk is usable and returned actual result
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
151
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
152 if (result) // this chunk is waste but the true result is always actual
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
153 return true;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
154
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
155 // try again
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
156 chunk = m_first;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
157 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
158
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
159 // the queue is empty
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
160 value = default(T);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
161 return false;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
162 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
163
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
164 bool EnqueueChunk(Chunk last, Chunk chunk) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
165 if (Interlocked.CompareExchange(ref m_last, chunk, last) != last)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
166 return false;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
167
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
168 if (last != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
169 last.next = chunk;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
170 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
171 m_first = chunk;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
172 return true;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
173 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
174
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
175 void RecycleFirstChunk(Chunk first) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
176 var next = first.next;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
177
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
178 if (next == null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
179 // looks like this is the last chunk
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
180 if (first != Interlocked.CompareExchange(ref m_last, null, first)) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
181 // race
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
182 // maybe someone already recycled this chunk
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
183 // or a new chunk has been appedned to the queue
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
184
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
185 return; // give up
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
186 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
187 // the tail is updated
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
188 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
189
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
190 // we need to update the head
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
191 Interlocked.CompareExchange(ref m_first, next, first);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
192 // if the head is already updated then give up
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
193 return;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
194
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
195 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
196
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
197 #region IEnumerable implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
198
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
199 class Enumerator : IEnumerator<T> {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
200 Chunk m_current;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
201 int m_pos = -1;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
202
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
203 public Enumerator(Chunk fisrt) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
204 m_current = fisrt;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
205 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
206
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
207 #region IEnumerator implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
208
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
209 public bool MoveNext() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
210 if (m_current == null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
211 return false;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
212
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
213 if (m_pos == -1)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
214 m_pos = m_current.Low;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
215 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
216 m_pos++;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
217 if (m_pos == m_current.Hi) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
218 m_pos = 0;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
219 m_current = m_current.next;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
220 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
221
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
222 return true;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
223 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
224
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
225 public void Reset() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
226 throw new NotSupportedException();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
227 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
228
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
229 object IEnumerator.Current {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
230 get {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
231 return Current;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
232 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
233 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
234
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
235 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
236
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
237 #region IDisposable implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
238
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
239 public void Dispose() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
240 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
241
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
242 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
243
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
244 #region IEnumerator implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
245
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
246 public T Current {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
247 get {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
248 if (m_pos == -1 || m_current == null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
249 throw new InvalidOperationException();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
250 return m_current.GetAt(m_pos);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
251 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
252 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
253
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
254 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
255 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
256
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
257 public IEnumerator<T> GetEnumerator() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
258 return new Enumerator(m_first);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
259 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
260
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
261 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
262
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
263 #region IEnumerable implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
264
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
265 IEnumerator IEnumerable.GetEnumerator() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
266 return GetEnumerator();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
267 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
268
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
269 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
270 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
271 }