public TValue GetValue(TKey key, CreateValueCallback createValueCallback) { if (key == default(TKey)) { throw new ArgumentNullException("Null key", "key"); } if (createValueCallback == null) { throw new ArgumentNullException("Null create delegate", "createValueCallback"); } TValue res; lock (_lock) { if (TryGetValue(key, out res)) { return(res); } res = createValueCallback(key); Add(key, res); } return(res); }
public TValue GetValue(TKey key, CreateValueCallback createValueCallback) { TValue local; if (createValueCallback == null) { throw new ArgumentNullException("createValueCallback"); } if (TryGetValue(key, out local)) { return(local); } // If we got here, the key is not currently in table. Invoke the callback (outside the lock) to generate the new value for the key. var newValue = createValueCallback(key); lock (_lock) { VerifyIntegrity(); _invalid = true; // Now that we've retaken the lock, must recheck in case we lost a ---- to add the key. if (TryGetValueWorker(key, out local)) { _invalid = false; return(local); } // Verified in-lock that we won the ---- to add the key. Add it now. CreateEntry(key, newValue); _invalid = false; return(newValue); } }
public TValue GetValue(TKey key, CreateValueCallback <TKey, TValue> createValueCallback) { TValue local; if (createValueCallback == null) { throw new ArgumentNullException("createValueCallback"); } if (this.TryGetValue(key, out local)) { return(local); } TValue local2 = createValueCallback(key); lock (this._lock) { this.VerifyIntegrity(); this._invalid = true; if (this.TryGetValueWorker(key, out local)) { this._invalid = false; return(local); } this.CreateEntry(key, local2); this._invalid = false; return(local2); } }
/// <summary> /// Atomically searches for a specified key in the table and returns the corresponding value. /// If the key does not exist in the table, the method invokes a callback method to create a /// value that is bound to the specified key. /// </summary> /// <param name="key">key of the value to find. Cannot be null.</param> /// <param name="createValueCallback">callback that creates value for key. Cannot be null.</param> /// <returns></returns> /// <remarks> /// If multiple threads try to initialize the same key, the table may invoke createValueCallback /// multiple times with the same key. Exactly one of these calls will succeed and the returned /// value of that call will be the one added to the table and returned by all the racing GetValue() calls. /// This rule permits the table to invoke createValueCallback outside the internal table lock /// to prevent deadlocks. /// </remarks> public TValue GetValue(TKey key, CreateValueCallback createValueCallback) { ArgumentNullException.ThrowIfNull(createValueCallback); // key is validated by TryGetValue return(TryGetValue(key, out TValue? existingValue) ? existingValue : GetValueLocked(key, createValueCallback)); }
/// <summary> /// Atomically searches for a specified key in the table and returns the corresponding value.If the key does not exist in the table, the method invokes a callback method to create a value that is bound to the specified key. /// </summary> /// <param name="key">The key to search for. key represents the object to which the property is attached.</param> /// <param name="createValueCallback">A delegate to a method that can create a value for the given key. It has a single parameter of type TKey, and returns a value of type TValue.</param> /// <returns>The value attached to key, if key already exists in the table; otherwise, the new value returned by the createValueCallback delegate.</returns> public TValue GetValue(TKey key, CreateValueCallback createValueCallback) { if (key == null) { throw new ArgumentNullException("key"); } if (createValueCallback == null) { throw new ArgumentNullException("createValueCallback"); } return(_wrapped.GetOrAdd(key, input => createValueCallback(input))); }
public TValue GetValue(TKey key, CreateValueCallback createValueCallback) { CleanUp(); var reference = new Reference(key); TValue value; if (data.TryGetValue(reference, out value)) { return(value); } return(data[reference] = createValueCallback(key)); }
/// <summary> /// Atomically searches for a specified key in the table and returns the corresponding value. /// If the key does not exist in the table, the method invokes a callback method to create a /// value that is bound to the specified key. /// </summary> /// <param name="key">key of the value to find. Cannot be null.</param> /// <param name="createValueCallback">callback that creates value for key. Cannot be null.</param> /// <returns></returns> /// <remarks> /// If multiple threads try to initialize the same key, the table may invoke createValueCallback /// multiple times with the same key. Exactly one of these calls will succeed and the returned /// value of that call will be the one added to the table and returned by all the racing GetValue() calls. /// This rule permits the table to invoke createValueCallback outside the internal table lock /// to prevent deadlocks. /// </remarks> public TValue GetValue(TKey key, CreateValueCallback createValueCallback) { // key is validated by TryGetValue if (createValueCallback is null) { throw new ArgumentNullException(nameof(createValueCallback)); } return(TryGetValue(key, out TValue existingValue) ? existingValue : GetValueLocked(key, createValueCallback)); }
public TValue GetValue(TKey key, CreateValueCallback createValueCallback) { // Our call to TryGetValue() validates key so no need for us to. // // if (key == null) // { // ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); // } if (createValueCallback == null) { throw new ArgumentNullException("createValueCallback"); } TValue existingValue; if (TryGetValue(key, out existingValue)) { return(existingValue); } // If we got here, the key is not currently in table. Invoke the callback (outside the lock) // to generate the new value for the key. TValue newValue = createValueCallback(key); lock (_lock) { VerifyIntegrity(); _invalid = true; // Now that we've retaken the lock, must recheck in case we lost a ---- to add the key. if (TryGetValueWorker(key, out existingValue)) { _invalid = false; return(existingValue); } else { // Verified in-lock that we won the ---- to add the key. Add it now. CreateEntry(key, newValue); _invalid = false; return(newValue); } } }
private TValue GetValueLocked(TKey key, CreateValueCallback createValueCallback) { // If we got here, the key was not in the table. Invoke the callback (outside the lock) // to generate the new value for the key. TValue newValue = createValueCallback(key); lock (_lock) { // Now that we've taken the lock, must recheck in case we lost a race to add the key. if (_container.TryGetValueWorker(key, out TValue existingValue)) { return(existingValue); } else { // Verified in-lock that we won the race to add the key. Add it now. CreateEntry(key, newValue); return(newValue); } } }
public TValue GetValue(TKey key, CreateValueCallback createValueCallback) { if (createValueCallback == null) { throw new ArgumentException("Null create delegate", nameof(createValueCallback)); } lock (_lock) { if (TryGetValue(key, out var value)) { return(value); } else { value = createValueCallback(key); Add(key, value); return(value); } } }
//-------------------------------------------------------------------------------------------- // key: key of the value to find. Cannot be null. // createValueCallback: callback that creates value for key. Cannot be null. // // Atomically tests if key exists in table. If so, returns corresponding value. If not, // invokes createValueCallback() passing it the key. The returned value is bound to the key in the table // and returned as the result of GetValue(). // // If multiple threads race to initialize the same key, the table may invoke createValueCallback // multiple times with the same key. Exactly one of these calls will "win the race" and the returned // value of that call will be the one added to the table and returned by all the racing GetValue() calls. // // This rule permits the table to invoke createValueCallback outside the internal table lock // to prevent deadlocks. //-------------------------------------------------------------------------------------------- public TValue GetValue(TKey key, CreateValueCallback createValueCallback) { // Our call to TryGetValue() validates key so no need for us to. // // if (key == null) // { // ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); // } if (createValueCallback == null) { throw new ArgumentNullException("createValueCallback"); } TValue existingValue; if (TryGetValue(key, out existingValue)) { return(existingValue); } return(GetValueLocked(key, createValueCallback)); }
/// <summary> /// Atomically searches for a specified key in the table and returns the corresponding value. /// If the key does not exist in the table, the method invokes a callback method to create a /// value that is bound to the specified key. /// </summary> /// <param name="key">key of the value to find. Cannot be null.</param> /// <param name="createValueCallback">callback that creates value for key. Cannot be null.</param> /// <returns></returns> /// <remarks> /// If multiple threads try to initialize the same key, the table may invoke createValueCallback /// multiple times with the same key. Exactly one of these calls will succeed and the returned /// value of that call will be the one added to the table and returned by all the racing GetValue() calls. /// This rule permits the table to invoke createValueCallback outside the internal table lock /// to prevent deadlocks. /// </remarks> public TValue GetValue(TKey key, CreateValueCallback createValueCallback !!) { // key is validated by TryGetValue return(TryGetValue(key, out TValue? existingValue) ?
public TValue GetValue(TKey key, CreateValueCallback createValueCallback) { throw new NotImplementedException(); }