// Add a value to an existing basket
if (TryGetBasket (key, out basket)) {
- while (!taken) {
- try {
- basket.Lock.Enter (ref taken);
- if (!taken)
- continue;
-
- foreach (var p in basket) {
- if (comparer.Equals (p.Key, key))
- return false;
- }
- basket.Add (new Pair (key, value));
- } finally {
- if (taken)
- basket.Lock.Exit ();
+ try {
+ basket.Lock.Enter (ref taken);
+
+ foreach (var p in basket) {
+ if (comparer.Equals (p.Key, key))
+ return false;
}
+ basket.Add (new Pair (key, value));
+ } finally {
+ if (taken)
+ basket.Lock.Exit ();
}
} else {
// Add a new basket
if (!TryGetBasket (key, out basket)) {
Add (key, (temp = addValueFactory (key)));
} else {
- while (!taken) {
- try {
- basket.Lock.Enter (ref taken);
- if (!taken)
- continue;
-
- Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
- if (pair == null)
- throw new InvalidOperationException ("pair is null, shouldn't be");
- pair.Value = (temp = updateValueFactory (key, pair.Value));
- } finally {
- if (taken)
- basket.Lock.Exit ();
- }
+ try {
+ basket.Lock.Enter (ref taken);
+
+ Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
+ if (pair == null)
+ throw new InvalidOperationException ("pair is null, shouldn't be");
+ pair.Value = (temp = updateValueFactory (key, pair.Value));
+ } finally {
+ if (taken)
+ basket.Lock.Exit ();
}
}
if (!TryGetBasket (key, out basket))
return false;
- while (!taken) {
- try {
- basket.Lock.Enter (ref taken);
- if (!taken)
- continue;
+ try {
+ basket.Lock.Enter (ref taken);
- Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
- if (pair == null)
- return false;
- value = pair.Value;
- } finally {
- if (taken)
- basket.Lock.Exit ();
- }
+ Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
+ if (pair == null)
+ return false;
+ value = pair.Value;
+ } finally {
+ if (taken)
+ basket.Lock.Exit ();
}
return true;
if (!TryGetBasket (key, out basket))
return false;
- while (!taken) {
- try {
- basket.Lock.Enter (ref taken);
- if (!taken)
- continue;
+ try {
+ basket.Lock.Enter (ref taken);
- Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
- if (pair.Value.Equals (comparand)) {
- pair.Value = newValue;
+ Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
+ if (pair.Value.Equals (comparand)) {
+ pair.Value = newValue;
- return true;
- }
- } finally {
- if (taken)
- basket.Lock.Exit ();
+ return true;
}
+ } finally {
+ if (taken)
+ basket.Lock.Exit ();
}
return false;
return;
}
- while (!taken) {
- try {
- basket.Lock.Enter (ref taken);
- if (!taken)
- continue;
-
- Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
- if (pair == null)
- throw new InvalidOperationException ("pair is null, shouldn't be");
- pair.Value = value;
- } finally {
- if (taken)
- basket.Lock.Exit ();
- }
+ try {
+ basket.Lock.Enter (ref taken);
+
+ Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
+ if (pair == null)
+ throw new InvalidOperationException ("pair is null, shouldn't be");
+ pair.Value = value;
+ } finally {
+ if (taken)
+ basket.Lock.Exit ();
}
}
}
Pair pair = null;
bool taken = false;
- while (!taken) {
- try {
- basket.Lock.Enter (ref taken);
- if (!taken)
- continue;
- pair = basket.Find ((p) => comparer.Equals (p.Key, key));
- if (pair != null)
- temp = pair.Value;
- } finally {
- if (taken)
- basket.Lock.Exit ();
- }
+ try {
+ basket.Lock.Enter (ref taken);
+
+ pair = basket.Find ((p) => comparer.Equals (p.Key, key));
+ if (pair != null)
+ temp = pair.Value;
+ } finally {
+ if (taken)
+ basket.Lock.Exit ();
}
if (pair == null)
if (!TryGetBasket (key, out b))
return false;
- while (!taken) {
- try {
- b.Lock.Enter (ref taken);
- if (!taken)
- continue;
-
- TValue temp = default (TValue);
- // Should always be == 1 but who know
- bool result = b.RemoveAll ((p) => {
- bool r = comparer.Equals (p.Key, key);
- if (r) temp = p.Value;
- return r;
- }) >= 1;
- value = temp;
-
- if (result)
- Interlocked.Decrement (ref count);
-
- return result;
- } finally {
- if (taken)
- b.Lock.Exit ();
- }
+ try {
+ b.Lock.Enter (ref taken);
+
+ TValue temp = default (TValue);
+ // Should always be == 1 but who know
+ bool result = b.RemoveAll ((p) => {
+ bool r = comparer.Equals (p.Key, key);
+ if (r) temp = p.Value;
+ return r;
+ }) >= 1;
+ value = temp;
+
+ if (result)
+ Interlocked.Decrement (ref count);
+
+ return result;
+ } finally {
+ if (taken)
+ b.Lock.Exit ();
}
return false;
foreach (Basket b in container) {
bool taken = false;
- while (!taken) {
- try {
- b.Lock.Enter (ref taken);
- if (!taken)
- continue;
-
- foreach (Pair p in b) {
- if (i >= num)
- break;
- array[i++] = new KeyValuePair<TKey, TValue> (p.Key, p.Value);
- }
- } finally {
- if (taken)
- b.Lock.Exit ();
+ try {
+ b.Lock.Enter (ref taken);
+
+ foreach (Pair p in b) {
+ if (i >= num)
+ break;
+ array[i++] = new KeyValuePair<TKey, TValue> (p.Key, p.Value);
}
+ } finally {
+ if (taken)
+ b.Lock.Exit ();
}
}
}
foreach (Basket b in container) {
bool taken = false;
- while (!taken) {
- try {
- b.Lock.Enter (ref taken);
- if (!taken)
- continue;
-
- foreach (Pair p in b)
- yield return new KeyValuePair<TKey, TValue> (p.Key, p.Value);
- } finally {
- if (taken)
- b.Lock.Exit ();
- }
+ try {
+ b.Lock.Enter (ref taken);
+
+ foreach (Pair p in b)
+ yield return new KeyValuePair<TKey, TValue> (p.Key, p.Value);
+ } finally {
+ if (taken)
+ b.Lock.Exit ();
}
}
}