GEM5的事件驱动型
回顾创建一个SimObject的步骤
- Create a Python class for your new SimObject:创建一个.py文件。这个文件是SConscript指向的py文件。
- Implement your SimObject in C++:在相同的目录下创建.hh与.cc文件。目的是实现.py文件的SimObject.
- Register the SimObject and C++ file: 在相同目录下创建SConscript文件。目的让scons编译的时候能找到该脚本并且根据参数编译SimObject
- 编译:scons build/X86/gem5.opt
- 创建configs文件,使用自己的SimObject.
事件驱动型编程
gem5提供了用于创建简单事件的包装器功能。以processEvent()函数为事件触发时候的执行函数(不带参数,不返回值),使用EventFunctionWrapper 来封装事件。
在头文件中定义event,在构造函数中实现。event有两个参数:一个是执行的函数,一个是名称。如下:
.hh文件中:
class HelloObject : public SimObject
{
private:
void processEvent();
EventFunctionWrapper event;
public:
HelloObject(HelloObjectParams *p);
void startup();
};
.cc文件中
HelloObject::HelloObject(HelloObjectParams *params) :
SimObject(params), event([this]{processEvent();}, name())
{
DPRINTF(Hello, "Created the hello object\n");
}
所以创建对象时就会触发这个事件,执行功能函数processEvent().
通常使用schedule()函数安排事件,使之在一定的时间内触发:
schedule(Event *event, Tick when)
Schedules the event (event) to execute at time when. This function places the event into the event queue and will execute the event at tick when.
其他
event封装功能函数的时候使用lambda表达式:
如:event([this] {processEvent();} , name())
其中:[函数对象参数] {函数体},函数对象参数为this时候代表函数体内可以使用Lambda所在类中的成员变量。
持续更新…