📄 clock.cc
字号:
#include "assert.hh"#include "clock.hh"#include "sulima.hh"#include "checkpoint.hh"#include "error.hh"// The serialization information.SerialType<Event> Event::type("Event");SerialType<Clock> Clock::type("Clock");// Restore an event from a checkpoint.Event::Event(Checkpoint &cp) : Serializable(cp), when(extract<ClockValue>(cp)){ SerialID cid = extract<SerialID>(cp); if (cid) { // Insert it into the clock's queue. Clock *clock = dynamic_cast<Clock *>(find_object(cid)); if (!clock) throw Checkpoint::SyntaxError(cp, "%d is not a clock ID", cid); clock->insert(this); }}// If the sentinel event is invoked, we're in trouble.voidEvent::invoke(){ throw Error("Clock overflow."); // ...but squeek for help}// Serialization interface. Sentinel objects do not get checkpointed.voidEvent::checkpoint(Checkpoint &cp, bool parent) const{ if (when < infinity) { // Find the sentinel event in this list. Event *e = next; for (; e && e->when < infinity; e = e->next) continue; SerialID clock_id = e ? e->clock->id : 0; // Dump the information in the file. if (parent) cp << type << ' ' << id << '\n'; cp << when << ' ' << clock_id << '\n'; }}// Clock constructor.Clock::Clock(ClockValue f) : freq(f){ now = 0; earliest = infinity; queue = new Event(this);}// Restore a clock from a checkpoint.Clock::Clock(Checkpoint &cp) : Serializable(cp), freq(extract<ClockValue>(cp)){ cp >> now; earliest = infinity; queue = new Event(this);}Clock::~Clock(){}// Poll the event queue.voidClock::invoke_events(){ Event *e; do { e = queue; queue = queue->next; earliest = queue->when; e->invoke(); if (e->interval > 0) { e->when += e->interval; insert(e); } } while (now > earliest);}// Insert an event into the queue.voidClock::insert(Event *e){ if (e->when < earliest) { // Prepend to the queue. earliest = e->when; e->next = queue; queue = e; } else { // Insert in the middle of a queue. Event *f = queue; for (; e->when >= f->next->when; f = f->next) continue; e->next = f->next; f->next = e; }}// Remove an event from the queue.voidClock::remove(Event *e){ if (e == queue) { queue = e->next; // easy earliest = queue->when; } else { Event *f = queue; for (; f->next != e; f = f->next) if (f->when == infinity) return; f->next = e->next; }}// Serialization interface.voidClock::checkpoint(Checkpoint &cp, bool parent) const{ if (parent) cp << type << ' ' << id << '\n'; cp << freq << ' ' << now << '\n';}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -