19983Sstever@gmail.com/*
29983Sstever@gmail.com * Copyright (c) 2011-2013 Advanced Micro Devices, Inc.
39983Sstever@gmail.com * Copyright (c) 2013 Mark D. Hill and David A. Wood
49983Sstever@gmail.com * All rights reserved.
59983Sstever@gmail.com *
69983Sstever@gmail.com * Redistribution and use in source and binary forms, with or without
79983Sstever@gmail.com * modification, are permitted provided that the following conditions are
89983Sstever@gmail.com * met: redistributions of source code must retain the above copyright
99983Sstever@gmail.com * notice, this list of conditions and the following disclaimer;
109983Sstever@gmail.com * redistributions in binary form must reproduce the above copyright
119983Sstever@gmail.com * notice, this list of conditions and the following disclaimer in the
129983Sstever@gmail.com * documentation and/or other materials provided with the distribution;
139983Sstever@gmail.com * neither the name of the copyright holders nor the names of its
149983Sstever@gmail.com * contributors may be used to endorse or promote products derived from
159983Sstever@gmail.com * this software without specific prior written permission.
169983Sstever@gmail.com *
179983Sstever@gmail.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
189983Sstever@gmail.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
199983Sstever@gmail.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
209983Sstever@gmail.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
219983Sstever@gmail.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
229983Sstever@gmail.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
239983Sstever@gmail.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
249983Sstever@gmail.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
259983Sstever@gmail.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
269983Sstever@gmail.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
279983Sstever@gmail.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289983Sstever@gmail.com *
299983Sstever@gmail.com * Authors: Steve Reinhardt
309983Sstever@gmail.com */
319983Sstever@gmail.com
329983Sstever@gmail.com#include "sim/global_event.hh"
339983Sstever@gmail.com
349983Sstever@gmail.comstd::mutex BaseGlobalEvent::globalQMutex;
359983Sstever@gmail.com
369983Sstever@gmail.comBaseGlobalEvent::BaseGlobalEvent(Priority p, Flags f)
3710361SAndreas.Sandberg@ARM.com    : barrier(numMainEventQueues),
3810361SAndreas.Sandberg@ARM.com      barrierEvent(numMainEventQueues, NULL)
399983Sstever@gmail.com{
409983Sstever@gmail.com}
419983Sstever@gmail.com
429983Sstever@gmail.com
439983Sstever@gmail.comBaseGlobalEvent::~BaseGlobalEvent()
449983Sstever@gmail.com{
459983Sstever@gmail.com    // see GlobalEvent::BarrierEvent::~BarrierEvent() comments
469983Sstever@gmail.com    if (barrierEvent[0] != NULL) {
479983Sstever@gmail.com        for (int i = 0; i < numMainEventQueues; ++i)
489983Sstever@gmail.com            delete barrierEvent[i];
499983Sstever@gmail.com    }
509983Sstever@gmail.com}
519983Sstever@gmail.com
529983Sstever@gmail.com
539983Sstever@gmail.comvoid BaseGlobalEvent::schedule(Tick when)
549983Sstever@gmail.com{
559983Sstever@gmail.com    // This function is scheduling a global event, which actually is a
569983Sstever@gmail.com    // set of local events, one event on each eventq. Global events need
579983Sstever@gmail.com    // to have a total order. A thread cannot start executing events that
589983Sstever@gmail.com    // follow a global event till all other threads have executed that global
599983Sstever@gmail.com    // event as well. If global events were not in a total order, a deadlock
609983Sstever@gmail.com    // would occur for there will be two threads who would be waiting for
619983Sstever@gmail.com    // each other to execute the global events they themselves have executed.
629983Sstever@gmail.com    //
639983Sstever@gmail.com    // To ensure this total order, we do two things.
649983Sstever@gmail.com    // First, before scheduling any global event, a thread needs to acquire
659983Sstever@gmail.com    // the lock globalQMutex. This ensures that only one thread can schedule
669983Sstever@gmail.com    // global events at any given time.
679983Sstever@gmail.com    // Second, the local events corresponding to a global event are always
689983Sstever@gmail.com    // first inserted in to the asyncq, irrespective of whether or not the
699983Sstever@gmail.com    // thread scheduling the event owns the eventq on which the event is
709983Sstever@gmail.com    // being scheduled. Thus global events have the same order in the asyncq
719983Sstever@gmail.com    // of each thread. When they are inserted in the actual eventq, the
729983Sstever@gmail.com    // comparators in the Event class ensure that the total order is
739983Sstever@gmail.com    // maintained.
749983Sstever@gmail.com
759983Sstever@gmail.com    globalQMutex.lock();
769983Sstever@gmail.com
779983Sstever@gmail.com    for (int i = 0; i < numMainEventQueues; ++i) {
789983Sstever@gmail.com        mainEventQueue[i]->schedule(barrierEvent[i], when, true);
799983Sstever@gmail.com    }
809983Sstever@gmail.com
819983Sstever@gmail.com    globalQMutex.unlock();
829983Sstever@gmail.com}
839983Sstever@gmail.com
849983Sstever@gmail.comvoid BaseGlobalEvent::deschedule()
859983Sstever@gmail.com{
869983Sstever@gmail.com    EventQueue *q = curEventQueue();
879983Sstever@gmail.com    for (uint32_t i = 0; i < numMainEventQueues; ++i) {
889983Sstever@gmail.com        if (barrierEvent[i]->scheduled()) {
899983Sstever@gmail.com            curEventQueue(mainEventQueue[i]);
909983Sstever@gmail.com            mainEventQueue[i]->deschedule(barrierEvent[i]);
919983Sstever@gmail.com        }
929983Sstever@gmail.com    }
939983Sstever@gmail.com
949983Sstever@gmail.com    curEventQueue(q);
959983Sstever@gmail.com}
969983Sstever@gmail.com
979983Sstever@gmail.comvoid BaseGlobalEvent::reschedule(Tick when)
989983Sstever@gmail.com{
999983Sstever@gmail.com    // Read the comment in the schedule() function above.
1009983Sstever@gmail.com    globalQMutex.lock();
1019983Sstever@gmail.com
1029983Sstever@gmail.com    for (uint32_t i = 0; i < numMainEventQueues; ++i) {
1039983Sstever@gmail.com        if (barrierEvent[i]->scheduled())
1049983Sstever@gmail.com            mainEventQueue[i]->reschedule(barrierEvent[i], when);
1059983Sstever@gmail.com        else
1069983Sstever@gmail.com            mainEventQueue[i]->schedule(barrierEvent[i], when, true);
1079983Sstever@gmail.com    }
1089983Sstever@gmail.com
1099983Sstever@gmail.com    globalQMutex.unlock();
1109983Sstever@gmail.com}
1119983Sstever@gmail.com
1129983Sstever@gmail.comBaseGlobalEvent::BarrierEvent::~BarrierEvent()
1139983Sstever@gmail.com{
1149983Sstever@gmail.com    // if AutoDelete is set, local events will get deleted in event
1159983Sstever@gmail.com    // loop, but we need to delete GlobalEvent object too... so let
1169983Sstever@gmail.com    // the local event in slot 0 do it
1179983Sstever@gmail.com    if (isFlagSet(AutoDelete) && _globalEvent->barrierEvent[0] == this) {
1189983Sstever@gmail.com        // set backpointer to NULL so that global event knows not to
1199983Sstever@gmail.com        // turn around and recursively delete local events
1209983Sstever@gmail.com        _globalEvent->barrierEvent[0] = NULL;
1219983Sstever@gmail.com        delete _globalEvent;
1229983Sstever@gmail.com    }
1239983Sstever@gmail.com}
1249983Sstever@gmail.com
1259983Sstever@gmail.com
1269983Sstever@gmail.comvoid
1279983Sstever@gmail.comGlobalEvent::BarrierEvent::process()
1289983Sstever@gmail.com{
1299983Sstever@gmail.com    // wait for all queues to arrive at barrier, then process event
1309983Sstever@gmail.com    if (globalBarrier()) {
1319983Sstever@gmail.com        _globalEvent->process();
1329983Sstever@gmail.com    }
1339983Sstever@gmail.com
1349983Sstever@gmail.com    // second barrier to force all queues to wait for event processing
1359983Sstever@gmail.com    // to finish before continuing
1369983Sstever@gmail.com    globalBarrier();
1379983Sstever@gmail.com}
1389983Sstever@gmail.com
1399983Sstever@gmail.com
1409983Sstever@gmail.comvoid
1419983Sstever@gmail.comGlobalSyncEvent::BarrierEvent::process()
1429983Sstever@gmail.com{
1439983Sstever@gmail.com    // wait for all queues to arrive at barrier, then process event
1449983Sstever@gmail.com    if (globalBarrier()) {
1459983Sstever@gmail.com        _globalEvent->process();
1469983Sstever@gmail.com    }
1479983Sstever@gmail.com
1489983Sstever@gmail.com    // second barrier to force all queues to wait for event processing
1499983Sstever@gmail.com    // to finish before continuing
1509983Sstever@gmail.com    globalBarrier();
1519983Sstever@gmail.com    curEventQueue()->handleAsyncInsertions();
1529983Sstever@gmail.com}
1539983Sstever@gmail.com
1549983Sstever@gmail.comvoid
1559983Sstever@gmail.comGlobalSyncEvent::process()
1569983Sstever@gmail.com{
1579983Sstever@gmail.com    if (repeat) {
1589983Sstever@gmail.com        schedule(curTick() + repeat);
1599983Sstever@gmail.com    }
1609983Sstever@gmail.com}
1619983Sstever@gmail.com
1629983Sstever@gmail.comconst char *
1639983Sstever@gmail.comGlobalSyncEvent::description() const
1649983Sstever@gmail.com{
1659983Sstever@gmail.com    return "GlobalSyncEvent";
1669983Sstever@gmail.com}
167