网上演练
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.

47 lines
1.3 KiB

// Needed for NET40
#if !NET_4_6
using System;
using System.Collections.Generic;
using LinqInternal.Threading;
namespace LinqInternal.Collections.ThreadSafe
{
internal static class ReentryGuardHelper
{
[ThreadStatic]
private static HashSet<RuntimeUniqueIdProdiver.UniqueId> _guard;
public static bool Enter(RuntimeUniqueIdProdiver.UniqueId id)
{
// Assume anything could have been set to null, start no sync operation, this could be running during DomainUnload
var guard = _guard;
if (guard == null)
{
_guard = new HashSet<RuntimeUniqueIdProdiver.UniqueId> { id };
return true;
}
if (!guard.Contains(id))
{
guard.Add(id);
return true;
}
return false;
}
public static bool IsTaken(RuntimeUniqueIdProdiver.UniqueId id)
{
return _guard.Contains(id);
}
public static void Leave(RuntimeUniqueIdProdiver.UniqueId id)
{
// Assume anything could have been set to null, start no sync operation, this could be running during DomainUnload
var guard = _guard;
if (guard != null)
{
guard.Remove(id);
}
}
}
}
#endif