-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcurrentObservable.cs
More file actions
325 lines (289 loc) · 9.9 KB
/
ConcurrentObservable.cs
File metadata and controls
325 lines (289 loc) · 9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading;
using System.ComponentModel;
using System.Windows;
using System.Collections.Concurrent;
using System.Diagnostics;
namespace TPSNet.Desktop.Modules.TPS.ActiveBlocks.Collections
{
/// <summary>
/// Multithreaded observable generic collection
/// </summary>
/// <typeparam name="T"></typeparam>
public class ConcurrentObservable<T> : ICollection<T>, INotifyCollectionChanged, INotifyPropertyChanged where T : class
{
public int Count => mCollection.Count;
public bool IsReadOnly => throw new NotImplementedException();
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event PropertyChangedEventHandler PropertyChanged;
private List<T> mCollection;
private int mUpdatePending = 0;
private readonly object mEnumLock = new object();
private readonly object mRaiseLock = new object();
private readonly object mExclusiveLock = new object();
public ConcurrentObservable()
{
mCollection = new List<T>();
}
public ConcurrentObservable(IEnumerable<T> items)
{
if (items != null)
mCollection = new List<T>(items);
else
mCollection = new List<T>();
}
public void Add(T item)
{
lock (mExclusiveLock)
{
lock (mRaiseLock)
{
lock (mEnumLock)
mCollection.Add(item);
}
RaiseCollectionChanged(NotifyCollectionChangedAction.Add, item);
}
}
public void Clear()
{
lock (mExclusiveLock)
{
lock (mRaiseLock)
{
lock (mEnumLock)
mCollection.Clear();
}
RaiseCollectionChanged(NotifyCollectionChangedAction.Reset);
}
}
public bool Remove(T item)
{
int idx = -1;
lock (mExclusiveLock)
{
lock (mRaiseLock)
{
lock (mEnumLock)
{
idx = mCollection.IndexOf(item);
if (idx != -1)
mCollection.RemoveAt(idx);
}
}
if (idx != -1)
RaiseCollectionChanged(NotifyCollectionChangedAction.Remove, item, idx);
}
return idx != -1;
}
public bool Contains(T item)
{
lock (mEnumLock)
{
return mCollection.Contains(item);
}
}
public void CopyTo(T[] array, int arrayIndex)
{
lock (mEnumLock)
{
if (array.Length >= mCollection.Count)
mCollection.CopyTo(array, arrayIndex);
else
mCollection.CopyTo(0, array, arrayIndex, array.Length - arrayIndex);
}
}
public IEnumerator<T> GetEnumerator()
{
return new SafeEnumerator<T>(mCollection, mEnumLock);
}
/// <summary>
/// Removes list of items, fires Reset event
/// </summary>
/// <param name="items"></param>
public void RemoveRange(IList<T> items)
{
lock (mExclusiveLock)
{
lock (mEnumLock)
{
lock (mRaiseLock)
{
foreach (var i in items)
{
mCollection.Remove(i);
}
}
}
RaiseCollectionChanged(NotifyCollectionChangedAction.Reset);
}
}
/// <summary>
/// Adds list of items, fires Reset event
/// </summary>
/// <param name="items"></param>
public void AddRange(IList<T> items)
{
lock (mExclusiveLock)
{
lock (mEnumLock)
{
lock (mRaiseLock)
{
foreach (var i in items)
{
mCollection.Add(i);
}
}
}
RaiseCollectionChanged(NotifyCollectionChangedAction.Reset);
}
}
/// <summary>
/// Use isExclusive to block all other threads during update
/// Don't use begin/endupdate if you need remove event
/// </summary>
/// <param name="isExclusive"></param>
public void BeginUpdate(bool isExclusive = true)
{
if (isExclusive)
Monitor.Enter(mExclusiveLock);
Interlocked.Add(ref mUpdatePending, 1);
}
/// <summary>
/// Perform RaiseCollectionChanged
/// </summary>
/// <param name="onlyAdd">true if you certain, that no items where removed during update</param>
/// <param name="newItems"></param>
public void EndUpdate(bool onlyAdd = false, object newItems = null)
{
if (mUpdatePending > 0)
Interlocked.Add(ref mUpdatePending, -1);
if (onlyAdd)
RaiseCollectionChanged(NotifyCollectionChangedAction.Add, newItems);
else
RaiseCollectionChanged(NotifyCollectionChangedAction.Reset);
if (Monitor.IsEntered(mExclusiveLock))
Monitor.Exit(mExclusiveLock);
}
public object GetExclusiveLock()
{
return mExclusiveLock;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
void RaiseCollectionChanged(NotifyCollectionChangedAction action, object changedItems = null, int removedIndex = -1)
{
if (mUpdatePending == 0)
{
System.Windows.Application.Current?.Dispatcher.Invoke(() => InvokeCollectionChanged(action, changedItems, removedIndex));
//InvokeCollectionChanged(action, changedItems, removedIndex);
}
}
void InvokeCollectionChanged(NotifyCollectionChangedAction action, object changedItems = null, int removedIndex = -1)
{
lock (mRaiseLock)
{
switch (action)
{
case NotifyCollectionChangedAction.Reset:
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action));
break;
case NotifyCollectionChangedAction.Add:
if (changedItems is IList items)
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action, items));
else
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action, changedItems));
break;
case NotifyCollectionChangedAction.Remove:
if (changedItems is IList)
throw new NotImplementedException();
else
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action, changedItems, removedIndex));
break;
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Count"));
}
}
public class SafeEnumerator<G> : IEnumerator<G>
{
private readonly IEnumerator<G> mInner;
private readonly object mLock;
//SmartLocker SL;
public SafeEnumerator(ICollection<G> inner, object enumlock)
{
mLock = enumlock;
//SL = new SmartLocker();
//SL.Enter(mLock);
Monitor.Enter(mLock);
mInner = inner.GetEnumerator();
}
public void Dispose()
{
mInner.Dispose();
//SL.Dispose();
Monitor.Exit(mLock);
}
public bool MoveNext()
{
return mInner != null && mInner.MoveNext();
}
public void Reset()
{
mInner.Reset();
}
public G Current
{
get { return mInner.Current; }
}
object IEnumerator.Current
{
get { return Current; }
}
}
class SmartLocker : IDisposable
{
public static string HoldingTrace;
object Locker;
public void Enter(object locker)
{
bool locked = false;
int timeout = 0;
Locker = locker;
while (!locked)
{
locked = Monitor.TryEnter(locker, 5000);
if (!locked)
{
timeout += 3000;
}
}
HoldingTrace = Thread.CurrentThread.ManagedThreadId + " - " + Environment.StackTrace;
}
public void Exit()
{
if (Monitor.IsEntered(Locker))
Monitor.Exit(Locker);
HoldingTrace = "";
}
public void Dispose()
{
Exit();
}
}
}
static class ObservableExtensions
{
public static List<T> ToList<T>(this ConcurrentObservable<T> collection) where T : class
{
lock (collection.GetExclusiveLock())
{
return collection.ToList();
}
}
}
}