private void CleanUpFields() { _logger.LogDebug("Searching for items to clean up."); var currTickCount = Environment.TickCount; var toRemove = Shield.InTransaction(() => _freshIndex .Where(i => i.Item.RemovableSince.HasValue ? unchecked (currTickCount - i.Item.RemovableSince.Value) > Configuration.RemovableItemLingerMs : i.Item.ExpiresInMs <= 0) .Select(i => i.Item) .ToArray()); _logger.LogDebug("Found {ToRemoveCount} items to clean up.", toRemove.Length); Shield.InTransaction(() => { foreach (var item in toRemove) { if (_local.TryGetValue(item.Key, out var mi) && mi == item) { if (item.RemovableSince.HasValue) { _local.Remove(item.Key); } else { Expire(item); } } } }); }
private TimerCallback GetDeletableTimerMethod() { var lockObj = new object(); return(_ => { bool lockTaken = false; try { Monitor.TryEnter(lockObj, ref lockTaken); if (!lockTaken) { return; } var currTickCount = Environment.TickCount; var toRemove = Shield.InTransaction(() => _freshIndex .Where(i => i.Item.RemovableSince.HasValue ? unchecked (currTickCount - i.Item.RemovableSince.Value) > Configuration.RemovableItemLingerMs : i.Item.ExpiresInMs <= 0) .Select(i => i.Item) .ToArray()); Shield.InTransaction(() => { foreach (var item in toRemove) { if (_local.TryGetValue(item.Key, out var mi) && mi == item) { if (item.RemovableSince.HasValue) { _local.Remove(item.Key); } else { Expire(item); } } } }); } catch (Exception ex) { RaiseError(new GossipBackendException("Unexpected error on deletable timer task.", ex)); } finally { if (lockTaken) { Monitor.Exit(lockObj); } } }); }
private static void DictionaryTest() { var dict = new ShieldedDictNc<int, Shielded<int>>(); var randomizr = new Random(); while (true) { var transactionCounter = 0; var time = mtTest("dictionary", 10000, i => { var rnd = randomizr.Next(10); if (i % 2 == 0) // adder task - 500 of these return Task.Factory.StartNew(() => { Shield.InTransaction(() => { Interlocked.Increment(ref transactionCounter); var v = dict.ContainsKey(rnd) ? dict[rnd] : null; int? num = v != null ? (int?)v.Value : null; Thread.Sleep(1); if (v == null) dict[rnd] = new Shielded<int>(1); else if (v.Value == -1) dict.Remove(rnd); else v.Modify((ref int a) => a = num.Value + 1); } ); }, TaskCreationOptions.LongRunning ); else // subtractor task - 500 of these return Task.Factory.StartNew(() => { Shield.InTransaction(() => { Interlocked.Increment(ref transactionCounter); var v = dict.ContainsKey(rnd) ? dict[rnd] : null; int? num = v != null ? (int?)v.Value : null; Thread.Sleep(1); if (v == null) dict[rnd] = new Shielded<int>(-1); else if (v.Value == 1) dict.Remove(rnd); else v.Modify((ref int a) => a = num.Value - 1); } ); }, TaskCreationOptions.LongRunning ); }); var sum = Enumerable.Range(0, 10).Sum(n => dict.ContainsKey(n) ? dict[n] : 0); var zeroes = Shield.InTransaction(() => dict.Any(kvp => kvp.Value == 0)); Console.WriteLine(" {0} ms with {1} iterations and sum {2}, {3}", time, transactionCounter, sum, zeroes ? "with zeroes!" : "no zeroes."); } }