internal V Get <V>(UserDataSlot <V> slot, V defaultValue) { using (synchronizer.AcquireReadLock()) { return(slot.GetUserData(this, defaultValue)); } }
internal bool Remove <V>(UserDataSlot <V> slot) { using (synchronizer.AcquireWriteLock()) { return(slot.RemoveUserData(this)); } }
internal void Set <V>(UserDataSlot <V> slot, V userData) { using (synchronizer.AcquireWriteLock()) { slot.SetUserData(this, userData); } }
internal bool Get <V>(UserDataSlot <V> slot, out V userData) { using (synchronizer.AcquireReadLock()) { return(slot.GetUserData(this, out userData)); } }
public static void UserDataStorageGetOrSetSimpleFactory() { string CreateString() => "Hello, world!"; var obj = new object(); var slot = UserDataSlot <string> .Allocate(); Equal("Hello, world!", obj.GetUserData().GetOrSet(slot, CreateString)); }
public static void UserDataStorageGetOrSet() { string ToStr(int value) => value.ToString(); var obj = new object(); var slot = UserDataSlot <string> .Allocate(); Equal("42", obj.GetUserData().GetOrSet(slot, 42, ToStr)); }
public static void UserDataStorage() { var slot = UserDataSlot <long> .Allocate(); var str = new string('a', 3); str.GetUserData().Set(slot, 42); Equal(42, str.GetUserData().Get(slot)); str = null; GC.Collect(); GC.WaitForFullGCComplete(); str = new string('a', 3); Equal(0, str.GetUserData().Get(slot)); }
internal bool Remove <V>(UserDataSlot <V> slot, out V userData) { //fast path if user data doesn't exist using (synchronizer.AcquireReadLock()) { if (!slot.Contains(this)) { userData = default; return(false); } } //non-fast path, user data exists, so remove it using (synchronizer.AcquireWriteLock()) { userData = slot.GetUserData(this, default); return(slot.RemoveUserData(this)); } }
internal V GetOrSet <V, S>(UserDataSlot <V> slot, ref S valueFactory) where S : struct, ISupplier <V> { V userData; //fast path - read user data if it is already exists //do not use UpgradeableReadLock due to performance reasons using (synchronizer.AcquireReadLock()) { if (slot.GetUserData(this, out userData)) { return(userData); } } //non-fast path, no user data presented using (synchronizer.AcquireWriteLock()) { if (!slot.GetUserData(this, out userData)) { slot.SetUserData(this, userData = valueFactory.Supply()); } return(userData); } }