comparison Implab/Components/LazyAndWeak.cs @ 182:76e8f2ba12b8 ref20160224

pretty print DFA, the minimization is still buggy
author cin
date Thu, 24 Mar 2016 18:52:10 +0300
parents c32688129f14
children 3a6e18c432be
comparison
equal deleted inserted replaced
181:b2b6a6640aa3 182:76e8f2ba12b8
5 /// <summary> 5 /// <summary>
6 /// Creates an instace on-demand and allows it to be garbage collected. 6 /// Creates an instace on-demand and allows it to be garbage collected.
7 /// </summary> 7 /// </summary>
8 /// <remarks> 8 /// <remarks>
9 /// Usefull when dealing with memory-intensive objects which are frequently used. 9 /// Usefull when dealing with memory-intensive objects which are frequently used.
10 /// This class is similar to <see cref="ObjectPool{T}"/> except is a singleton. 10 /// This class is similar to <see cref="ObjectPool{T}"/> except it is a singleton.
11 /// </remarks> 11 /// </remarks>
12 public class LazyAndWeak<T> where T : class { 12 public class LazyAndWeak<T> where T : class {
13 13
14 readonly Func<T> m_factory; 14 readonly Func<T> m_factory;
15 readonly object m_lock; 15 readonly object m_lock;
42 if (Interlocked.CompareExchange(ref m_reference, new WeakReference(value), weak) == weak) 42 if (Interlocked.CompareExchange(ref m_reference, new WeakReference(value), weak) == weak)
43 return value; 43 return value;
44 } else { 44 } else {
45 lock (m_lock) { 45 lock (m_lock) {
46 // double check 46 // double check
47 weak = m_reference;
47 if (weak != null) { 48 if (weak != null) {
48 value = weak.Target as T; 49 value = weak.Target as T;
49 if (value != null) 50 if (value != null)
50 return value; 51 return value;
51 } 52 }