Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement a (cheap) ActorImpl::is_maestro()
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Wed, 22 Dec 2021 00:39:09 +0000 (01:39 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Wed, 22 Dec 2021 10:09:23 +0000 (11:09 +0100)
src/kernel/actor/ActorImpl.cpp
src/kernel/actor/ActorImpl.hpp
src/kernel/context/Context.cpp
src/kernel/context/Context.hpp
src/kernel/context/ContextSwapped.cpp
src/kernel/context/ContextThread.cpp
src/kernel/context/ContextThread.hpp

index f293ca0..52fd3cb 100644 (file)
@@ -128,6 +128,12 @@ void ActorImpl::detach()
   context->attach_stop();
 }
 
+/** Whether this actor is actually maestro */
+bool ActorImpl::is_maestro() const
+{
+  return context_->is_maestro();
+}
+
 void ActorImpl::cleanup_from_simix()
 {
   auto* engine = EngineImpl::get_instance();
index b5ae3c0..a4b11b7 100644 (file)
@@ -55,6 +55,7 @@ public:
   aid_t get_ppid() const { return ppid_; }
   void set_ppid(aid_t ppid) { ppid_ = ppid; }
   bool is_daemon() const { return daemon_; } /** Whether this actor has been daemonized */
+  bool is_maestro() const;                   /** Whether this actor is actually maestro (rather cheap call) */
   bool has_to_auto_restart() const { return auto_restart_; }
   void set_auto_restart(bool autorestart) { auto_restart_ = autorestart; }
   void set_stacksize(unsigned stacksize) { stacksize_ = stacksize; }
index e296e64..efd68ab 100644 (file)
@@ -122,11 +122,11 @@ Context* ContextFactory::create_maestro(std::function<void()>&&, actor::ActorImp
     "Try using --cfg=contexts/factory:thread instead.\n");
 }
 
-Context::Context(std::function<void()>&& code, actor::ActorImpl* actor) : code_(std::move(code)), actor_(actor)
+Context::Context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
+    : code_(std::move(code)), actor_(actor), is_maestro_(maestro)
 {
-  /* If no function was provided, this is the context for maestro
-   * and we should set it as the current context */
-  if (not has_code())
+  /* If we are creating maestro, we should set it as the current context */
+  if (maestro)
     set_current(this);
 }
 
index bd3e09a..a389566 100644 (file)
@@ -52,6 +52,7 @@ class XBT_PUBLIC Context {
   std::function<void()> code_;
   actor::ActorImpl* actor_ = nullptr;
   bool iwannadie_          = false;
+  bool is_maestro_;
   void declare_context(std::size_t size);
 
 public:
@@ -59,13 +60,14 @@ public:
   static int install_sigsegv_stack(stack_t* old_stack, bool enable);
 #endif
 
-  Context(std::function<void()>&& code, actor::ActorImpl* actor);
+  Context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro);
   Context(const Context&) = delete;
   Context& operator=(const Context&) = delete;
   virtual ~Context();
 
   bool wannadie() const { return iwannadie_; }
   void set_wannadie(bool value = true) { iwannadie_ = value; }
+  bool is_maestro() const { return is_maestro_; }
   void operator()() const { code_(); }
   bool has_code() const { return static_cast<bool>(code_); }
   actor::ActorImpl* get_actor() const { return this->actor_; }
@@ -83,14 +85,15 @@ public:
 
 class XBT_PUBLIC AttachContext : public Context {
 public:
-  AttachContext(std::function<void()>&& code, actor::ActorImpl* actor) : Context(std::move(code), actor) {}
+  AttachContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
+      : Context(std::move(code), actor, maestro)
+  {
+  }
   AttachContext(const AttachContext&) = delete;
   AttachContext& operator=(const AttachContext&) = delete;
   ~AttachContext() override;
 
-  /** Called by the context when it is ready to give control
-   *  to the maestro.
-   */
+  /** Called by the context when it is ready to give control to the maestro */
   virtual void attach_start() = 0;
 
   /** Called by the context when it has finished its job */
index a88a5bc..8ce901e 100644 (file)
@@ -71,7 +71,7 @@ namespace context {
 thread_local SwappedContext* SwappedContext::worker_context_ = nullptr;
 
 SwappedContext::SwappedContext(std::function<void()>&& code, smx_actor_t actor, SwappedContextFactory* factory)
-    : Context(std::move(code), actor), factory_(*factory)
+    : Context(std::move(code), actor, not code /* maestro if no code */), factory_(*factory)
 {
   // Save maestro (=first created context) in preparation for run_all
   if (not is_parallel() && factory_.maestro_context_ == nullptr)
index 53c0ba8..eb2c6c0 100644 (file)
@@ -60,7 +60,7 @@ void ThreadContextFactory::run_all()
 // ThreadContext
 
 ThreadContext::ThreadContext(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
-    : AttachContext(std::move(code), actor), is_maestro_(maestro)
+    : AttachContext(std::move(code), actormaestro)
 {
   /* If the user provided a function for the actor then use it */
   if (has_code()) {
index 70f7adb..5509482 100644 (file)
@@ -29,7 +29,6 @@ public:
   void attach_start() override;
   void attach_stop() override;
 
-  bool is_maestro() const { return is_maestro_; }
   void release(); // unblock context's start()
   void wait();    // wait for context's yield()
 
@@ -40,7 +39,6 @@ private:
   xbt::OsSemaphore begin_{0};
   /** Semaphore used to schedule/unschedule (not needed when the maestro is in main, but harmless then) */
   xbt::OsSemaphore end_{0};
-  bool is_maestro_;
 
   void start();                // match a call to release()
   void yield();                // match a call to yield()