// // Author: Carson Jones // Adapted from "Art of Multiprocessor Programming" by Herlihy and Shavit // CS6966 // using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace UnboundedLockFreeQueue { public class AtomicReference where T : class { private volatile T m_Ref; private volatile Object refLock = new Object(); public AtomicReference(T initial) { m_Ref = initial; } /// /// If the current value == expect, update the reference to the value update. /// /// The reference to test equality with the current. /// The reference to update the current. /// True if the current reference was updated. public bool CompareAndSet(T expect, T update) { bool retVal = false; //NOTE: Comment out the lock to see a race lock (refLock) { if (m_Ref == expect) { m_Ref = update; retVal = true; } } return retVal; } /// /// Gets the current reference. /// /// The current reference. public T Get() { T retVal; //NOTE: Comment out the lock to see a race lock (refLock) { retVal = m_Ref; } return retVal; } } }