internal void ScheduleEvent(SimEvent ev) { Debug.Assert(!ev.Scheduled && !ev.Succeeded && !ev.Failed); ev.At = Now; ev.Version = _lowPriority++; _events.Add(ev); }
internal static bool IsSmaller(SimEvent h1, SimEvent h2) { #pragma warning disable RECS0018 // Comparison of floating point numbers with equality operator return(h1.At < h2.At || (h1.At == h2.At && h1.Version < h2.Version)); #pragma warning restore RECS0018 // Comparison of floating point numbers with equality operator }
private void DoSimulate() { while (!Ended) { var minP = _processes.Min; var minT = _events.Min; if (SimEvent.IsSmaller(minP, minT)) { // Step a process minP.Step(); } else { // End an event minT.End(); _events.RemoveMin(); } } }
/// <summary> /// </summary> /// <param name="seed"></param> internal SimEnvironment(int seed) { // We add a dummy timeout event and a dummy process, so that heaps are never empty. They // have the maximum priority available, since they never have to be called. const double maxPr = double.PositiveInfinity; const ulong maxVer = ulong.MaxValue; var dummyP = new SimProcess(this, DummyProcess()) { At = maxPr, Version = maxVer }; var dummyEv = new Dummy(this) { At = maxPr, Version = maxVer }; _processes = new OptimizedSkewHeap(dummyP); _events = new OptimizedSkewHeap(dummyEv); Random = TRandom.New(new MT19937Generator(seed)); EndEvent = new Dummy(this); }
public void Run(SimEvent until) { Contract.Requires <InvalidOperationException>(!Ended); Contract.Requires <InvalidOperationException>(!double.IsPositiveInfinity(Peek)); Contract.Requires <ArgumentNullException>(until != null, ErrorMessages.NullEvent); Contract.Requires <ArgumentException>(ReferenceEquals(this, until.Env), ErrorMessages.DifferentEnvironment); Contract.Requires <ArgumentException>(!until.Failed); Contract.Ensures(Ended); // Real-time management. if (RealTime.Enabled) { // Set the base UNIX time, used to computed the "wall clock" time of timeout events. RealTime.SetCurrentUnixTime(); } // TODO Fix this, since when until is triggered UntilProcess is not immediately triggered. Process(UntilProcess(until)); DoSimulate(); }
private static IEnumerable <SimEvent> ResumeDelayed(SimEvent <object> suspend, double delay) { yield return(suspend.Env.Timeout(delay)); suspend.TrySucceed(); }
private IEnumerable <SimEvent> UntilProcess(SimEvent ev) { yield return(ev); EndSimulation(); }