Mercurial > pub > ImplabNet
comparison Implab/Parallels/MTQueue.cs @ 93:dc4942d09e74 v2
improved tracing
added the application components container MTComponentContainer.AppContainer
author | cin |
---|---|
date | Thu, 23 Oct 2014 01:13:57 +0400 |
parents | 4f20870d0816 |
children | b11c7e9d93bc |
comparison
equal
deleted
inserted
replaced
92:4c0e5ef99986 | 93:dc4942d09e74 |
---|---|
1 using System; | 1 using System.Threading; |
2 using System.Collections.Generic; | |
3 using System.Linq; | |
4 using System.Text; | |
5 using System.Threading; | |
6 | 2 |
7 namespace Implab.Parallels { | 3 namespace Implab.Parallels { |
8 public class MTQueue<T> { | 4 public class MTQueue<T> { |
9 class Node { | 5 class Node { |
10 public Node(T value) { | 6 public Node(T value) { |
32 m_first = next; | 28 m_first = next; |
33 } | 29 } |
34 | 30 |
35 public bool TryDequeue(out T value) { | 31 public bool TryDequeue(out T value) { |
36 Node first; | 32 Node first; |
37 Node next = null; | 33 Node next; |
38 value = default(T); | 34 value = default(T); |
39 | 35 |
40 Thread.MemoryBarrier(); | 36 Thread.MemoryBarrier(); |
41 do { | 37 do { |
42 first = m_first; | 38 first = m_first; |
62 // so we need to fix inconsistency by setting m_first to null or if it has been | 58 // so we need to fix inconsistency by setting m_first to null or if it has been |
63 // updated by the writer already then we should just to give up | 59 // updated by the writer already then we should just to give up |
64 Interlocked.CompareExchange(ref m_first, null, first); | 60 Interlocked.CompareExchange(ref m_first, null, first); |
65 break; | 61 break; |
66 | 62 |
67 } else { | |
68 if (first == Interlocked.CompareExchange(ref m_first, next, first)) | |
69 // head succesfully updated | |
70 break; | |
71 } | 63 } |
64 if (first == Interlocked.CompareExchange(ref m_first, next, first)) | |
65 // head succesfully updated | |
66 break; | |
72 } while (true); | 67 } while (true); |
73 | 68 |
74 value = first.value; | 69 value = first.value; |
75 return true; | 70 return true; |
76 } | 71 } |