annotate Implab/Components/LazyAndWeak.cs @ 281:e0916ddc9950 v3 tip

code cleanup and refactoring
author cin
date Fri, 01 Jun 2018 21:35:24 +0300
parents 3a6e18c432be
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
178
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
1 using System;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
2 using System.Threading;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
3
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
4 namespace Implab.Components {
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
5 /// <summary>
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
6 /// Creates an instace on-demand and allows it to be garbage collected.
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
7 /// </summary>
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
8 /// <remarks>
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
9 /// Usefull when dealing with memory-intensive objects which are frequently used.
182
76e8f2ba12b8 pretty print DFA, the minimization is still buggy
cin
parents: 180
diff changeset
10 /// This class is similar to <see cref="ObjectPool{T}"/> except it is a singleton.
264
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 182
diff changeset
11 /// This class can't be used to hold diposable objects.
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
12 /// </remarks>
178
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
13 public class LazyAndWeak<T> where T : class {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
14
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
15 readonly Func<T> m_factory;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
16 readonly object m_lock;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
17 WeakReference m_reference;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
18
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
19
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
20 public LazyAndWeak(Func<T> factory, bool useLock) {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
21 Safe.ArgumentNotNull(factory, "factory");
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
22 m_factory = factory;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
23 m_lock = useLock ? new object() : null;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
24 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
25
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
26 public LazyAndWeak(Func<T> factory) : this(factory, false) {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
27 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
28
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
29 public T Value {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
30 get {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
31 while (true) {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
32 var weak = m_reference;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
33 T value;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
34 if (weak != null) {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
35 value = weak.Target as T;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
36 if (value != null)
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
37 return value;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
38 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
39
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
40 if (m_lock == null) {
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
41 value = m_factory();
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
42
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
43 if (Interlocked.CompareExchange(ref m_reference, new WeakReference(value), weak) == weak)
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
44 return value;
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
45 } else {
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
46 lock (m_lock) {
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
47 // double check
182
76e8f2ba12b8 pretty print DFA, the minimization is still buggy
cin
parents: 180
diff changeset
48 weak = m_reference;
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
49 if (weak != null) {
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
50 value = weak.Target as T;
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
51 if (value != null)
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
52 return value;
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
53 }
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
54 // we are safe to write
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
55 value = m_factory();
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
56 m_reference = new WeakReference(value);
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
57 return value;
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
58 }
178
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
59 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
60 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
61 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
62 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
63 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
64 }
d5c5db0335ee working on JSON parser
cin
parents:
diff changeset
65