だいぶ空いてしまった。
Event-driven programming
ソースを編集。
--- ../backup/1/src/learning_gem5/part2/hello_object.hh 2024-04-03 22:02:25.385861133 +0900 +++ src/learning_gem5/part2/hello_object.hh 2024-04-03 22:04:29.235837762 +0900 @@ -9,8 +9,12 @@ class HelloObject : public SimObject { + private: + void processEvent(); + EventFunctionWrapper event; public: HelloObject(const HelloObjectParams &p); + void startup() override; }; } // namespace gem5 --- ../backup/1/src/learning_gem5/part2/hello_object.cc 2024-03-24 16:17:08.087853809 +0900 +++ src/learning_gem5/part2/hello_object.cc 2024-04-03 22:06:04.745818640 +0900 @@ -8,9 +8,19 @@ { HelloObject::HelloObject(const HelloObjectParams ¶ms) : - SimObject(params) + SimObject(params), event([this]{processEvent();}, name()) { DPRINTF(HelloExample, "Created the hello object\n"); } +void HelloObject::processEvent() +{ + DPRINTF(HelloExample, "Hello world! Processing the event!\n"); +} + +void HelloObject::startup() +{ + schedule(event, 100); +} + } // namespace gem5
実行する。
$ scons build/X86/gem5.opt -j20 $ build/X86/gem5.opt --debug-flags=HelloExample configs/learning_gem5/part2/run_hello.py
以下のメッセージが出る。
100: hello: Hello world! Processing the event!
さらに変更を加える。
--- ../backup/2/src/learning_gem5/part2/hello_object.hh 2024-04-03 22:17:05.645691333 +0900 +++ src/learning_gem5/part2/hello_object.hh 2024-04-03 22:17:41.355683887 +0900 @@ -12,6 +12,8 @@ private: void processEvent(); EventFunctionWrapper event; + const Tick latency; + int timesLeft; public: HelloObject(const HelloObjectParams &p); void startup() override; --- ../backup/2/src/learning_gem5/part2/hello_object.cc 2024-04-03 22:17:10.005690492 +0900 +++ src/learning_gem5/part2/hello_object.cc 2024-04-03 22:19:40.035660455 +0900 @@ -8,19 +8,27 @@ { HelloObject::HelloObject(const HelloObjectParams ¶ms) : - SimObject(params), event([this]{processEvent();}, name()) + SimObject(params), event([this]{processEvent();}, name()), + latency(100), timesLeft(10) { DPRINTF(HelloExample, "Created the hello object\n"); } void HelloObject::processEvent() { - DPRINTF(HelloExample, "Hello world! Processing the event!\n"); + timesLeft--; + DPRINTF(HelloExample, "Hello world! Processing the event! %d left\n", timesLeft); + + if (timesLeft <= 0) { + DPRINTF(HelloExample, "Done firing!\n"); + } else { + schedule(event, curTick() + latency); + } } void HelloObject::startup() { - schedule(event, 100); + schedule(event, latency); } } // namespace gem5
実行する。
$ scons build/X86/gem5.opt -j20 $ build/X86/gem5.opt --debug-flags=HelloExample configs/learning_gem5/part2/run_hello.py
以下のメッセージが出る。
src/sim/simulate.cc:199: info: Entering event queue @ 0. Starting simulation... 100: hello: Hello world! Processing the event! 9 left 200: hello: Hello world! Processing the event! 8 left 300: hello: Hello world! Processing the event! 7 left 400: hello: Hello world! Processing the event! 6 left 500: hello: Hello world! Processing the event! 5 left 600: hello: Hello world! Processing the event! 4 left 700: hello: Hello world! Processing the event! 3 left 800: hello: Hello world! Processing the event! 2 left 900: hello: Hello world! Processing the event! 1 left 1000: hello: Hello world! Processing the event! 0 left 1000: hello: Done firing!