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); }
protected override bool InitValue(TArg arg, out TValue value) { var v = Initializer(arg); _value = ValueBox.Create(v); value = v; return(true); }
protected override bool SetValue(TValue value, bool replace) { if (replace || null == _value) { _value = ValueBox.Create(value); return(true); } return(false); }
protected override bool SetValue(TValue value, bool replace) { return(_lock.Synchronized(() => { if (replace || null == _value) { _value = ValueBox.Create(value); return true; } return false; })); }
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); }
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(); } }