Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
split a piece of src/mc/ModelChecker.cpp into src/mc/remote/EventLoop.cpp
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Wed, 6 May 2020 14:49:06 +0000 (16:49 +0200)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Wed, 6 May 2020 14:49:06 +0000 (16:49 +0200)
src/mc/ModelChecker.cpp
src/mc/ModelChecker.hpp
src/mc/remote/EventLoop.cpp [new file with mode: 0644]
src/mc/remote/EventLoop.hpp [new file with mode: 0644]
tools/cmake/DefinePackages.cmake

index f49da37..2cf8ac8 100644 (file)
@@ -34,30 +34,11 @@ namespace mc {
 
 ModelChecker::ModelChecker(std::unique_ptr<RemoteClient> process) : process_(std::move(process)) {}
 
-ModelChecker::~ModelChecker()
-{
-  if (socket_event_ != nullptr)
-    event_free(socket_event_);
-  if (signal_event_ != nullptr)
-    event_free(signal_event_);
-  if (base_ != nullptr)
-    event_base_free(base_);
-}
-
 void ModelChecker::start()
 {
-  base_ = event_base_new();
-  event_callback_fn event_callback = [](evutil_socket_t fd, short events, void *arg)
-  {
+  event_loop_.start(process_->get_channel().get_socket(), [](evutil_socket_t fd, short events, void* arg) {
     ((ModelChecker *)arg)->handle_events(fd, events);
-  };
-  socket_event_ = event_new(base_, process_->get_channel().get_socket(), EV_READ | EV_PERSIST, event_callback, this);
-  event_add(socket_event_, NULL);
-  signal_event_ = event_new(base_,
-                            SIGCHLD,
-                            EV_SIGNAL|EV_PERSIST,
-                            event_callback, this);
-  event_add(signal_event_, NULL);
+  });
 
   XBT_DEBUG("Waiting for the model-checked process");
   int status;
@@ -257,9 +238,9 @@ void ModelChecker::handle_events(int fd, short events)
     ssize_t size = process_->get_channel().receive(buffer, sizeof(buffer), false);
     if (size == -1 && errno != EAGAIN)
       throw simgrid::xbt::errno_error();
-    if (not handle_message(buffer, size)) {
-      event_base_loopbreak(base_);
-    }
+
+    if (not handle_message(buffer, size))
+      event_loop_.break_loop();
   }
   else if (events == EV_SIGNAL) {
     on_signal(fd);
@@ -331,7 +312,7 @@ void ModelChecker::wait_for_requests()
 {
   this->resume(process());
   if (this->process().running())
-    event_base_dispatch(base_);
+    event_loop_.dispatch();
 }
 
 void ModelChecker::handle_simcall(Transition const& transition)
@@ -344,7 +325,7 @@ void ModelChecker::handle_simcall(Transition const& transition)
   this->process_->get_channel().send(m);
   this->process_->clear_cache();
   if (this->process_->running())
-    event_base_dispatch(base_);
+    event_loop_.dispatch();
 }
 
 bool ModelChecker::checkDeadlock()
index e6a0002..212c528 100644 (file)
@@ -6,6 +6,7 @@
 #ifndef SIMGRID_MC_MODEL_CHECKER_HPP
 #define SIMGRID_MC_MODEL_CHECKER_HPP
 
+#include "src/mc/remote/EventLoop.hpp"
 #include "src/mc/sosp/PageStore.hpp"
 #include "xbt/base.h"
 
 #include <set>
 #include <string>
 
-#include <event2/event.h>
-
 namespace simgrid {
 namespace mc {
 
 /** State of the model-checker (global variables for the model checker)
  */
 class ModelChecker {
-  struct event_base* base_    = nullptr;
-  struct event* socket_event_ = nullptr;
-  struct event* signal_event_ = nullptr;
+  EventLoop event_loop_;
   /** String pool for host names */
   std::set<std::string> hostnames_;
   // This is the parent snapshot of the current state:
@@ -35,7 +32,6 @@ public:
   ModelChecker(ModelChecker const&) = delete;
   ModelChecker& operator=(ModelChecker const&) = delete;
   explicit ModelChecker(std::unique_ptr<RemoteClient> process);
-  ~ModelChecker();
 
   RemoteClient& process() { return *process_; }
   PageStore& page_store()
diff --git a/src/mc/remote/EventLoop.cpp b/src/mc/remote/EventLoop.cpp
new file mode 100644 (file)
index 0000000..6ea7232
--- /dev/null
@@ -0,0 +1,38 @@
+/* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved.          */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include "src/mc/remote/EventLoop.hpp"
+#include <sys/wait.h>
+
+simgrid::mc::EventLoop::~EventLoop()
+{
+  if (socket_event_ != nullptr)
+    event_free(socket_event_);
+  if (signal_event_ != nullptr)
+    event_free(signal_event_);
+  if (base_ != nullptr)
+    event_base_free(base_);
+}
+
+void simgrid::mc::EventLoop::start(int socket, void (*handler)(int, short, void*))
+{
+  base_ = event_base_new();
+
+  socket_event_ = event_new(base_, socket, EV_READ | EV_PERSIST, handler, this);
+  event_add(socket_event_, NULL);
+
+  signal_event_ = event_new(base_, SIGCHLD, EV_SIGNAL | EV_PERSIST, handler, this);
+  event_add(signal_event_, NULL);
+}
+
+void simgrid::mc::EventLoop::dispatch()
+{
+  event_base_dispatch(base_);
+}
+
+void simgrid::mc::EventLoop::break_loop()
+{
+  event_base_loopbreak(base_);
+}
diff --git a/src/mc/remote/EventLoop.hpp b/src/mc/remote/EventLoop.hpp
new file mode 100644 (file)
index 0000000..6cecdc0
--- /dev/null
@@ -0,0 +1,31 @@
+/* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved.          */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#ifndef SIMGRID_MC_REMOTE_EVENTLOOP_HPP
+#define SIMGRID_MC_REMOTE_EVENTLOOP_HPP
+
+#include <event2/event.h>
+#include <functional>
+
+namespace simgrid {
+namespace mc {
+
+class EventLoop {
+  struct event_base* base_    = nullptr;
+  struct event* socket_event_ = nullptr;
+  struct event* signal_event_ = nullptr;
+
+public:
+  ~EventLoop();
+
+  void start(int socket, void (*handler)(int, short, void*));
+  void dispatch();
+  void break_loop();
+};
+
+} // namespace mc
+} // namespace simgrid
+
+#endif
index 472a786..5989baa 100644 (file)
@@ -628,6 +628,8 @@ set(MC_SRC
   src/mc/remote/Channel.hpp
   src/mc/remote/Client.cpp
   src/mc/remote/Client.hpp
+  src/mc/remote/EventLoop.cpp
+  src/mc/remote/EventLoop.hpp
   src/mc/remote/RemoteClient.hpp
   src/mc/remote/RemoteClient.cpp
   src/mc/remote/RemotePtr.hpp