scheduler.hh revision 12953
1/* 2 * Copyright 2018 Google, Inc. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer; 8 * redistributions in binary form must reproduce the above copyright 9 * notice, this list of conditions and the following disclaimer in the 10 * documentation and/or other materials provided with the distribution; 11 * neither the name of the copyright holders nor the names of its 12 * contributors may be used to endorse or promote products derived from 13 * this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * Authors: Gabe Black 28 */ 29 30#ifndef __SYSTEMC_CORE_SCHEDULER_HH__ 31#define __SYSTEMC_CORE_SCHEDULER_HH__ 32 33#include "systemc/core/list.hh" 34#include "systemc/core/process.hh" 35 36namespace sc_gem5 37{ 38 39typedef NodeList<Process> ProcessList; 40 41class Scheduler 42{ 43 public: 44 Scheduler(); 45 46 uint64_t numCycles() { return _numCycles; } 47 Process *current() { return _current; } 48 49 // Run the initialization phase. 50 void initialize(); 51 52 // Run delta cycles until time needs to advance. 53 void runCycles(); 54 55 // Put a process on the list of processes to be initialized. 56 void init(Process *p) { initList.pushLast(p); } 57 58 // Run the next process, if there is one. 59 void yield(); 60 61 // Put a process on the ready list. 62 void 63 ready(Process *p) 64 { 65 // Clump methods together to minimize context switching. 66 if (p->procKind() == ::sc_core::SC_METHOD_PROC_) 67 readyList.pushFirst(p); 68 else 69 readyList.pushLast(p); 70 } 71 72 // Run the given process immediately, preempting whatever may be running. 73 void 74 runNow(Process *p) 75 { 76 // If a process is running, schedule it/us to run again. 77 if (_current) 78 readyList.pushFirst(_current); 79 // Schedule p to run first. 80 readyList.pushFirst(p); 81 yield(); 82 } 83 84 private: 85 uint64_t _numCycles; 86 87 Process *_current; 88 89 ProcessList initList; 90 ProcessList readyList; 91 92 void evaluate(); 93 void update(); 94 void delta(); 95}; 96 97extern Scheduler scheduler; 98 99} // namespace sc_gem5 100 101#endif // __SYSTEMC_CORE_SCHEDULER_H__ 102