annotate Implab/Parallels/SimpleAsyncQueue.cs @ 238:bdfdba6b645b v2

fixed unpredictable Safe.Dispose behaviour
author cin
date Fri, 01 Dec 2017 01:28:56 +0300
parents d6fe09f5592c
children cbe10ac0731e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
233
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
1 using System.Threading;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
2 using System.Collections.Generic;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
3 using System;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
4 using System.Collections;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
5
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
6 namespace Implab.Parallels {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
7 public class SimpleAsyncQueue<T> : IEnumerable<T> {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
8 class Node {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
9 public Node(T value) {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
10 this.value = value;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
11 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
12 public readonly T value;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
13 public volatile Node next;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
14 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
15
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
16 // the reader and the writer are mainteined completely independent,
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
17 // the reader can read next item when m_first.next is not null
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
18 // the writer creates the a new node, moves m_last to this node and
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
19 // only after that restores the reference from the previous node
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
20 // making available the reader to read the new node.
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
21
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
22 Node m_first; // position on the node which is already read
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
23 Node m_last; // position on the node which is already written
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
24
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
25 public SimpleAsyncQueue() {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
26 m_first = m_last = new Node(default(T));
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
27 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
28
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
29 public void Enqueue(T value) {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
30 var next = new Node(value);
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
31
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
32 // Interlocaked.CompareExchange implies Thread.MemoryBarrier();
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
33 // to ensure that the next node is completely constructed
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
34 var last = Interlocked.Exchange(ref m_last, next);
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
35
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
36 // release-fence
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
37 last.next = next;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
38
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
39 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
40
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
41 public bool TryDequeue(out T value) {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
42 Node first;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
43 Node next;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
44
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
45 Thread.MemoryBarrier(); // ensure m_first is fresh
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
46 SpinWait spin = new SpinWait();
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
47 do {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
48 first = m_first;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
49 // aquire-fence
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
50 next = first.next;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
51 if (next == null) {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
52 value = default(T);
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
53 return false;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
54 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
55
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
56 if (first == Interlocked.CompareExchange(ref m_first, next, first))
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
57 // head succesfully updated
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
58 break;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
59 spin.SpinOnce();
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
60 } while (true);
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
61
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
62 value = next.value;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
63 return true;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
64 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
65
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
66 #region IEnumerable implementation
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
67
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
68 class Enumerator : IEnumerator<T> {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
69 Node m_current;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
70 Node m_first;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
71
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
72 public Enumerator(Node first) {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
73 m_first = first;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
74 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
75
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
76 #region IEnumerator implementation
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
77
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
78 public bool MoveNext() {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
79 m_current = m_current == null ? m_first : m_current.next;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
80 return m_current != null;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
81 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
82
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
83 public void Reset() {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
84 m_current = null;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
85 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
86
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
87 object IEnumerator.Current {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
88 get {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
89 if (m_current == null)
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
90 throw new InvalidOperationException();
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
91 return m_current.value;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
92 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
93 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
94
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
95 #endregion
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
96
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
97 #region IDisposable implementation
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
98
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
99 public void Dispose() {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
100 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
101
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
102 #endregion
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
103
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
104 #region IEnumerator implementation
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
105
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
106 public T Current {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
107 get {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
108 if (m_current == null)
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
109 throw new InvalidOperationException();
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
110 return m_current.value;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
111 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
112 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
113
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
114 #endregion
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
115 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
116
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
117 public IEnumerator<T> GetEnumerator() {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
118 return new Enumerator(m_first);
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
119 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
120
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
121 #endregion
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
122
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
123 #region IEnumerable implementation
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
124
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
125 IEnumerator IEnumerable.GetEnumerator() {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
126 return GetEnumerator();
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
127 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
128
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
129 #endregion
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
130 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
131 }