comparison Implab/Parallels/MTQueue.cs @ 71:1714fd8678ef

code cleanup
author cin
date Wed, 03 Sep 2014 18:34:02 +0400
parents ee04e1fa78da
children 4f20870d0816
comparison
equal deleted inserted replaced
70:0349189d2564 71:1714fd8678ef
42 next = first.next; 42 next = first.next;
43 if (next == null) { 43 if (next == null) {
44 // this is the last element, 44 // this is the last element,
45 // then try to update the tail 45 // then try to update the tail
46 if (first != Interlocked.CompareExchange(ref m_last, null, first)) { 46 if (first != Interlocked.CompareExchange(ref m_last, null, first)) {
47 // this is a race condition 47 // this is the race condition
48 if (m_last == null) 48 if (m_last == null)
49 // the queue is empty 49 // the queue is empty
50 return false; 50 return false;
51 // tail has been changed, than we need to restart 51 // tail has been changed, we need to restart
52 continue; 52 continue;
53 } 53 }
54 54
55 // tail succesfully updated and first.next will never be changed 55 // tail succesfully updated and first.next will never be changed
56 // other readers will fail due to inconsistency m_last != m_fist, but m_first.next == null 56 // other readers will fail due to inconsistency m_last != m_fist && m_first.next == null
57 // but the writer may update the m_first since the m_last is null 57 // however the parallel writer may update the m_first since the m_last is null
58 58
59 // so we need to fix inconsistency by setting m_first to null, but if it already has been 59 // so we need to fix inconsistency by setting m_first to null or if it has been
60 // updated by a writer then we should just give up 60 // updated by the writer already then we should just to give up
61 Interlocked.CompareExchange(ref m_first, null, first); 61 Interlocked.CompareExchange(ref m_first, null, first);
62 break; 62 break;
63 63
64 } else { 64 } else {
65 if (first == Interlocked.CompareExchange(ref m_first, next, first)) 65 if (first == Interlocked.CompareExchange(ref m_first, next, first))
66 // head succesfully updated 66 // head succesfully updated
67 break; 67 break;
68 } 68 }
69 } while (true); 69 } while (true);
70 70
71 value = first.value; 71 value = first.value;
72 return true; 72 return true;