/// <summary> /// </summary> /// <param name="list"></param> /// <param name="item"></param> private void AddInternal(ThreadLocalList list, T item) { bool lockTaken = false; try { #pragma warning disable 0420 Interlocked.Exchange(ref list._currentOp, (int)ListOperation.Add); #pragma warning restore 0420 //Synchronization cases: // if the list count is less than two to avoid conflict with any stealing thread // if _needSync is set, this means there is a thread that needs to freeze the bag if (list.Count < 2 || _needSync) { // reset it back to zero to avoid deadlock with stealing thread list._currentOp = (int)ListOperation.None; Monitor.Enter(list, ref lockTaken); } list.Add(item, lockTaken); } finally { list._currentOp = (int)ListOperation.None; if (lockTaken) { Monitor.Exit(list); } } }
private void Initialize(IEnumerable <T> collection) { _locals = new ThreadLocal <ThreadLocalList>(); if (collection != null) { ThreadLocalList list = GetThreadList(true); foreach (T item in collection) { list.Add(item, false); } } }
private void OnDeserialized(StreamingContext context) { m_locals = new ThreadLocal <ThreadLocalList>(); ThreadLocalList list = GetThreadList(true); foreach (T item in m_serializationArray) { list.Add(item, false); } m_headList = list; m_tailList = list; m_serializationArray = null; }
private void AddInternal(ThreadLocalList list, T item) { bool lockTaken = false; try { Interlocked.Exchange(ref list._currentOp, (int)ListOperation.Add); if (list.Count < 2 || _needSync) { list._currentOp = (int)ListOperation.None; Monitor.Enter(list, ref lockTaken); } list.Add(item, lockTaken); } finally { list._currentOp = (int)ListOperation.None; if (lockTaken) { Monitor.Exit(list); } } }
private void AddInternal(ThreadLocalList <T> list, T item) { bool lockTaken = false; try { Interlocked.Exchange(ref list.m_currentOp, 1); if ((list.Count < 2) || this.m_needSync) { list.m_currentOp = 0; Monitor.Enter(list, ref lockTaken); } list.Add(item, lockTaken); } finally { list.m_currentOp = 0; if (lockTaken) { Monitor.Exit(list); } } }