diff --git a/StgSharp.Common/HighPerformance/Memory/L4.Evict.cs b/StgSharp.Common/HighPerformance/Memory/L4.Evict.cs index e005fe8c..1d878efa 100644 --- a/StgSharp.Common/HighPerformance/Memory/L4.Evict.cs +++ b/StgSharp.Common/HighPerformance/Memory/L4.Evict.cs @@ -73,7 +73,7 @@ namespace StgSharp.HighPerformance.Memory int bitPos = BitOperations.TrailingZeroCount(evictMask); int index = bitPos + i; CacheLineHead* head = &_head[index]; - if (Interlocked.CompareExchange(ref head->_refCount, -1, 0) == 0) + if (Interlocked.CompareExchange(ref head->_refCount, int.MinValue, 0) == 0) { // EvictLine(i + index); _ = _map.TryRemove(head->_origin, out _); diff --git a/StgSharp.Common/HighPerformance/Memory/L4.Prefetch.cs b/StgSharp.Common/HighPerformance/Memory/L4.Prefetch.cs index 0eb325fc..8f51cb33 100644 --- a/StgSharp.Common/HighPerformance/Memory/L4.Prefetch.cs +++ b/StgSharp.Common/HighPerformance/Memory/L4.Prefetch.cs @@ -82,6 +82,7 @@ namespace StgSharp.HighPerformance.Memory CacheLineHead* head = (CacheLineHead*)handle; int index = head->_id; (*_eviction)[index] = 0; // reset eviction counter + return true; } return false; } diff --git a/StgSharp.Common/HighPerformance/Memory/L4.cs b/StgSharp.Common/HighPerformance/Memory/L4.cs index ea2a32d0..d856f51c 100644 --- a/StgSharp.Common/HighPerformance/Memory/L4.cs +++ b/StgSharp.Common/HighPerformance/Memory/L4.cs @@ -118,12 +118,16 @@ namespace StgSharp.HighPerformance.Memory { if (Interlocked.CompareExchange(ref _prefetching, 1, 0) == 0) { - if (_aheadCount < PredictionLeastAhead) + try { - // do prefetch in work load now, sync - Prefetch(); - break; - } else + if (_aheadCount < PredictionLeastAhead) + { + // do prefetch in work load now, sync + Prefetch(); + break; + } + } + finally { _prefetching = 0; } @@ -136,9 +140,10 @@ namespace StgSharp.HighPerformance.Memory { // cache hit CacheLineHead* entry = (CacheLineHead*)entryHandle; - if (Interlocked.Increment(ref entry->_refCount) < 0) + if (Interlocked.Increment(ref entry->_refCount) > 0) { (*_eviction)[entry->_id] = 0; + _ = Interlocked.Increment(ref _activeCount); return new CacheLine(entry->_address, ref _activeCount, ref entry->_refCount); } else { @@ -171,8 +176,6 @@ namespace StgSharp.HighPerformance.Memory _handle = handle; _refCount = ref cacheRefCount; _activeCount = ref activeCount; - _ = Interlocked.Increment(ref _activeCount); - _ = Interlocked.Increment(ref _refCount); } [Obsolete("You should not create a CacheLineHandle outside a L4", true)]