public void MarkDirty(IHaveCells target, bool allowMultiple = false) { // TODO Lock or use thread-safe collections // Because notify can be parallel var dirties = this.dirties[CurrentCellIndex]; lock (dirties) // TODO Better use a thread-safe collection to reduce blocking { var dirty = dirties.Find(it => ReferenceEquals(it.Inner, target)); if (dirty == null) { dirty = new Dirty { Phase = 1, Inner = target, }; dirties.Add(dirty); } else if (dirty.Phase == 1) { if (!allowMultiple) { throw new InvalidOperationException("Cannot mark dirty for same target twice in same run"); } } else if (dirty.Phase >= 2 && dirty.Phase <= 4) { dirty.Phase = 1; } else { throw new NotImplementedException(dirty.Phase.ToString()); } } }
public void MarkDirty(IHaveCells target, bool allowMultiple = false) { var dirties = this.dirties[CurrentCellIndex]; lock (dirties) // TODO Better use a thread-safe collection to reduce blocking { var dirty = dirties.Find(it => ReferenceEquals(it.Inner, target)); // TODO If use thread-safe collection, should lock on dirty too // at least in debug mode, to avoid accidentally using 2 threads to write to same if (dirty == null) { dirty = new Dirty { Phase = 1, Inner = target, }; dirties.Add(dirty); } else if (dirty.Phase == 1) { if (!allowMultiple) { throw new InvalidOperationException("Cannot mark dirty for same target twice in same run"); } } else if (dirty.Phase >= 2 && dirty.Phase <= 4) { dirty.Phase = 1; } else { throw new NotImplementedException(dirty.Phase.ToString()); } } }