예제 #1
0
 internal V Get <V>(UserDataSlot <V> slot, V defaultValue)
 {
     using (synchronizer.AcquireReadLock())
     {
         return(slot.GetUserData(this, defaultValue));
     }
 }
예제 #2
0
 internal bool Remove <V>(UserDataSlot <V> slot)
 {
     using (synchronizer.AcquireWriteLock())
     {
         return(slot.RemoveUserData(this));
     }
 }
예제 #3
0
 internal void Set <V>(UserDataSlot <V> slot, V userData)
 {
     using (synchronizer.AcquireWriteLock())
     {
         slot.SetUserData(this, userData);
     }
 }
예제 #4
0
 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));
        }
예제 #8
0
 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));
     }
 }
예제 #9
0
            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);
                }
            }