wasSharp – Blame information for rev 54
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
40 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System.Collections; |
||
8 | using System.Collections.Generic; |
||
9 | using System.Threading; |
||
10 | |||
11 | namespace wasSharp.Collections.Specialized |
||
12 | { |
||
13 | public static class ConcurrentListExtensions |
||
14 | { |
||
15 | public static ConcurrentList<T> ToConcurrentList<T>(this IEnumerable<T> enumerable) |
||
16 | { |
||
17 | return new ConcurrentList<T>(enumerable); |
||
18 | } |
||
19 | } |
||
20 | |||
21 | public class ConcurrentList<T> : IList<T> |
||
22 | { |
||
23 | private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
||
44 | office | 24 | private readonly List<T> _list; |
40 | office | 25 | |
26 | public ConcurrentList(IEnumerable<T> v) |
||
27 | { |
||
44 | office | 28 | _lock.EnterWriteLock(); |
40 | office | 29 | try |
30 | { |
||
31 | _list = new List<T>(); |
||
32 | foreach (var i in v) |
||
33 | { |
||
34 | _list.Add(i); |
||
35 | } |
||
36 | } |
||
37 | finally |
||
38 | { |
||
39 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | public ConcurrentList() |
||
44 | { |
||
44 | office | 45 | _lock.EnterWriteLock(); |
40 | office | 46 | try |
47 | { |
||
48 | _list = new List<T>(); |
||
49 | } |
||
50 | finally |
||
51 | { |
||
52 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
53 | } |
||
54 | } |
||
55 | |||
54 | office | 56 | public bool IsReadOnly |
40 | office | 57 | { |
58 | get |
||
59 | { |
||
60 | _lock.EnterReadLock(); |
||
44 | office | 61 | try |
62 | { |
||
54 | office | 63 | return ((IList<T>)_list).IsReadOnly; |
44 | office | 64 | } |
65 | finally |
||
66 | { |
||
67 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
68 | } |
||
40 | office | 69 | } |
54 | office | 70 | } |
71 | |||
72 | int ICollection<T>.Count |
||
40 | office | 73 | { |
54 | office | 74 | get |
40 | office | 75 | { |
54 | office | 76 | _lock.EnterReadLock(); |
77 | try |
||
78 | { |
||
79 | return _list.Count; |
||
80 | } |
||
81 | finally |
||
82 | { |
||
83 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
40 | office | 84 | } |
85 | } |
||
54 | office | 86 | } |
87 | |||
40 | office | 88 | public T this[int index] |
89 | { |
||
90 | get |
||
91 | { |
||
44 | office | 92 | _lock.EnterReadLock(); |
40 | office | 93 | try |
94 | { |
||
95 | return _list[index]; |
||
96 | } |
||
97 | finally |
||
98 | { |
||
99 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | set |
||
104 | { |
||
44 | office | 105 | _lock.EnterWriteLock(); |
40 | office | 106 | try |
107 | { |
||
108 | _list[index] = value; |
||
109 | } |
||
110 | finally |
||
111 | { |
||
112 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | public int IndexOf(T item) |
||
118 | { |
||
44 | office | 119 | _lock.EnterReadLock(); |
40 | office | 120 | try |
121 | { |
||
122 | return _list.IndexOf(item); |
||
123 | } |
||
124 | finally |
||
125 | { |
||
126 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public void Insert(int index, T item) |
||
131 | { |
||
44 | office | 132 | _lock.EnterWriteLock(); |
40 | office | 133 | try |
134 | { |
||
135 | _list.Insert(index, item); |
||
136 | } |
||
137 | finally |
||
138 | { |
||
139 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | public void RemoveAt(int index) |
||
144 | { |
||
44 | office | 145 | _lock.EnterWriteLock(); |
40 | office | 146 | try |
147 | { |
||
148 | _list.RemoveAt(index); |
||
149 | } |
||
150 | finally |
||
151 | { |
||
152 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | public void Add(T item) |
||
157 | { |
||
44 | office | 158 | _lock.EnterWriteLock(); |
40 | office | 159 | try |
160 | { |
||
161 | _list.Add(item); |
||
162 | } |
||
163 | finally |
||
164 | { |
||
165 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | public void Clear() |
||
170 | { |
||
44 | office | 171 | _lock.EnterWriteLock(); |
40 | office | 172 | try |
173 | { |
||
174 | _list.Clear(); |
||
175 | } |
||
176 | finally |
||
177 | { |
||
178 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | public bool Contains(T item) |
||
183 | { |
||
44 | office | 184 | _lock.EnterReadLock(); |
40 | office | 185 | try |
186 | { |
||
187 | return _list.Contains(item); |
||
188 | } |
||
189 | finally |
||
190 | { |
||
191 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | public void CopyTo(T[] array, int arrayIndex) |
||
196 | { |
||
44 | office | 197 | _lock.EnterReadLock(); |
40 | office | 198 | try |
199 | { |
||
200 | _list.CopyTo(array, arrayIndex); |
||
201 | } |
||
202 | finally |
||
203 | { |
||
204 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | public bool Remove(T item) |
||
209 | { |
||
44 | office | 210 | _lock.EnterWriteLock(); |
40 | office | 211 | try |
212 | { |
||
213 | return _list.Remove(item); |
||
214 | } |
||
215 | finally |
||
216 | { |
||
217 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | public IEnumerator<T> GetEnumerator() |
||
222 | { |
||
44 | office | 223 | _lock.EnterReadLock(); |
40 | office | 224 | try |
225 | { |
||
44 | office | 226 | using (var enumerator = _list.GetEnumerator()) |
227 | { |
||
228 | while (enumerator.MoveNext()) |
||
229 | yield return enumerator.Current; |
||
230 | } |
||
40 | office | 231 | } |
232 | finally |
||
233 | { |
||
234 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | IEnumerator IEnumerable.GetEnumerator() |
||
239 | { |
||
44 | office | 240 | _lock.EnterReadLock(); |
40 | office | 241 | try |
242 | { |
||
44 | office | 243 | using (var enumerator = _list.GetEnumerator()) |
244 | { |
||
245 | while (enumerator.MoveNext()) |
||
246 | yield return enumerator.Current; |
||
247 | } |
||
40 | office | 248 | } |
249 | finally |
||
250 | { |
||
251 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | } |