11using System ;
22using System . Threading ;
3- using System . Threading . Tasks ;
43
54namespace Unity . Lifetime
65{
@@ -25,73 +24,33 @@ public abstract class SynchronizedLifetimeManager : LifetimeManager, IDisposable
2524 {
2625 #region Fields
2726
27+ private readonly object _lock = new object ( ) ;
28+
2829 /// <summary>
29- /// This field controls how long the monitor will wait to
30+ /// This field controlls how long the monitor will wait to
3031 /// enter the lock. It is <see cref="Timeout.Infinite"/> by default or number of
3132 /// milliseconds from 0 to 2147483647.
3233 /// </summary>
3334 public static int ResolveTimeout = Timeout . Infinite ;
3435
3536 #endregion
3637
37-
38- #region Constructors
39-
40- public SynchronizedLifetimeManager ( )
41- {
42- GetResult = GetValue ;
43- SetResult = SetValue ;
44-
45- GetTask = ( c ) => throw new NotImplementedException ( ) ;
46- SetTask = ( t , c ) => throw new NotImplementedException ( ) ;
47- }
48-
49- #endregion
50-
51-
52- #region ILifetimeManagerAsync
53-
54- public Func < ILifetimeContainer , object > GetResult { get ; protected set ; }
55-
56- public Action < object , ILifetimeContainer > SetResult { get ; protected set ; }
57-
58- public Func < ILifetimeContainer , Task < object > > GetTask { get ; protected set ; }
59-
60- public Func < Task < object > , ILifetimeContainer , Task < object > > SetTask { get ; protected set ; }
61-
62- #endregion
63-
64-
65- #region LifetimeManager
66-
6738 /// <inheritdoc/>
6839 public override object GetValue ( ILifetimeContainer container = null )
6940 {
70- if ( Monitor . TryEnter ( SyncRoot , ResolveTimeout ) )
41+ if ( Monitor . TryEnter ( _lock , ResolveTimeout ) )
7142 {
7243 var result = SynchronizedGetValue ( container ) ;
7344 if ( NoValue != result )
7445 {
75- Monitor . Exit ( SyncRoot ) ;
46+ Monitor . Exit ( _lock ) ;
7647 }
7748 return result ;
7849 }
7950 else
8051 throw new TimeoutException ( $ "Failed to enter a monitor") ;
8152 }
8253
83- /// <inheritdoc/>
84- public override void SetValue ( object newValue , ILifetimeContainer container = null )
85- {
86- SynchronizedSetValue ( newValue , container ) ;
87- TryExit ( ) ;
88- }
89-
90- #endregion
91-
92-
93- #region SynchronizedLifetimeManager
94-
9554 /// <summary>
9655 /// Performs the actual retrieval of a value from the backing store associated
9756 /// with this Lifetime policy.
@@ -102,6 +61,14 @@ public override void SetValue(object newValue, ILifetimeContainer container = nu
10261 /// <returns>the object desired, or null if no such object is currently stored.</returns>
10362 protected abstract object SynchronizedGetValue ( ILifetimeContainer container ) ;
10463
64+
65+ /// <inheritdoc/>
66+ public override void SetValue ( object newValue , ILifetimeContainer container = null )
67+ {
68+ SynchronizedSetValue ( newValue , container ) ;
69+ TryExit ( ) ;
70+ }
71+
10572 /// <summary>
10673 /// Performs the actual storage of the given value into backing store for retrieval later.
10774 /// </summary>
@@ -125,41 +92,34 @@ public void Recover()
12592 TryExit ( ) ;
12693 }
12794
128- #endregion
129-
130-
131- #region IDisposable
132-
133- /// <summary>
134- /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
135- /// </summary>
136- public void Dispose ( )
137- {
138- Dispose ( true ) ;
139- GC . SuppressFinalize ( this ) ;
140- }
141-
142- #endregion
143-
144-
145- #region Implementation
146-
14795 protected virtual void TryExit ( )
14896 {
14997#if ! NET40
15098 // Prevent first chance exception when abandoning a lock that has not been entered
151- if ( ! Monitor . IsEntered ( SyncRoot ) ) return ;
99+ if ( ! Monitor . IsEntered ( _lock ) ) return ;
152100#endif
153101 try
154102 {
155- Monitor . Exit ( SyncRoot ) ;
103+ Monitor . Exit ( _lock ) ;
156104 }
157105 catch ( SynchronizationLockException )
158106 {
159- // Ignore .
107+ // Noop here - we don't hold the lock and that's ok .
160108 }
161109 }
162110
111+
112+ #region IDisposable
113+
114+ /// <summary>
115+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
116+ /// </summary>
117+ public void Dispose ( )
118+ {
119+ Dispose ( true ) ;
120+ GC . SuppressFinalize ( this ) ;
121+ }
122+
163123 /// <summary>
164124 /// Standard Dispose pattern implementation.
165125 /// </summary>
0 commit comments