Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Shared / MS / Internal / CopyOnWriteList.cs / 1 / CopyOnWriteList.cs
using System;
using System.Collections;
using System.Diagnostics;
#if WINDOWS_BASE
using MS.Internal.WindowsBase;
#elif PRESENTATION_CORE
using MS.Internal.PresentationCore;
#elif PRESENTATIONFRAMEWORK
using MS.Internal.PresentationFramework;
#elif DRT
using MS.Internal.Drt;
#else
#error Attempt to use FriendAccessAllowedAttribute from an unknown assembly.
using MS.Internal.YourAssemblyName;
#endif
namespace MS.Internal
{
///
/// This is a ThreadSafe ArrayList that uses Copy On Write to support consistency.
/// - When the "List" property is requested a readonly reference to the
/// list is returned and a reference to the readonly list is cached.
/// - If the "List" is requested again, the same cached reference is returned.
/// - When the list is modified, if a readonly reference is present in the
/// cache then the list is copied before it is modified and the readonly list is
/// released from the cache.
///
[FriendAccessAllowed]
internal class CopyOnWriteList
{
public CopyOnWriteList() : this(null)
{
}
public CopyOnWriteList(object syncRoot)
{
if(syncRoot == null)
{
syncRoot = new Object();
}
_syncRoot = syncRoot;
}
///
/// Return a readonly wrapper of the list. Note: this is NOT a copy.
/// A non-null _readonlyWrapper is a "Copy on Write" flag.
/// Methods that change the list (eg. Add() and Remove()) are
/// responsible for:
/// 1) Checking _readonlyWrapper and copying the list before modifing it.
/// 2) Clearing _readonlyWrapper.
///
public ArrayList List
{
get
{
ArrayList tempList;
lock(_syncRoot)
{
if(null == _readonlyWrapper)
_readonlyWrapper = ArrayList.ReadOnly(_LiveList);
tempList = _readonlyWrapper;
}
return tempList;
}
}
///
/// Add an object to the List.
/// Returns true if successfully added.
/// Returns false if object is already on the list.
///
public virtual bool Add(object obj)
{
Debug.Assert(null!=obj, "CopyOnWriteList.Add() should not be passed null.");
lock(_syncRoot)
{
int index = Find(obj);
if(index >= 0)
return false;
return Internal_Add(obj);
}
}
///
/// Remove an object from the List.
/// Returns true if successfully removed.
/// Returns false if object was not on the list.
///
public virtual bool Remove(object obj)
{
Debug.Assert(null!=obj, "CopyOnWriteList.Remove() should not be passed null.");
lock(_syncRoot)
{
int index = Find(obj);
// If the object is not on the list then
// we are done. (return false)
if(index < 0)
return false;
return RemoveAt(index);
}
}
///
/// This allows derived classes to take the lock. This is mostly used
/// to extend Add() and Remove() etc.
///
protected object SyncRoot
{
get{ return _syncRoot; }
}
///
/// This is protected and the caller can get into real serious trouble
/// using this. Because this points at the real current list without
/// any copy on write protection. So the caller must really know what
/// they are doing.
///
protected ArrayList LiveList
{
get{ return _LiveList; }
}
///
/// Add an object to the List.
/// Without any error checks.
/// For use by derived classes that implement there own error checks.
///
protected bool Internal_Add(object obj)
{
DoCopyOnWriteCheck();
_LiveList.Add(obj);
return true;
}
///
/// Insert an object into the List at the given index.
/// Without any error checks.
/// For use by derived classes that implement there own error checks.
///
protected bool Internal_Insert(int index, object obj)
{
DoCopyOnWriteCheck();
_LiveList.Insert(index, obj);
return true;
}
///
/// Find an object on the List.
///
private int Find(object obj)
{
// syncRoot Lock MUST be held by the caller.
for(int i = 0; i < _LiveList.Count; i++)
{
if(obj == _LiveList[i])
{
return i;
}
}
return -1;
}
///
/// Remove the object at a given index from the List.
/// Returns true if successfully removed.
/// Returns false if index is outside the range of the list.
///
/// This is protected because it operates on the LiveList
///
protected bool RemoveAt(int index)
{
// syncRoot Lock MUST be held by the caller.
if(index <0 || index >= _LiveList.Count )
return false;
DoCopyOnWriteCheck();
_LiveList.RemoveAt(index);
return true;
}
private void DoCopyOnWriteCheck()
{
// syncRoot Lock MUST be held by the caller.
// If we have exposed (given out) a readonly reference to this
// version of the list, then clone a new internal copy and cut
// the old version free.
if(null != _readonlyWrapper)
{
_LiveList = (ArrayList)_LiveList.Clone();
_readonlyWrapper = null;
}
}
private object _syncRoot;
private ArrayList _LiveList = new ArrayList();
private ArrayList _readonlyWrapper;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Collections;
using System.Diagnostics;
#if WINDOWS_BASE
using MS.Internal.WindowsBase;
#elif PRESENTATION_CORE
using MS.Internal.PresentationCore;
#elif PRESENTATIONFRAMEWORK
using MS.Internal.PresentationFramework;
#elif DRT
using MS.Internal.Drt;
#else
#error Attempt to use FriendAccessAllowedAttribute from an unknown assembly.
using MS.Internal.YourAssemblyName;
#endif
namespace MS.Internal
{
///
/// This is a ThreadSafe ArrayList that uses Copy On Write to support consistency.
/// - When the "List" property is requested a readonly reference to the
/// list is returned and a reference to the readonly list is cached.
/// - If the "List" is requested again, the same cached reference is returned.
/// - When the list is modified, if a readonly reference is present in the
/// cache then the list is copied before it is modified and the readonly list is
/// released from the cache.
///
[FriendAccessAllowed]
internal class CopyOnWriteList
{
public CopyOnWriteList() : this(null)
{
}
public CopyOnWriteList(object syncRoot)
{
if(syncRoot == null)
{
syncRoot = new Object();
}
_syncRoot = syncRoot;
}
///
/// Return a readonly wrapper of the list. Note: this is NOT a copy.
/// A non-null _readonlyWrapper is a "Copy on Write" flag.
/// Methods that change the list (eg. Add() and Remove()) are
/// responsible for:
/// 1) Checking _readonlyWrapper and copying the list before modifing it.
/// 2) Clearing _readonlyWrapper.
///
public ArrayList List
{
get
{
ArrayList tempList;
lock(_syncRoot)
{
if(null == _readonlyWrapper)
_readonlyWrapper = ArrayList.ReadOnly(_LiveList);
tempList = _readonlyWrapper;
}
return tempList;
}
}
///
/// Add an object to the List.
/// Returns true if successfully added.
/// Returns false if object is already on the list.
///
public virtual bool Add(object obj)
{
Debug.Assert(null!=obj, "CopyOnWriteList.Add() should not be passed null.");
lock(_syncRoot)
{
int index = Find(obj);
if(index >= 0)
return false;
return Internal_Add(obj);
}
}
///
/// Remove an object from the List.
/// Returns true if successfully removed.
/// Returns false if object was not on the list.
///
public virtual bool Remove(object obj)
{
Debug.Assert(null!=obj, "CopyOnWriteList.Remove() should not be passed null.");
lock(_syncRoot)
{
int index = Find(obj);
// If the object is not on the list then
// we are done. (return false)
if(index < 0)
return false;
return RemoveAt(index);
}
}
///
/// This allows derived classes to take the lock. This is mostly used
/// to extend Add() and Remove() etc.
///
protected object SyncRoot
{
get{ return _syncRoot; }
}
///
/// This is protected and the caller can get into real serious trouble
/// using this. Because this points at the real current list without
/// any copy on write protection. So the caller must really know what
/// they are doing.
///
protected ArrayList LiveList
{
get{ return _LiveList; }
}
///
/// Add an object to the List.
/// Without any error checks.
/// For use by derived classes that implement there own error checks.
///
protected bool Internal_Add(object obj)
{
DoCopyOnWriteCheck();
_LiveList.Add(obj);
return true;
}
///
/// Insert an object into the List at the given index.
/// Without any error checks.
/// For use by derived classes that implement there own error checks.
///
protected bool Internal_Insert(int index, object obj)
{
DoCopyOnWriteCheck();
_LiveList.Insert(index, obj);
return true;
}
///
/// Find an object on the List.
///
private int Find(object obj)
{
// syncRoot Lock MUST be held by the caller.
for(int i = 0; i < _LiveList.Count; i++)
{
if(obj == _LiveList[i])
{
return i;
}
}
return -1;
}
///
/// Remove the object at a given index from the List.
/// Returns true if successfully removed.
/// Returns false if index is outside the range of the list.
///
/// This is protected because it operates on the LiveList
///
protected bool RemoveAt(int index)
{
// syncRoot Lock MUST be held by the caller.
if(index <0 || index >= _LiveList.Count )
return false;
DoCopyOnWriteCheck();
_LiveList.RemoveAt(index);
return true;
}
private void DoCopyOnWriteCheck()
{
// syncRoot Lock MUST be held by the caller.
// If we have exposed (given out) a readonly reference to this
// version of the list, then clone a new internal copy and cut
// the old version free.
if(null != _readonlyWrapper)
{
_LiveList = (ArrayList)_LiveList.Clone();
_readonlyWrapper = null;
}
}
private object _syncRoot;
private ArrayList _LiveList = new ArrayList();
private ArrayList _readonlyWrapper;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- FacetValues.cs
- HttpModuleAction.cs
- LocationSectionRecord.cs
- HashRepartitionEnumerator.cs
- followingquery.cs
- PassportIdentity.cs
- ValidationPropertyAttribute.cs
- MultiPropertyDescriptorGridEntry.cs
- OleDbParameterCollection.cs
- BindingWorker.cs
- IDictionary.cs
- WindowsAuthenticationEventArgs.cs
- CacheDependency.cs
- PngBitmapEncoder.cs
- TextBreakpoint.cs
- TableColumnCollection.cs
- DataGridViewColumnTypePicker.cs
- _ProxyRegBlob.cs
- MemberProjectedSlot.cs
- MILUtilities.cs
- FormConverter.cs
- PropertyTab.cs
- WindowsStatic.cs
- RuntimeArgumentHandle.cs
- ComplexTypeEmitter.cs
- NonParentingControl.cs
- BridgeDataReader.cs
- SecurityTokenProviderContainer.cs
- DesignerGenericWebPart.cs
- EventToken.cs
- ListChunk.cs
- MergeFilterQuery.cs
- DbParameterCollectionHelper.cs
- StreamReader.cs
- UshortList2.cs
- mda.cs
- UserControl.cs
- SaveFileDialog.cs
- SequenceRange.cs
- Pkcs9Attribute.cs
- pingexception.cs
- ItemContainerPattern.cs
- MultipleCopiesCollection.cs
- SqlWorkflowPersistenceService.cs
- PointLightBase.cs
- CodeDelegateCreateExpression.cs
- FileDialog.cs
- VoiceSynthesis.cs
- RectangleConverter.cs
- XmlSerializer.cs
- ConfigurationManagerHelperFactory.cs
- ConnectionManagementSection.cs
- DataGridPagingPage.cs
- IdleTimeoutMonitor.cs
- wgx_sdk_version.cs
- DataGridViewRowEventArgs.cs
- DataObjectMethodAttribute.cs
- CompositeCollection.cs
- PersonalizationDictionary.cs
- XPathScanner.cs
- QuaternionAnimation.cs
- EventsTab.cs
- AuthenticationService.cs
- IsolatedStorageFileStream.cs
- ErasingStroke.cs
- DataServiceEntityAttribute.cs
- WindowsPrincipal.cs
- DeadCharTextComposition.cs
- assertwrapper.cs
- CompressedStack.cs
- ClientBuildManagerCallback.cs
- TraceSection.cs
- XMLSchema.cs
- MultipartContentParser.cs
- Thread.cs
- SQLBytes.cs
- QueryGenerator.cs
- ProjectedSlot.cs
- InfoCardRSAPKCS1SignatureDeformatter.cs
- CacheChildrenQuery.cs
- Lease.cs
- PropertyEmitter.cs
- ISAPIWorkerRequest.cs
- XPathBuilder.cs
- PasswordTextNavigator.cs
- DefaultSettingsSection.cs
- ManipulationLogic.cs
- TypeDependencyAttribute.cs
- ZipIOCentralDirectoryFileHeader.cs
- ClientSettingsSection.cs
- _LoggingObject.cs
- PolyQuadraticBezierSegmentFigureLogic.cs
- SmuggledIUnknown.cs
- UpdateCompiler.cs
- MulticastOption.cs
- TrustLevelCollection.cs
- FormsIdentity.cs
- VectorValueSerializer.cs
- CodeGenerator.cs
- XmlSchemaComplexType.cs