public static bool TryRelease(ref CacheAwareElement bucket, T value) { if (null == Interlocked.CompareExchange(ref bucket.Value1, value, null)) { goto Done; } if (null == Interlocked.CompareExchange(ref bucket.Value2, value, null)) { goto Done; } if (null == Interlocked.CompareExchange(ref bucket.Value3, value, null)) { goto Done; } if (null == Interlocked.CompareExchange(ref bucket.Value4, value, null)) { goto Done; } return(false); Done : return(true); }
public static void Clear <TEvictionStrategy>(ref CacheAwareElement bucket, ref TEvictionStrategy policy) where TEvictionStrategy : struct, IEvictionStrategy <T> { // Note that the initial read is optimistically not synchronized. That is intentional. // We will interlock only when we have a candidate. in a worst case we may miss some // recently returned objects. Not a big deal. T inst = bucket.Value1; if (inst != null && policy.CanEvict(inst)) { Interlocked.CompareExchange(ref bucket.Value1, null, inst); } inst = bucket.Value2; if (inst != null && policy.CanEvict(inst)) { Interlocked.CompareExchange(ref bucket.Value2, null, inst); } inst = bucket.Value3; if (inst != null && policy.CanEvict(inst)) { Interlocked.CompareExchange(ref bucket.Value3, null, inst); } inst = bucket.Value4; if (inst != null && policy.CanEvict(inst)) { Interlocked.CompareExchange(ref bucket.Value4, null, inst); } }
public static bool TryClaim(ref CacheAwareElement bucket, out T item) { // Note that the initial read is optimistically not synchronized. That is intentional. // We will interlock only when we have a candidate. in a worst case we may miss some // recently returned objects. Not a big deal. T inst = bucket.Value1; if (inst != null && inst == Interlocked.CompareExchange(ref bucket.Value1, null, inst)) { goto Done; } inst = bucket.Value2; if (inst != null && inst == Interlocked.CompareExchange(ref bucket.Value2, null, inst)) { goto Done; } inst = bucket.Value3; if (inst != null && inst == Interlocked.CompareExchange(ref bucket.Value3, null, inst)) { goto Done; } inst = bucket.Value4; if (inst != null && inst == Interlocked.CompareExchange(ref bucket.Value4, null, inst)) { goto Done; } item = null; return(false); Done: item = inst; return(true); }