annotate Implab/Parallels/SimpleAsyncQueue.cs @ 274:22629bf26121 v3

Unity xml configuration, alpha2
author cin
date Fri, 27 Apr 2018 04:47:52 +0300
parents cbe10ac0731e
children
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
242
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
18 // the writer creates a new node, moves m_last to this node and
233
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
19 // only after that restores the reference from the previous node
242
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
20 // making the reader be able to read the new node.
233
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
21
242
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
22 volatile Node m_first; // position on the node which is already read
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
23 volatile Node m_last; // position on the node which is already written
233
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;
242
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
38
233
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) {
242
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
42 Node first = m_first; ;
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
43 Node next = first.next; ;
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
44
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
45 if (next == null) {
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
46 value = default(T);
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
47 return false;
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
48 }
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
49
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
50 var first2 = Interlocked.CompareExchange(ref m_first, next, first);
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
51
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
52 if (first != first2) {
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
53 // head is updated by someone else
233
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
54
242
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
55 SpinWait spin = new SpinWait();
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
56 do {
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
57 first = first2;
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
58 next = first.next;
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
59 if (next == null) {
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
60 value = default(T);
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
61 return false;
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
62 }
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
63
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
64 first2 = Interlocked.CompareExchange(ref m_first, next, first);
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
65 if (first == first2)
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
66 break;
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
67 spin.SpinOnce();
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
68 } while (true);
cbe10ac0731e Working on promises
cin
parents: 233
diff changeset
69 }
233
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
70
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
71 value = next.value;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
72 return true;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
73 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
74
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
75 #region IEnumerable implementation
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
76
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
77 class Enumerator : IEnumerator<T> {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
78 Node m_current;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
79 Node m_first;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
80
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
81 public Enumerator(Node first) {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
82 m_first = first;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
83 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
84
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
85 #region IEnumerator implementation
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
86
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
87 public bool MoveNext() {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
88 m_current = m_current == null ? m_first : m_current.next;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
89 return m_current != null;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
90 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
91
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
92 public void Reset() {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
93 m_current = null;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
94 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
95
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
96 object IEnumerator.Current {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
97 get {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
98 if (m_current == null)
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
99 throw new InvalidOperationException();
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
100 return m_current.value;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
101 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
102 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
103
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
104 #endregion
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
105
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
106 #region IDisposable implementation
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
107
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
108 public void Dispose() {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
109 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
110
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
111 #endregion
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
112
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
113 #region IEnumerator implementation
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
114
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
115 public T Current {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
116 get {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
117 if (m_current == null)
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
118 throw new InvalidOperationException();
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
119 return m_current.value;
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
120 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
121 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
122
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
123 #endregion
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
124 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
125
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
126 public IEnumerator<T> GetEnumerator() {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
127 return new Enumerator(m_first);
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
128 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
129
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
130 #endregion
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
131
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
132 #region IEnumerable implementation
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
133
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
134 IEnumerator IEnumerable.GetEnumerator() {
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
135 return GetEnumerator();
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
136 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
137
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
138 #endregion
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
139 }
d6fe09f5592c Improved AsyncQueue
cin
parents:
diff changeset
140 }