wasSharp – Blame information for rev 44
?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 | |||
56 | public int Count |
||
57 | { |
||
58 | get |
||
59 | { |
||
60 | _lock.EnterReadLock(); |
||
44 | office | 61 | try |
62 | { |
||
63 | return _list.Count; |
||
64 | } |
||
65 | finally |
||
66 | { |
||
67 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
68 | } |
||
69 | |||
40 | office | 70 | } |
71 | } |
||
72 | |||
73 | public bool IsReadOnly |
||
74 | { |
||
75 | get |
||
76 | { |
||
44 | office | 77 | _lock.EnterReadLock(); |
40 | office | 78 | try |
79 | { |
||
80 | return ((IList<T>)_list).IsReadOnly; |
||
81 | } |
||
82 | finally |
||
83 | { |
||
84 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | public T this[int index] |
||
90 | { |
||
91 | get |
||
92 | { |
||
44 | office | 93 | _lock.EnterReadLock(); |
40 | office | 94 | try |
95 | { |
||
96 | return _list[index]; |
||
97 | } |
||
98 | finally |
||
99 | { |
||
100 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | set |
||
105 | { |
||
44 | office | 106 | _lock.EnterWriteLock(); |
40 | office | 107 | try |
108 | { |
||
109 | _list[index] = value; |
||
110 | } |
||
111 | finally |
||
112 | { |
||
113 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | |||
118 | public int IndexOf(T item) |
||
119 | { |
||
44 | office | 120 | _lock.EnterReadLock(); |
40 | office | 121 | try |
122 | { |
||
123 | return _list.IndexOf(item); |
||
124 | } |
||
125 | finally |
||
126 | { |
||
127 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | public void Insert(int index, T item) |
||
132 | { |
||
44 | office | 133 | _lock.EnterWriteLock(); |
40 | office | 134 | try |
135 | { |
||
136 | _list.Insert(index, item); |
||
137 | } |
||
138 | finally |
||
139 | { |
||
140 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | public void RemoveAt(int index) |
||
145 | { |
||
44 | office | 146 | _lock.EnterWriteLock(); |
40 | office | 147 | try |
148 | { |
||
149 | _list.RemoveAt(index); |
||
150 | } |
||
151 | finally |
||
152 | { |
||
153 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | public void Add(T item) |
||
158 | { |
||
44 | office | 159 | _lock.EnterWriteLock(); |
40 | office | 160 | try |
161 | { |
||
162 | _list.Add(item); |
||
163 | } |
||
164 | finally |
||
165 | { |
||
166 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | public void Clear() |
||
171 | { |
||
44 | office | 172 | _lock.EnterWriteLock(); |
40 | office | 173 | try |
174 | { |
||
175 | _list.Clear(); |
||
176 | } |
||
177 | finally |
||
178 | { |
||
179 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | public bool Contains(T item) |
||
184 | { |
||
44 | office | 185 | _lock.EnterReadLock(); |
40 | office | 186 | try |
187 | { |
||
188 | return _list.Contains(item); |
||
189 | } |
||
190 | finally |
||
191 | { |
||
192 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
193 | } |
||
194 | } |
||
195 | |||
196 | public void CopyTo(T[] array, int arrayIndex) |
||
197 | { |
||
44 | office | 198 | _lock.EnterReadLock(); |
40 | office | 199 | try |
200 | { |
||
201 | _list.CopyTo(array, arrayIndex); |
||
202 | } |
||
203 | finally |
||
204 | { |
||
205 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | public bool Remove(T item) |
||
210 | { |
||
44 | office | 211 | _lock.EnterWriteLock(); |
40 | office | 212 | try |
213 | { |
||
214 | return _list.Remove(item); |
||
215 | } |
||
216 | finally |
||
217 | { |
||
218 | if (_lock.IsWriteLockHeld) _lock.ExitWriteLock(); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | public IEnumerator<T> GetEnumerator() |
||
223 | { |
||
44 | office | 224 | _lock.EnterReadLock(); |
40 | office | 225 | try |
226 | { |
||
44 | office | 227 | using (var enumerator = _list.GetEnumerator()) |
228 | { |
||
229 | while (enumerator.MoveNext()) |
||
230 | yield return enumerator.Current; |
||
231 | } |
||
40 | office | 232 | } |
233 | finally |
||
234 | { |
||
235 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | IEnumerator IEnumerable.GetEnumerator() |
||
240 | { |
||
44 | office | 241 | _lock.EnterReadLock(); |
40 | office | 242 | try |
243 | { |
||
44 | office | 244 | using (var enumerator = _list.GetEnumerator()) |
245 | { |
||
246 | while (enumerator.MoveNext()) |
||
247 | yield return enumerator.Current; |
||
248 | } |
||
40 | office | 249 | } |
250 | finally |
||
251 | { |
||
252 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | } |