From 02de96cb808f6e543070a446f8c77e74b4ef8dad Mon Sep 17 00:00:00 2001 From: Gabe Black Date: Fri, 14 Aug 2020 00:46:45 -0700 Subject: [PATCH] dev: Use lambdas instead of the Callback type for serial devices. Issue-on: https://gem5.atlassian.net/browse/GEM5-698 Change-Id: Idb87fa0b90d14981fd61f997285f61b2ef304227 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/32647 Reviewed-by: Andreas Sandberg Maintainer: Gabe Black Tested-by: kokoro --- src/dev/serial/serial.cc | 13 ++++++------- src/dev/serial/serial.hh | 9 +++++---- src/dev/serial/uart.cc | 8 +++----- src/dev/serial/uart.hh | 3 --- src/dev/virtio/console.cc | 4 ++-- src/dev/virtio/console.hh | 2 -- 6 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/dev/serial/serial.cc b/src/dev/serial/serial.cc index ac1f261b0..366c3887a 100644 --- a/src/dev/serial/serial.cc +++ b/src/dev/serial/serial.cc @@ -41,8 +41,7 @@ #include "params/SerialDevice.hh" #include "params/SerialNullDevice.hh" -SerialDevice::SerialDevice(const SerialDeviceParams *p) - : SimObject(p), interfaceCallback(nullptr) +SerialDevice::SerialDevice(const SerialDeviceParams *p) : SimObject(p) { } @@ -51,14 +50,14 @@ SerialDevice::~SerialDevice() } void -SerialDevice::regInterfaceCallback(Callback *c) +SerialDevice::regInterfaceCallback(const std::function &callback) { // This can happen if the user has connected multiple UARTs to the // same terminal. In that case, each of them tries to register // callbacks. - if (interfaceCallback) - fatal("A UART has already been associated with this device.\n"); - interfaceCallback = c; + fatal_if(interfaceCallback, + "A UART has already been associated with this device."); + interfaceCallback = callback; } void @@ -67,7 +66,7 @@ SerialDevice::notifyInterface() assert(dataAvailable()); // Registering a callback is optional. if (interfaceCallback) - interfaceCallback->process(); + interfaceCallback(); } diff --git a/src/dev/serial/serial.hh b/src/dev/serial/serial.hh index 2ea145d47..838c0ab51 100644 --- a/src/dev/serial/serial.hh +++ b/src/dev/serial/serial.hh @@ -38,7 +38,8 @@ #ifndef __DEV_SERIAL_HH__ #define __DEV_SERIAL_HH__ -#include "base/callback.hh" +#include + #include "sim/sim_object.hh" struct SerialDeviceParams; @@ -103,9 +104,9 @@ class SerialDevice : public SimObject * method. The interface layer may use this method to register a * callback that informs it of pending data. * - * @param c Callback instance from interface layer. + * @param c Callback from interface layer. */ - void regInterfaceCallback(Callback *c); + void regInterfaceCallback(const std::function &callback); /** * Check if there is pending data from the serial device. @@ -136,7 +137,7 @@ class SerialDevice : public SimObject private: /** Currently regisxtered host interface layer callback */ - Callback *interfaceCallback; + std::function interfaceCallback; }; /** diff --git a/src/dev/serial/uart.cc b/src/dev/serial/uart.cc index 098808c51..3e9131c48 100644 --- a/src/dev/serial/uart.cc +++ b/src/dev/serial/uart.cc @@ -32,13 +32,11 @@ #include "dev/serial/uart.hh" -Uart::Uart(const Params *p, Addr pio_size) - : BasicPioDevice(p, pio_size), - platform(p->platform), device(p->device), - callbackDataAvail(this) +Uart::Uart(const Params *p, Addr pio_size) : + BasicPioDevice(p, pio_size), platform(p->platform), device(p->device) { status = 0; // setup serial device callbacks - device->regInterfaceCallback(&callbackDataAvail); + device->regInterfaceCallback([this]() { dataAvailable(); }); } diff --git a/src/dev/serial/uart.hh b/src/dev/serial/uart.hh index 14ce44ee4..21ea5789e 100644 --- a/src/dev/serial/uart.hh +++ b/src/dev/serial/uart.hh @@ -70,9 +70,6 @@ class Uart : public BasicPioDevice * @return interrupt status */ bool intStatus() { return status ? true : false; } - - protected: - MakeCallback callbackDataAvail; }; #endif // __UART_HH__ diff --git a/src/dev/virtio/console.cc b/src/dev/virtio/console.cc index 5aeceb018..1bb6adaba 100644 --- a/src/dev/virtio/console.cc +++ b/src/dev/virtio/console.cc @@ -45,7 +45,7 @@ VirtIOConsole::VirtIOConsole(Params *params) : VirtIODeviceBase(params, ID_CONSOLE, sizeof(Config), F_SIZE), qRecv(params->system->physProxy, byteOrder, params->qRecvSize, *this), qTrans(params->system->physProxy, byteOrder, params->qTransSize, *this), - device(*params->device), callbackDataAvail(qRecv) + device(*params->device) { registerQueue(qRecv); registerQueue(qTrans); @@ -53,7 +53,7 @@ VirtIOConsole::VirtIOConsole(Params *params) config.cols = 80; config.rows = 24; - device.regInterfaceCallback(&callbackDataAvail); + device.regInterfaceCallback([this]() { qRecv.trySend(); }); } diff --git a/src/dev/virtio/console.hh b/src/dev/virtio/console.hh index 369158501..d60bc66c3 100644 --- a/src/dev/virtio/console.hh +++ b/src/dev/virtio/console.hh @@ -148,8 +148,6 @@ class VirtIOConsole : public VirtIODeviceBase protected: SerialDevice &device; - MakeCallback callbackDataAvail; }; #endif // __DEV_VIRTIO_CONSOLE_HH__ -- 2.30.2