Skip to content

Commit 1fd3717

Browse files
authored
Remove syncroot fields from collections (dotnet#34198)
* Syncroot * Fix typo in build-native * Specialized * Remove noise * Revert non generic SortedList * Test fixups * CLR test fixes * Revert "CLR test fixes" This reverts commit 8db135c. * Disable syncroot tests for NETFX * Update QueueTests.cs * Test fixes * Typo
1 parent a7b5f8c commit 1fd3717

File tree

27 files changed

+44
-250
lines changed

27 files changed

+44
-250
lines changed

build.cmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
@echo off
22
powershell -ExecutionPolicy ByPass -NoProfile %~dp0eng\build.ps1 %*
3-
echo Build.cmd ErrorLevel=%ERRORLEVEL%
43
exit /b %ERRORLEVEL%

src/Common/tests/System/Collections/ICollection.NonGeneric.Tests.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,6 @@ protected virtual ICollection NonGenericICollectionFactory(int count)
8383
/// </summary>
8484
protected virtual bool ICollection_NonGeneric_HasNullSyncRoot => false;
8585

86-
/// <summary>
87-
/// Used for the ICollection_NonGeneric_SyncRootType_MatchesExcepted test. Most SyncRoots are created
88-
/// using System.Threading.Interlocked.CompareExchange(ref _syncRoot, new Object(), null)
89-
/// so we should test that the SyncRoot is the type we expect.
90-
/// </summary>
91-
protected virtual Type ICollection_NonGeneric_SyncRootType => typeof(object);
92-
9386
/// <summary>
9487
/// Used for the ICollection_NonGeneric_CopyTo_IndexLargerThanArrayCount_ThrowsArgumentException tests. Some
9588
/// implementations throw a different exception type (e.g. ArgumentOutOfRangeException).
@@ -147,22 +140,6 @@ public void ICollection_NonGeneric_SyncRoot(int count)
147140
{
148141
Assert.Equal(ICollection_NonGeneric_HasNullSyncRoot, collection.SyncRoot == null);
149142
Assert.Same(collection.SyncRoot, collection.SyncRoot);
150-
151-
if (!ICollection_NonGeneric_HasNullSyncRoot)
152-
{
153-
Assert.IsType(ICollection_NonGeneric_SyncRootType, collection.SyncRoot);
154-
155-
if (ICollection_NonGeneric_SyncRootType == collection.GetType())
156-
{
157-
// If we expect the SyncRoot to be the same type as the collection,
158-
// the SyncRoot should be the same as the collection (e.g. HybridDictionary)
159-
Assert.Same(collection, collection.SyncRoot);
160-
}
161-
else
162-
{
163-
Assert.NotSame(collection, collection.SyncRoot);
164-
}
165-
}
166143
}
167144
else
168145
{

src/Native/build-native.cmd

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ call "%_VSCOMNTOOLS%\VsDevCmd.bat"
6868
:RunVCVars
6969
if "%VisualStudioVersion%"=="16.0" (
7070
goto :VS2019
71-
)
72-
else if "%VisualStudioVersion%"=="15.0" (
71+
) else if "%VisualStudioVersion%"=="15.0" (
7372
goto :VS2017
7473
) else if "%VisualStudioVersion%"=="14.0" (
7574
goto :VS2015

src/System.Collections.NonGeneric/src/System/Collections/Queue.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public class Queue : ICollection, ICloneable
2929
private int _size; // Number of elements. Do not rename (binary serialization)
3030
private int _growFactor; // 100 == 1.0, 130 == 1.3, 200 == 2.0. Do not rename (binary serialization)
3131
private int _version; // Do not rename (binary serialization)
32-
[NonSerialized]
33-
private object _syncRoot;
3432

3533
private const int _MinimumGrow = 4;
3634
private const int _ShrinkThreshold = 32;
@@ -107,17 +105,7 @@ public virtual bool IsSynchronized
107105
get { return false; }
108106
}
109107

110-
public virtual object SyncRoot
111-
{
112-
get
113-
{
114-
if (_syncRoot == null)
115-
{
116-
System.Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null);
117-
}
118-
return _syncRoot;
119-
}
120-
}
108+
public virtual object SyncRoot => this;
121109

122110
// Removes all Objects from the queue.
123111
public virtual void Clear()

src/System.Collections.NonGeneric/src/System/Collections/SortedList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class SortedList : IDictionary, ICloneable
7171
private KeyList keyList; // Do not rename (binary serialization)
7272
private ValueList valueList; // Do not rename (binary serialization)
7373
[NonSerialized]
74-
private object _syncRoot;
74+
private object _syncRoot; // Serialized indirectly in SyncSortedList
7575

7676
private const int _defaultCapacity = 16;
7777

src/System.Collections.NonGeneric/src/System/Collections/Stack.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public class Stack : ICollection, ICloneable
2727
private object[] _array; // Storage for stack elements. Do not rename (binary serialization)
2828
private int _size; // Number of items in the stack. Do not rename (binary serialization)
2929
private int _version; // Used to keep enumerator in sync w/ collection. Do not rename (binary serialization)
30-
[NonSerialized]
31-
private object _syncRoot;
3230

3331
private const int _defaultCapacity = 10;
3432

@@ -79,17 +77,7 @@ public virtual bool IsSynchronized
7977
get { return false; }
8078
}
8179

82-
public virtual object SyncRoot
83-
{
84-
get
85-
{
86-
if (_syncRoot == null)
87-
{
88-
System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new object(), null);
89-
}
90-
return _syncRoot;
91-
}
92-
}
80+
public virtual object SyncRoot => this;
9381

9482
// Removes all Objects from the Stack.
9583
public virtual void Clear()

src/System.Collections.NonGeneric/tests/ArrayListTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,7 @@ public DerivedArrayList(ICollection c) : base(c) { }
25482548
}
25492549
}
25502550

2551+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] // Changed behavior
25512552
public class ArrayList_SyncRootTests
25522553
{
25532554
private ArrayList _arrDaughter;
@@ -2576,12 +2577,12 @@ public void GetSyncRoot()
25762577
_arrGrandDaughter = ArrayList.Synchronized(arrMother2);
25772578
_arrDaughter = ArrayList.Synchronized(arrMother2);
25782579

2579-
Assert.False(arrMother2.SyncRoot is ArrayList);
2580-
Assert.False(arrSon1.SyncRoot is ArrayList);
2581-
Assert.False(arrSon2.SyncRoot is ArrayList);
2582-
Assert.False(_arrDaughter.SyncRoot is ArrayList);
2580+
Assert.True(arrMother2.SyncRoot is ArrayList);
2581+
Assert.True(arrSon1.SyncRoot is ArrayList);
2582+
Assert.True(arrSon2.SyncRoot is ArrayList);
2583+
Assert.True(_arrDaughter.SyncRoot is ArrayList);
25832584
Assert.Equal(arrSon1.SyncRoot, arrMother2.SyncRoot);
2584-
Assert.False(_arrGrandDaughter.SyncRoot is ArrayList);
2585+
Assert.True(_arrGrandDaughter.SyncRoot is ArrayList);
25852586

25862587
arrMother2 = new ArrayList();
25872588
for (int i = 0; i < NumberOfElements; i++)

src/System.Collections.NonGeneric/tests/CollectionBaseTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ public static void SyncRoot()
299299
// SyncRoot should be the reference to the underlying collection, not to MyCollection
300300
var collBase = new MyCollection();
301301
object syncRoot = collBase.SyncRoot;
302-
Assert.NotEqual(syncRoot, collBase);
303-
Assert.Equal(collBase.SyncRoot, collBase.SyncRoot);
302+
Assert.NotNull(syncRoot);
303+
Assert.Same(collBase.SyncRoot, collBase.SyncRoot);
304304
}
305305

306306
[Fact]

src/System.Collections.NonGeneric/tests/HashtableTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,7 @@ private void RemoveElements(string strName)
11941194
}
11951195
}
11961196

1197+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] // Changed behavior
11971198
public class Hashtable_SyncRootTests
11981199
{
11991200
private Hashtable _hashDaughter;

src/System.Collections.NonGeneric/tests/QueueTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ public Foo(int intValue)
834834
}
835835
}
836836

837+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] // Changed behavior
837838
public class Queue_SyncRootTests
838839
{
839840
private const int NumberOfElements = 1000;
@@ -850,7 +851,7 @@ public void SyncRoot()
850851
{
851852
queueMother.Enqueue(i);
852853
}
853-
Assert.Equal(queueMother.SyncRoot.GetType(), typeof(object));
854+
Assert.IsType<Queue>(queueMother.SyncRoot);
854855

855856
var queueSon = Queue.Synchronized(queueMother);
856857
_queueGrandDaughter = Queue.Synchronized(queueSon);

0 commit comments

Comments
 (0)