sim, arch, base: Refactor the base remote GDB class.
[gem5.git] / src / sim / eventq.cc
index 0735a011b2534d5933b7980460174058d4a99fdd..80de1808fbef5eb1994c07c3cc3f24d78f110f2e 100644 (file)
 #include <cassert>
 #include <iostream>
 #include <string>
+#include <unordered_map>
 #include <vector>
 
-#include "base/hashmap.hh"
-#include "base/misc.hh"
+#include "base/logging.hh"
 #include "base/trace.hh"
 #include "cpu/smt.hh"
-#include "debug/Config.hh"
+#include "debug/Checkpoint.hh"
 #include "sim/core.hh"
 #include "sim/eventq_impl.hh"
 
@@ -203,6 +203,7 @@ EventQueue::remove(Event *event)
 Event *
 EventQueue::serviceOne()
 {
+    std::lock_guard<EventQueue> lock(*this);
     Event *event = head;
     Event *next = head->nextInBin;
     event->flags.clear(Event::Scheduled);
@@ -226,7 +227,7 @@ EventQueue::serviceOne()
 
         event->process();
         if (event->isExitEvent()) {
-            assert(!event->flags.isSet(Event::AutoDelete) ||
+            assert(!event->flags.isSet(Event::Managed) ||
                    !event->flags.isSet(Event::IsMainQueue)); // would be silly
             return event;
         }
@@ -234,14 +235,13 @@ EventQueue::serviceOne()
         event->flags.clear(Event::Squashed);
     }
 
-    if (event->flags.isSet(Event::AutoDelete) && !event->scheduled())
-        delete event;
+    event->release();
 
     return NULL;
 }
 
 void
-Event::serialize(std::ostream &os)
+Event::serialize(CheckpointOut &cp) const
 {
     SERIALIZE_SCALAR(_when);
     SERIALIZE_SCALAR(_priority);
@@ -250,27 +250,21 @@ Event::serialize(std::ostream &os)
 }
 
 void
-Event::unserialize(Checkpoint *cp, const string &section)
+Event::unserialize(CheckpointIn &cp)
 {
-}
-
-void
-Event::unserialize(Checkpoint *cp, const string &section, EventQueue *eventq)
-{
-    if (scheduled())
-        eventq->deschedule(this);
+    assert(!scheduled());
 
     UNSERIALIZE_SCALAR(_when);
     UNSERIALIZE_SCALAR(_priority);
 
-    short _flags;
+    FlagsType _flags;
     UNSERIALIZE_SCALAR(_flags);
 
     // Old checkpoints had no concept of the Initialized flag
     // so restoring from old checkpoints always fail.
-    // Events are initialized on construction but original code 
-    // "flags = _flags" would just overwrite the initialization. 
-    // So, read in the checkpoint flags, but then set the Initialized 
+    // Events are initialized on construction but original code
+    // "flags = _flags" would just overwrite the initialization.
+    // So, read in the checkpoint flags, but then set the Initialized
     // flag on top of it in order to avoid failures.
     assert(initialized());
     flags = _flags;
@@ -279,62 +273,23 @@ Event::unserialize(Checkpoint *cp, const string &section, EventQueue *eventq)
     // need to see if original event was in a scheduled, unsquashed
     // state, but don't want to restore those flags in the current
     // object itself (since they aren't immediately true)
-    bool wasScheduled = flags.isSet(Scheduled) && !flags.isSet(Squashed);
-    flags.clear(Squashed | Scheduled);
-
-    if (wasScheduled) {
-        DPRINTF(Config, "rescheduling at %d\n", _when);
-        eventq->schedule(this, _when);
-    }
-}
-
-void
-EventQueue::serialize(ostream &os)
-{
-    std::list<Event *> eventPtrs;
-
-    int numEvents = 0;
-    Event *nextBin = head;
-    while (nextBin) {
-        Event *nextInBin = nextBin;
-
-        while (nextInBin) {
-            if (nextInBin->flags.isSet(Event::AutoSerialize)) {
-                eventPtrs.push_back(nextInBin);
-                paramOut(os, csprintf("event%d", numEvents++),
-                         nextInBin->name());
-            }
-            nextInBin = nextInBin->nextInBin;
-        }
-
-        nextBin = nextBin->nextBin;
-    }
-
-    SERIALIZE_SCALAR(numEvents);
-
-    for (std::list<Event *>::iterator it = eventPtrs.begin();
-         it != eventPtrs.end(); ++it) {
-        (*it)->nameOut(os);
-        (*it)->serialize(os);
+    if (flags.isSet(Scheduled) && !flags.isSet(Squashed)) {
+        flags.clear(Squashed | Scheduled);
+    } else {
+        DPRINTF(Checkpoint, "Event '%s' need to be scheduled @%d\n",
+                name(), _when);
     }
 }
 
 void
-EventQueue::unserialize(Checkpoint *cp, const std::string &section)
+EventQueue::checkpointReschedule(Event *event)
 {
-    int numEvents;
-    UNSERIALIZE_SCALAR(numEvents);
-
-    std::string eventName;
-    for (int i = 0; i < numEvents; i++) {
-        // get the pointer value associated with the event
-        paramIn(cp, section, csprintf("event%d", i), eventName);
-
-        // create the event based on its pointer value
-        Serializable::create(cp, eventName);
-    }
+    // It's safe to call insert() directly here since this method
+    // should only be called when restoring from a checkpoint (which
+    // happens before thread creation).
+    if (event->flags.isSet(Event::Scheduled))
+        insert(event);
 }
-
 void
 EventQueue::dump() const
 {
@@ -363,7 +318,7 @@ EventQueue::dump() const
 bool
 EventQueue::debugVerify() const
 {
-    m5::hash_map<long, bool> map;
+    std::unordered_map<long, bool> map;
 
     Tick time = 0;
     short priority = 0;
@@ -459,29 +414,28 @@ Event::dump() const
 }
 
 EventQueue::EventQueue(const string &n)
-    : objName(n), head(NULL), _curTick(0),
-    async_queue_mutex(new std::mutex())
+    : objName(n), head(NULL), _curTick(0)
 {
 }
 
 void
 EventQueue::asyncInsert(Event *event)
 {
-    async_queue_mutex->lock();
+    async_queue_mutex.lock();
     async_queue.push_back(event);
-    async_queue_mutex->unlock();
+    async_queue_mutex.unlock();
 }
 
 void
 EventQueue::handleAsyncInsertions()
 {
     assert(this == curEventQueue());
-    async_queue_mutex->lock();
+    async_queue_mutex.lock();
 
     while (!async_queue.empty()) {
         insert(async_queue.front());
         async_queue.pop_front();
     }
 
-    async_queue_mutex->unlock();
+    async_queue_mutex.unlock();
 }