# HG changeset patch # User cin # Date 2014-09-03 14:34:02 # Node ID 1714fd8678efdc39734f3b99fbe23445e414f59d # Parent 0349189d25647d369de7e5f19cf34ae3f7678920 code cleanup diff --git a/Implab/Parallels/MTQueue.cs b/Implab/Parallels/MTQueue.cs --- a/Implab/Parallels/MTQueue.cs +++ b/Implab/Parallels/MTQueue.cs @@ -44,27 +44,27 @@ namespace Implab.Parallels { // 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); diff --git a/Implab/Promise.cs b/Implab/Promise.cs --- a/Implab/Promise.cs +++ b/Implab/Promise.cs @@ -80,11 +80,11 @@ namespace Implab { } } - 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 @@ namespace Implab { } 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 @@ namespace Implab { public bool IsCancelled { get { - return m_state == CancelledState; + return m_state == CANCELLED_STATE; } } @@ -151,11 +151,11 @@ namespace Implab { 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 @@ namespace Implab { 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 @@ namespace Implab { /// true Операция была отменена, обработчики не будут вызваны.false отмена не возможна, поскольку обещание уже выполнено и обработчики отработали. public bool Cancel() { if (BeginTransit()) { - CompleteTransit(CancelledState); + CompleteTransit(CANCELLED_STATE); OnStateChanged(); return true; } else { @@ -545,11 +545,11 @@ namespace Implab { 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 @@ namespace Implab { 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: