Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Merged with lock-free implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
safern committed Dec 10, 2016
1 parent 0ead086 commit 662a05f
Showing 1 changed file with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,19 @@ public void AddOrUpdate(TKey key, TValue value)

lock (_lock)
{
VerifyIntegrity();
_invalid = true;
int entryIndex = FindEntry(key);
object otherValue;
int entryIndex = _container.FindEntry(key, out otherValue);

// if we found a key we should just update, if no we should create a new entry.
if (entryIndex != -1)
{
_entries[entryIndex].depHnd.Free();
_entries[entryIndex].depHnd = new DependentHandle(key, value);
_container.UpdateValue(entryIndex, value);
}
else
{
CreateEntry(key, value);
}

_invalid = false;
}
}

Expand Down Expand Up @@ -540,6 +537,13 @@ internal void Clear()
}
}

internal void UpdateValue(int entryIndex, TValue newValue)
{
Debug.Assert(entryIndex != -1);

_entries[entryIndex].depHnd.SetSecondary(newValue);
}

//----------------------------------------------------------------------------------------
// This does two things: resize and scrub expired keys off bucket lists.
//
Expand Down Expand Up @@ -843,6 +847,11 @@ public void GetPrimaryAndSecondary(out object primary, out object secondary)
nGetPrimaryAndSecondary(_handle, out primary, out secondary);
}

public void SetSecondary(object secondary)
{
nSetSecondary(_handle, secondary);
}

//----------------------------------------------------------------------
// Forces dependentHandle back to non-allocated state (if not already there)
// and frees the handle if needed.
Expand All @@ -868,6 +877,9 @@ public void Free()
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void nGetPrimaryAndSecondary(IntPtr dependentHandle, out object primary, out object secondary);

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void nSetSecondary(IntPtr dependentHandle, object secondary);

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void nFree(IntPtr dependentHandle);
#endregion
Expand Down

0 comments on commit 662a05f

Please sign in to comment.