sched_event.hh revision 13072
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>
3413063Sgabeblack@google.com
3513063Sgabeblack@google.com#include "base/types.hh"
3613063Sgabeblack@google.com
3713063Sgabeblack@google.comnamespace sc_gem5
3813063Sgabeblack@google.com{
3913063Sgabeblack@google.com
4013063Sgabeblack@google.comclass ScEvent
4113063Sgabeblack@google.com{
4213063Sgabeblack@google.com  private:
4313063Sgabeblack@google.com    std::function<void()> work;
4413063Sgabeblack@google.com    Tick _when;
4513063Sgabeblack@google.com    bool _scheduled;
4613063Sgabeblack@google.com
4713063Sgabeblack@google.com    friend class Scheduler;
4813063Sgabeblack@google.com
4913063Sgabeblack@google.com    void
5013063Sgabeblack@google.com    schedule(Tick w)
5113063Sgabeblack@google.com    {
5213063Sgabeblack@google.com        when(w);
5313063Sgabeblack@google.com        _scheduled = true;
5413063Sgabeblack@google.com    }
5513063Sgabeblack@google.com
5613063Sgabeblack@google.com    void deschedule() { _scheduled = false; }
5713063Sgabeblack@google.com  public:
5813063Sgabeblack@google.com    ScEvent(std::function<void()> work) :
5913063Sgabeblack@google.com        work(work), _when(MaxTick), _scheduled(false)
6013063Sgabeblack@google.com    {}
6113063Sgabeblack@google.com
6213072Sgabeblack@google.com    ~ScEvent();
6313072Sgabeblack@google.com
6413063Sgabeblack@google.com    bool scheduled() { return _scheduled; }
6513063Sgabeblack@google.com
6613063Sgabeblack@google.com    void when(Tick w) { _when = w; }
6713063Sgabeblack@google.com    Tick when() { return _when; }
6813063Sgabeblack@google.com
6913063Sgabeblack@google.com    void run() { deschedule(); work(); }
7013063Sgabeblack@google.com};
7113063Sgabeblack@google.com
7213063Sgabeblack@google.com} // namespace sc_gem5
7313063Sgabeblack@google.com
7413063Sgabeblack@google.com#endif // __SYSTEMC_CORE_SCHED_EVENT_HH__
75