Skip to content

Commit

Permalink
Fixes #101
Browse files Browse the repository at this point in the history
  • Loading branch information
ENikS committed Apr 2, 2019
1 parent c975f66 commit ddb6bff
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/Lifetime/Abstracts/SynchronizedLifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,31 @@ public abstract class SynchronizedLifetimeManager : LifetimeManager, IDisposable
{
#region Fields

private readonly object _lockObj = new object();
private readonly object _lock = new object();

/// <summary>
/// This field controlls how long the monitor will wait to
/// enter the lock. It is <see cref="Timeout.Infinite"/> by default or number of
/// milliseconds from 0 to 2147483647.
/// </summary>
public static int ResolveTimeout = Timeout.Infinite;

#endregion

/// <inheritdoc/>
public override object GetValue(ILifetimeContainer container = null)
{
Monitor.Enter(_lockObj);
var result = SynchronizedGetValue(container);
if (NoValue != result)
if (Monitor.TryEnter(_lock, ResolveTimeout))
{
Monitor.Exit(_lockObj);
var result = SynchronizedGetValue(container);
if (NoValue != result)
{
Monitor.Exit(_lock);
}
return result;
}
return result;
else
throw new TimeoutException($"Failed to enter a monitor");
}

/// <summary>
Expand Down Expand Up @@ -85,11 +96,11 @@ protected virtual void TryExit()
{
#if !NET40
// Prevent first chance exception when abandoning a lock that has not been entered
if (!Monitor.IsEntered(_lockObj)) return;
if (!Monitor.IsEntered(_lock)) return;
#endif
try
{
Monitor.Exit(_lockObj);
Monitor.Exit(_lock);
}
catch (SynchronizationLockException)
{
Expand Down

0 comments on commit ddb6bff

Please sign in to comment.