Mercurial > pub > ImplabNet
changeset 71:1714fd8678ef
code cleanup
author | cin |
---|---|
date | Wed, 03 Sep 2014 18:34:02 +0400 |
parents | 0349189d2564 |
children | d67b95eddaf4 0fdaf280c797 f1da3afc3521 |
files | Implab/Parallels/MTQueue.cs Implab/Promise.cs |
diffstat | 2 files changed, 29 insertions(+), 29 deletions(-) [+] |
line wrap: on
line diff
--- a/Implab/Parallels/MTQueue.cs Wed Sep 03 11:57:43 2014 +0400 +++ b/Implab/Parallels/MTQueue.cs Wed Sep 03 18:34:02 2014 +0400 @@ -44,27 +44,27 @@ // this is the last element, // then try to update the tail if (first != Interlocked.CompareExchange(ref m_last, null, first)) { - // this is a race condition + // this is the race condition if (m_last == null) // the queue is empty return false; - // tail has been changed, than we need to restart + // tail has been changed, we need to restart continue; } // tail succesfully updated and first.next will never be changed - // other readers will fail due to inconsistency m_last != m_fist, but m_first.next == null - // but the writer may update the m_first since the m_last is null + // other readers will fail due to inconsistency m_last != m_fist && m_first.next == null + // however the parallel writer may update the m_first since the m_last is null - // so we need to fix inconsistency by setting m_first to null, but if it already has been - // updated by a writer then we should just give up + // so we need to fix inconsistency by setting m_first to null or if it has been + // updated by the writer already then we should just to give up Interlocked.CompareExchange(ref m_first, null, first); break; } else { - if (first == Interlocked.CompareExchange(ref m_first, next, first)) - // head succesfully updated - break; + if (first == Interlocked.CompareExchange(ref m_first, next, first)) + // head succesfully updated + break; } } while (true);
--- a/Implab/Promise.cs Wed Sep 03 11:57:43 2014 +0400 +++ b/Implab/Promise.cs Wed Sep 03 18:34:02 2014 +0400 @@ -80,11 +80,11 @@ } } - const int UnresolvedSate = 0; - const int TransitionalState = 1; - const int SucceededState = 2; - const int RejectedState = 3; - const int CancelledState = 4; + const int UNRESOLVED_SATE = 0; + const int TRANSITIONAL_STATE = 1; + const int SUCCEEDED_STATE = 2; + const int REJECTED_STATE = 3; + const int CANCELLED_STATE = 4; readonly bool m_cancellable; @@ -113,16 +113,16 @@ } bool BeginTransit() { - return UnresolvedSate == Interlocked.CompareExchange(ref m_state, TransitionalState, UnresolvedSate); + return UNRESOLVED_SATE == Interlocked.CompareExchange(ref m_state, TRANSITIONAL_STATE, UNRESOLVED_SATE); } void CompleteTransit(int state) { - if (TransitionalState != Interlocked.CompareExchange(ref m_state, state, TransitionalState)) + if (TRANSITIONAL_STATE != Interlocked.CompareExchange(ref m_state, state, TRANSITIONAL_STATE)) throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state"); } void WaitTransition() { - while (m_state == TransitionalState) { + while (m_state == TRANSITIONAL_STATE) { /* noop */ } } @@ -135,7 +135,7 @@ public bool IsCancelled { get { - return m_state == CancelledState; + return m_state == CANCELLED_STATE; } } @@ -151,11 +151,11 @@ public void Resolve(T result) { if (BeginTransit()) { m_result = result; - CompleteTransit(SucceededState); + CompleteTransit(SUCCEEDED_STATE); OnStateChanged(); } else { WaitTransition(); - if (m_state != CancelledState) + if (m_state != CANCELLED_STATE) throw new InvalidOperationException("The promise is already resolved"); } } @@ -183,11 +183,11 @@ public void Reject(Exception error) { if (BeginTransit()) { m_error = error; - CompleteTransit(RejectedState); + CompleteTransit(REJECTED_STATE); OnStateChanged(); } else { WaitTransition(); - if (m_state == SucceededState) + if (m_state == SUCCEEDED_STATE) throw new InvalidOperationException("The promise is already resolved"); } } @@ -198,7 +198,7 @@ /// <returns><c>true</c> Операция была отменена, обработчики не будут вызваны.<c>false</c> отмена не возможна, поскольку обещание уже выполнено и обработчики отработали.</returns> public bool Cancel() { if (BeginTransit()) { - CompleteTransit(CancelledState); + CompleteTransit(CANCELLED_STATE); OnStateChanged(); return true; } else { @@ -545,11 +545,11 @@ throw new TimeoutException(); switch (m_state) { - case SucceededState: + case SUCCEEDED_STATE: return m_result; - case CancelledState: + case CANCELLED_STATE: throw new OperationCanceledException(); - case RejectedState: + case REJECTED_STATE: throw new TargetInvocationException(m_error); default: throw new ApplicationException(String.Format("Invalid promise state {0}", m_state)); @@ -592,13 +592,13 @@ protected virtual void InvokeHandler(HandlerDescriptor handler) { switch (m_state) { - case SucceededState: + case SUCCEEDED_STATE: handler.Resolve(m_result); break; - case RejectedState: + case REJECTED_STATE: handler.Reject(m_error); break; - case CancelledState: + case CANCELLED_STATE: handler.Cancel(); break; default: