1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System.Collections.Generic;
9 
10 namespace Antmicro.Renode.Utilities.Collections
11 {
12     public class FastReadConcurrentCollection<T>
13     {
FastReadConcurrentCollection()14         public FastReadConcurrentCollection()
15         {
16             Clear();
17         }
18 
Add(T item)19         public void Add(T item)
20         {
21             lock(locker)
22             {
23                 var copy = new List<T>(Items);
24                 copy.Add(item);
25                 Items = copy.ToArray();
26             }
27         }
28 
Remove(T item)29         public void Remove(T item)
30         {
31             lock(locker)
32             {
33                 var copy = new List<T>(Items);
34                 copy.Remove(item);
35                 Items = copy.ToArray();
36             }
37         }
38 
Clear()39         public void Clear()
40         {
41             lock(locker)
42             {
43                 Items = new T[0];
44             }
45         }
46 
47         public T[] Items { get; private set; }
48 
49         private readonly object locker = new object();
50     }
51 }
52 
53