mirror of https://github.com/icsharpcode/ILSpy.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.3 KiB
52 lines
1.3 KiB
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) |
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) |
|
|
|
using System; |
|
using System.Collections.Generic; |
|
|
|
namespace ICSharpCode.SharpDevelop.Dom |
|
{ |
|
/// <summary> |
|
/// This class is used to prevent stack overflows by representing a 'busy' flag |
|
/// that prevents reentrance when another call is running. |
|
/// However, using a simple 'bool busy' is not thread-safe, so we use a |
|
/// thread-static BusyManager. |
|
/// </summary> |
|
sealed class BusyManager |
|
{ |
|
public struct BusyLock : IDisposable |
|
{ |
|
public static readonly BusyLock Failed = new BusyLock(null); |
|
|
|
readonly BusyManager manager; |
|
|
|
public BusyLock(BusyManager manager) |
|
{ |
|
this.manager = manager; |
|
} |
|
|
|
public bool Success { |
|
get { return manager != null; } |
|
} |
|
|
|
public void Dispose() |
|
{ |
|
if (manager != null) { |
|
manager.activeObjects.RemoveAt(manager.activeObjects.Count - 1); |
|
} |
|
} |
|
} |
|
|
|
readonly List<object> activeObjects = new List<object>(); |
|
|
|
public BusyLock Enter(object obj) |
|
{ |
|
for (int i = 0; i < activeObjects.Count; i++) { |
|
if (activeObjects[i] == obj) |
|
return BusyLock.Failed; |
|
} |
|
activeObjects.Add(obj); |
|
return new BusyLock(this); |
|
} |
|
} |
|
}
|
|
|