Skip to content

Commit 0ef8b60

Browse files
committed
Is locking better that ToList()?
1 parent a34fe7c commit 0ef8b60

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

src/Files.Uwp/Files.Uwp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
<Compile Include="Helpers\ColorHelpers.cs" />
187187
<Compile Include="Helpers\CommonPaths.cs" />
188188
<Compile Include="Helpers\FileExtensionHelpers.cs" />
189+
<Compile Include="Helpers\ItemListDisplayHelpers\BlockingListEnumerator.cs" />
189190
<Compile Include="Helpers\LocalizedEnumHelper.cs" />
190191
<Compile Include="Helpers\ResourceHelpers.cs" />
191192
<Compile Include="Helpers\FtpHelpers.cs" />
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
namespace Files.Helpers
6+
{
7+
public class BlockingListEnumerator<T> : IEnumerator<T>
8+
{
9+
private readonly IList<T> m_Inner;
10+
private readonly object m_Lock;
11+
private int m_Pos;
12+
13+
public BlockingListEnumerator(IList<T> inner, object @lock)
14+
{
15+
m_Inner = inner;
16+
m_Lock = @lock;
17+
m_Pos = -1;
18+
}
19+
20+
public T Current
21+
{
22+
get
23+
{
24+
lock (m_Lock)
25+
{
26+
if (m_Pos < m_Inner.Count)
27+
{
28+
return m_Inner[m_Pos];
29+
}
30+
throw new IndexOutOfRangeException();
31+
}
32+
}
33+
}
34+
35+
object IEnumerator.Current => Current;
36+
37+
public void Dispose()
38+
{
39+
}
40+
41+
public bool MoveNext()
42+
{
43+
lock (m_Lock)
44+
{
45+
m_Pos++;
46+
return m_Pos < m_Inner.Count;
47+
}
48+
}
49+
50+
public void Reset()
51+
{
52+
lock (m_Lock)
53+
{
54+
m_Pos = -1;
55+
}
56+
}
57+
}
58+
}

src/Files.Uwp/Helpers/ItemListDisplayHelpers/BulkConcurrentObservableCollection.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,7 @@ public bool Remove(T item)
295295

296296
public IEnumerator<T> GetEnumerator()
297297
{
298-
lock (syncRoot)
299-
{
300-
return collection.ToList().GetEnumerator();
301-
}
298+
return new BlockingListEnumerator<T>(collection, syncRoot);
302299
}
303300

304301
IEnumerator IEnumerable.GetEnumerator()

0 commit comments

Comments
 (0)