dev: Use lambdas instead of the Callback type for serial devices.
authorGabe Black <gabeblack@google.com>
Fri, 14 Aug 2020 07:46:45 +0000 (00:46 -0700)
committerGabe Black <gabeblack@google.com>
Tue, 18 Aug 2020 19:53:59 +0000 (19:53 +0000)
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 <andreas.sandberg@arm.com>
Maintainer: Gabe Black <gabeblack@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
src/dev/serial/serial.cc
src/dev/serial/serial.hh
src/dev/serial/uart.cc
src/dev/serial/uart.hh
src/dev/virtio/console.cc
src/dev/virtio/console.hh

index ac1f261b09728e6072dc3b2c017779460039932a..366c3887a2111f4be9f095d0c820388bfbbc9966 100644 (file)
@@ -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<void()> &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();
 }
 
 
index 2ea145d47c6a388be78ba2fb535a426d9ae878f1..838c0ab51780386e6a334f18d50cba9d4d753d30 100644 (file)
@@ -38,7 +38,8 @@
 #ifndef __DEV_SERIAL_HH__
 #define __DEV_SERIAL_HH__
 
-#include "base/callback.hh"
+#include <functional>
+
 #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<void()> &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<void()> interfaceCallback;
 };
 
 /**
index 098808c51f5f753537897a42f3d5308cdb7e920c..3e9131c48627b62d29015892a59f0653752ef2f2 100644 (file)
 
 #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(); });
 }
index 14ce44ee470ecb163834c2ca9f9e75b29da9cab4..21ea5789e294e73cb394902bde726a970ad23bd8 100644 (file)
@@ -70,9 +70,6 @@ class Uart : public BasicPioDevice
      * @return interrupt status
      */
     bool intStatus() { return status ? true : false; }
-
-  protected:
-    MakeCallback<Uart, &Uart::dataAvailable> callbackDataAvail;
 };
 
 #endif // __UART_HH__
index 5aeceb018b79344c1128faf57a6aef666c2d4de6..1bb6adabacd62d26c87619adee21150216683b40 100644 (file)
@@ -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(); });
 }
 
 
index 369158501d8e9ca80a8604ec9d524200717e63e5..d60bc66c3dcdc673ceb93e2f142049963eadd357 100644 (file)
@@ -148,8 +148,6 @@ class VirtIOConsole : public VirtIODeviceBase
 
   protected:
     SerialDevice &device;
-    MakeCallback<VirtIOConsole::TermRecvQueue,
-                 &VirtIOConsole::TermRecvQueue::trySend> callbackDataAvail;
 };
 
 #endif // __DEV_VIRTIO_CONSOLE_HH__