113063Sgabeblack@google.com/*
213063Sgabeblack@google.com * Copyright 2018 Google, Inc.
313063Sgabeblack@google.com *
413063Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
513063Sgabeblack@google.com * modification, are permitted provided that the following conditions are
613063Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
713063Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
813063Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
913063Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1013063Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1113063Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1213063Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1313063Sgabeblack@google.com * this software without specific prior written permission.
1413063Sgabeblack@google.com *
1513063Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1613063Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1713063Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1813063Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1913063Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2013063Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2113063Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2213063Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2313063Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2413063Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2513063Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2613063Sgabeblack@google.com *
2713063Sgabeblack@google.com * Authors: Gabe Black
2813063Sgabeblack@google.com */
2913063Sgabeblack@google.com
3013063Sgabeblack@google.com#ifndef __SYSTEMC_CORE_SCHED_EVENT_HH__
3113063Sgabeblack@google.com#define __SYSTEMC_CORE_SCHED_EVENT_HH__
3213063Sgabeblack@google.com
3313063Sgabeblack@google.com#include <functional>
3413144Sgabeblack@google.com#include <list>
3513063Sgabeblack@google.com
3613063Sgabeblack@google.com#include "base/types.hh"
3713063Sgabeblack@google.com
3813063Sgabeblack@google.comnamespace sc_gem5
3913063Sgabeblack@google.com{
4013063Sgabeblack@google.com
4113144Sgabeblack@google.comclass ScEvent;
4213144Sgabeblack@google.com
4313144Sgabeblack@google.comtypedef std::list<ScEvent *> ScEvents;
4413144Sgabeblack@google.com
4513063Sgabeblack@google.comclass ScEvent
4613063Sgabeblack@google.com{
4713063Sgabeblack@google.com  private:
4813063Sgabeblack@google.com    std::function<void()> work;
4913063Sgabeblack@google.com    Tick _when;
5013144Sgabeblack@google.com    ScEvents *_events;
5113144Sgabeblack@google.com    ScEvents::iterator _it;
5213063Sgabeblack@google.com
5313063Sgabeblack@google.com    friend class Scheduler;
5413063Sgabeblack@google.com
5513063Sgabeblack@google.com    void
5613144Sgabeblack@google.com    schedule(ScEvents &events, Tick w)
5713063Sgabeblack@google.com    {
5813063Sgabeblack@google.com        when(w);
5913144Sgabeblack@google.com        assert(!scheduled());
6013144Sgabeblack@google.com        _events = &events;
6113144Sgabeblack@google.com        _events->push_back(this);
6213144Sgabeblack@google.com        _it = _events->end();
6313144Sgabeblack@google.com        _it--;
6413063Sgabeblack@google.com    }
6513063Sgabeblack@google.com
6613144Sgabeblack@google.com    void
6713144Sgabeblack@google.com    deschedule()
6813144Sgabeblack@google.com    {
6913144Sgabeblack@google.com        assert(scheduled());
7013144Sgabeblack@google.com        _events->erase(_it);
7113144Sgabeblack@google.com        _events = nullptr;
7213144Sgabeblack@google.com    }
7313063Sgabeblack@google.com  public:
7413063Sgabeblack@google.com    ScEvent(std::function<void()> work) :
7513144Sgabeblack@google.com        work(work), _when(MaxTick), _events(nullptr)
7613063Sgabeblack@google.com    {}
7713063Sgabeblack@google.com
7813072Sgabeblack@google.com    ~ScEvent();
7913072Sgabeblack@google.com
8013144Sgabeblack@google.com    bool scheduled() { return _events != nullptr; }
8113144Sgabeblack@google.com    ScEvents *scheduledOn() { return _events; }
8213063Sgabeblack@google.com
8313063Sgabeblack@google.com    void when(Tick w) { _when = w; }
8413063Sgabeblack@google.com    Tick when() { return _when; }
8513063Sgabeblack@google.com
8613063Sgabeblack@google.com    void run() { deschedule(); work(); }
8713063Sgabeblack@google.com};
8813063Sgabeblack@google.com
8913063Sgabeblack@google.com} // namespace sc_gem5
9013063Sgabeblack@google.com
9113063Sgabeblack@google.com#endif // __SYSTEMC_CORE_SCHED_EVENT_HH__
92