Example #1
0
            protected override bool SetValue(TValue value, bool replace)
            {
                var success = false;

                if (replace)
                {
                    while (!success)
                    {
                        var v = _value;
                        success = v == Interlocked.CompareExchange(ref _value, ValueBox.Create(value), v);
                    }
                    return(true);
                }
                var set = false;

                while (success)
                {
                    var v = _value;
                    if (null != v)
                    {
                        success = true;
                        set     = false;
                    }
                    else
                    {
                        success = v == Interlocked.CompareExchange(ref _value, ValueBox.Create(value), v);
                        set     = success;
                    }
                }
                return(set);
            }
Example #2
0
            protected override bool InitValue(TArg arg, out TValue value)
            {
                var v = Initializer(arg);

                _value = ValueBox.Create(v);
                value  = v;
                return(true);
            }
Example #3
0
 protected override bool SetValue(TValue value, bool replace)
 {
     if (replace || null == _value)
     {
         _value = ValueBox.Create(value);
         return(true);
     }
     return(false);
 }
Example #4
0
 protected override bool SetValue(TValue value, bool replace)
 {
     return(_lock.Synchronized(() => {
         if (replace || null == _value)
         {
             _value = ValueBox.Create(value);
             return true;
         }
         return false;
     }));
 }
Example #5
0
            protected override bool InitValue(TArg arg, out TValue value)
            {
                var newValue = ValueBox.Create(Initializer(arg));

                Interlocked.MemoryBarrier();
                if (null == Interlocked.CompareExchange(ref _value, newValue, null))
                {
                    value = newValue.Value;
                    return(true);
                }
                value = _value.Value;
                return(false);
            }
Example #6
0
 protected override bool InitValue(TArg arg, out TValue value)
 {
     _lock.Lock();
     try
     {
         if (null != _value)
         {
             value = _value.Value;
             return(false);
         }
         var v = Initializer(arg);
         _value = ValueBox.Create(v);
         value  = v;
         return(true);
     }
     finally
     {
         _lock.Release();
     }
 }