From b7b9fb781045188e65beb5c1dfc391a2d21e5472 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Thu, 7 Mar 2019 16:50:38 +0100 Subject: [PATCH] Use references for parameters of type std::function. --- include/simgrid/Exception.hpp | 2 +- src/bindings/java/JavaContext.cpp | 4 ++-- src/bindings/java/JavaContext.hpp | 4 ++-- src/kernel/context/Context.cpp | 4 ++-- src/kernel/context/Context.hpp | 8 ++++---- src/kernel/context/ContextBoost.cpp | 4 ++-- src/kernel/context/ContextBoost.hpp | 4 ++-- src/kernel/context/ContextRaw.cpp | 4 ++-- src/kernel/context/ContextRaw.hpp | 4 ++-- src/kernel/context/ContextSwapped.cpp | 2 +- src/kernel/context/ContextSwapped.hpp | 2 +- src/kernel/context/ContextThread.cpp | 4 ++-- src/kernel/context/ContextThread.hpp | 12 ++++++------ src/kernel/context/ContextUnix.cpp | 4 ++-- src/kernel/context/ContextUnix.hpp | 4 ++-- src/simgrid/Exception.cpp | 2 +- src/simix/ActorImpl.cpp | 12 ++++++------ src/simix/ActorImpl.hpp | 15 ++++++++------- 18 files changed, 48 insertions(+), 47 deletions(-) diff --git a/include/simgrid/Exception.hpp b/include/simgrid/Exception.hpp index 0f3a5e3a6d..08ccf09fa3 100644 --- a/include/simgrid/Exception.hpp +++ b/include/simgrid/Exception.hpp @@ -197,7 +197,7 @@ public: const char* what() const noexcept { return msg_.c_str(); } static void do_throw(); - static bool try_n_catch(std::function try_block); + static bool try_n_catch(const std::function& try_block); private: std::string msg_ = std::string("Actor killed."); diff --git a/src/bindings/java/JavaContext.cpp b/src/bindings/java/JavaContext.cpp index e822571a61..5d500af7b2 100644 --- a/src/bindings/java/JavaContext.cpp +++ b/src/bindings/java/JavaContext.cpp @@ -34,7 +34,7 @@ JavaContextFactory::JavaContextFactory() : ContextFactory() JavaContextFactory::~JavaContextFactory()=default; -JavaContext* JavaContextFactory::create_context(std::function code, smx_actor_t actor) +JavaContext* JavaContextFactory::create_context(std::function&& code, smx_actor_t actor) { return this->new_context(std::move(code), actor); } @@ -44,7 +44,7 @@ void JavaContextFactory::run_all() SerialThreadContext::run_all(); } -JavaContext::JavaContext(std::function code, smx_actor_t actor) +JavaContext::JavaContext(std::function&& code, smx_actor_t actor) : SerialThreadContext(std::move(code), actor, false /* not maestro */) { /* ThreadContext already does all we need */ diff --git a/src/bindings/java/JavaContext.hpp b/src/bindings/java/JavaContext.hpp index 0a750b2a68..8eef41b773 100644 --- a/src/bindings/java/JavaContext.hpp +++ b/src/bindings/java/JavaContext.hpp @@ -32,7 +32,7 @@ public: JNIEnv* jenv_ = nullptr; friend class JavaContextFactory; - JavaContext(std::function code, smx_actor_t actor); + JavaContext(std::function&& code, smx_actor_t actor); void start_hook() override; void stop_hook() override; @@ -42,7 +42,7 @@ class JavaContextFactory : public simgrid::kernel::context::ContextFactory { public: JavaContextFactory(); ~JavaContextFactory() override; - JavaContext* create_context(std::function code, smx_actor_t actor) override; + JavaContext* create_context(std::function&& code, smx_actor_t actor) override; void run_all() override; }; diff --git a/src/kernel/context/Context.cpp b/src/kernel/context/Context.cpp index dedbf2c147..16517db16a 100644 --- a/src/kernel/context/Context.cpp +++ b/src/kernel/context/Context.cpp @@ -47,13 +47,13 @@ Context* ContextFactory::attach(smx_actor_t) "Try using --cfg=contexts/factory:thread instead.\n"); } -Context* ContextFactory::create_maestro(std::function, smx_actor_t) +Context* ContextFactory::create_maestro(std::function&&, smx_actor_t) { xbt_die("Cannot create_maestro with this ContextFactory.\n" "Try using --cfg=contexts/factory:thread instead.\n"); } -Context::Context(std::function code, smx_actor_t actor) : code_(std::move(code)), actor_(actor) +Context::Context(std::function&& code, smx_actor_t actor) : code_(std::move(code)), actor_(actor) { /* If no function was provided, this is the context for maestro * and we should set it as the current context */ diff --git a/src/kernel/context/Context.hpp b/src/kernel/context/Context.hpp index c02dd39ea4..ae50c28a81 100644 --- a/src/kernel/context/Context.hpp +++ b/src/kernel/context/Context.hpp @@ -22,12 +22,12 @@ public: ContextFactory(const ContextFactory&) = delete; ContextFactory& operator=(const ContextFactory&) = delete; virtual ~ContextFactory(); - virtual Context* create_context(std::function code, smx_actor_t actor) = 0; + virtual Context* create_context(std::function&& code, smx_actor_t actor) = 0; /** Turn the current thread into a simulation context */ virtual Context* attach(smx_actor_t actor); /** Turn the current thread into maestro (the old maestro becomes a regular actor) */ - virtual Context* create_maestro(std::function code, smx_actor_t actor); + virtual Context* create_maestro(std::function&& code, smx_actor_t actor); virtual void run_all() = 0; @@ -50,7 +50,7 @@ class XBT_PUBLIC Context { public: bool iwannadie = false; - Context(std::function code, smx_actor_t actor); + Context(std::function&& code, smx_actor_t actor); Context(const Context&) = delete; Context& operator=(const Context&) = delete; virtual ~Context(); @@ -72,7 +72,7 @@ public: class XBT_PUBLIC AttachContext : public Context { public: - AttachContext(std::function code, smx_actor_t actor) : Context(std::move(code), actor) {} + AttachContext(std::function&& code, smx_actor_t actor) : Context(std::move(code), actor) {} AttachContext(const AttachContext&) = delete; AttachContext& operator=(const AttachContext&) = delete; ~AttachContext() override; diff --git a/src/kernel/context/ContextBoost.cpp b/src/kernel/context/ContextBoost.cpp index 95743c6f2b..ee99def4c2 100644 --- a/src/kernel/context/ContextBoost.cpp +++ b/src/kernel/context/ContextBoost.cpp @@ -15,14 +15,14 @@ namespace kernel { namespace context { // BoostContextFactory -smx_context_t BoostContextFactory::create_context(std::function code, smx_actor_t actor) +smx_context_t BoostContextFactory::create_context(std::function&& code, smx_actor_t actor) { return this->new_context(std::move(code), actor, this); } // BoostContext -BoostContext::BoostContext(std::function code, smx_actor_t actor, SwappedContextFactory* factory) +BoostContext::BoostContext(std::function&& code, smx_actor_t actor, SwappedContextFactory* factory) : SwappedContext(std::move(code), actor, factory) { diff --git a/src/kernel/context/ContextBoost.hpp b/src/kernel/context/ContextBoost.hpp index a078407592..cac896cab9 100644 --- a/src/kernel/context/ContextBoost.hpp +++ b/src/kernel/context/ContextBoost.hpp @@ -32,7 +32,7 @@ namespace context { /** @brief Userspace context switching implementation based on Boost.Context */ class BoostContext : public SwappedContext { public: - BoostContext(std::function code, smx_actor_t actor, SwappedContextFactory* factory); + BoostContext(std::function&& code, smx_actor_t actor, SwappedContextFactory* factory); ~BoostContext() override; void swap_into(SwappedContext* to) override; @@ -54,7 +54,7 @@ private: class BoostContextFactory : public SwappedContextFactory { public: - Context* create_context(std::function code, smx_actor_t actor) override; + Context* create_context(std::function&& code, smx_actor_t actor) override; }; }}} // namespace diff --git a/src/kernel/context/ContextRaw.cpp b/src/kernel/context/ContextRaw.cpp index 5e178afd58..520d8343a1 100644 --- a/src/kernel/context/ContextRaw.cpp +++ b/src/kernel/context/ContextRaw.cpp @@ -188,14 +188,14 @@ namespace context { // RawContextFactory -Context* RawContextFactory::create_context(std::function code, smx_actor_t actor) +Context* RawContextFactory::create_context(std::function&& code, smx_actor_t actor) { return this->new_context(std::move(code), actor, this); } // RawContext -RawContext::RawContext(std::function code, smx_actor_t actor, SwappedContextFactory* factory) +RawContext::RawContext(std::function&& code, smx_actor_t actor, SwappedContextFactory* factory) : SwappedContext(std::move(code), actor, factory) { if (has_code()) { diff --git a/src/kernel/context/ContextRaw.hpp b/src/kernel/context/ContextRaw.hpp index 2810c17948..074918050c 100644 --- a/src/kernel/context/ContextRaw.hpp +++ b/src/kernel/context/ContextRaw.hpp @@ -26,7 +26,7 @@ namespace context { */ class RawContext : public SwappedContext { public: - RawContext(std::function code, smx_actor_t actor, SwappedContextFactory* factory); + RawContext(std::function&& code, smx_actor_t actor, SwappedContextFactory* factory); void swap_into(SwappedContext* to) override; @@ -39,7 +39,7 @@ private: class RawContextFactory : public SwappedContextFactory { public: - Context* create_context(std::function code, smx_actor_t actor) override; + Context* create_context(std::function&& code, smx_actor_t actor) override; }; }}} // namespace diff --git a/src/kernel/context/ContextSwapped.cpp b/src/kernel/context/ContextSwapped.cpp index 9aabd63108..2fd0f477a9 100644 --- a/src/kernel/context/ContextSwapped.cpp +++ b/src/kernel/context/ContextSwapped.cpp @@ -48,7 +48,7 @@ SwappedContextFactory::~SwappedContextFactory() delete parmap_; } -SwappedContext::SwappedContext(std::function code, smx_actor_t actor, SwappedContextFactory* factory) +SwappedContext::SwappedContext(std::function&& code, smx_actor_t actor, SwappedContextFactory* factory) : Context(std::move(code), actor), factory_(factory) { // Save maestro (=context created first) in preparation for run_all diff --git a/src/kernel/context/ContextSwapped.hpp b/src/kernel/context/ContextSwapped.hpp index f7eedcd0ff..9e8796d53f 100644 --- a/src/kernel/context/ContextSwapped.hpp +++ b/src/kernel/context/ContextSwapped.hpp @@ -37,7 +37,7 @@ private: class SwappedContext : public Context { public: - SwappedContext(std::function code, smx_actor_t get_actor, SwappedContextFactory* factory); + SwappedContext(std::function&& code, smx_actor_t get_actor, SwappedContextFactory* factory); SwappedContext(const SwappedContext&) = delete; SwappedContext& operator=(const SwappedContext&) = delete; virtual ~SwappedContext(); diff --git a/src/kernel/context/ContextThread.cpp b/src/kernel/context/ContextThread.cpp index 17a3df7340..7914f29e76 100644 --- a/src/kernel/context/ContextThread.cpp +++ b/src/kernel/context/ContextThread.cpp @@ -34,7 +34,7 @@ ThreadContextFactory::~ThreadContextFactory() ParallelThreadContext::finalize(); } -ThreadContext* ThreadContextFactory::create_context(std::function code, smx_actor_t actor, bool maestro) +ThreadContext* ThreadContextFactory::create_context(std::function&& code, smx_actor_t actor, bool maestro) { if (parallel_) return this->new_context(std::move(code), actor, maestro); @@ -55,7 +55,7 @@ void ThreadContextFactory::run_all() // ThreadContext -ThreadContext::ThreadContext(std::function code, smx_actor_t actor, bool maestro) +ThreadContext::ThreadContext(std::function&& code, smx_actor_t actor, bool maestro) : AttachContext(std::move(code), actor), is_maestro_(maestro) { /* If the user provided a function for the actor then use it */ diff --git a/src/kernel/context/ContextThread.hpp b/src/kernel/context/ContextThread.hpp index 22e381484c..63cf956a38 100644 --- a/src/kernel/context/ContextThread.hpp +++ b/src/kernel/context/ContextThread.hpp @@ -20,7 +20,7 @@ namespace context { class XBT_PUBLIC ThreadContext : public AttachContext { public: - ThreadContext(std::function code, smx_actor_t actor, bool maestro); + ThreadContext(std::function&& code, smx_actor_t actor, bool maestro); ThreadContext(const ThreadContext&) = delete; ThreadContext& operator=(const ThreadContext&) = delete; ~ThreadContext() override; @@ -53,7 +53,7 @@ private: class XBT_PUBLIC SerialThreadContext : public ThreadContext { public: - SerialThreadContext(std::function code, smx_actor_t actor, bool maestro) + SerialThreadContext(std::function&& code, smx_actor_t actor, bool maestro) : ThreadContext(std::move(code), actor, maestro) { } @@ -63,7 +63,7 @@ public: class ParallelThreadContext : public ThreadContext { public: - ParallelThreadContext(std::function code, smx_actor_t actor, bool maestro) + ParallelThreadContext(std::function&& code, smx_actor_t actor, bool maestro) : ThreadContext(std::move(code), actor, maestro) { } @@ -85,7 +85,7 @@ public: ThreadContextFactory(const ThreadContextFactory&) = delete; ThreadContextFactory& operator=(const ThreadContextFactory&) = delete; ~ThreadContextFactory() override; - ThreadContext* create_context(std::function code, smx_actor_t actor) override + ThreadContext* create_context(std::function&& code, smx_actor_t actor) override { bool maestro = not code; return create_context(std::move(code), actor, maestro); @@ -94,7 +94,7 @@ public: // Optional methods: ThreadContext* attach(smx_actor_t actor) override { return create_context(std::function(), actor, false); } - ThreadContext* create_maestro(std::function code, smx_actor_t actor) override + ThreadContext* create_maestro(std::function&& code, smx_actor_t actor) override { return create_context(std::move(code), actor, true); } @@ -102,7 +102,7 @@ public: private: bool parallel_; - ThreadContext* create_context(std::function code, smx_actor_t actor, bool maestro); + ThreadContext* create_context(std::function&& code, smx_actor_t actor, bool maestro); }; }}} // namespace diff --git a/src/kernel/context/ContextUnix.cpp b/src/kernel/context/ContextUnix.cpp index be6fbc9075..88b2bdb525 100644 --- a/src/kernel/context/ContextUnix.cpp +++ b/src/kernel/context/ContextUnix.cpp @@ -56,7 +56,7 @@ namespace kernel { namespace context { // UContextFactory -Context* UContextFactory::create_context(std::function code, smx_actor_t actor) +Context* UContextFactory::create_context(std::function&& code, smx_actor_t actor) { return new_context(std::move(code), actor, this); } @@ -64,7 +64,7 @@ Context* UContextFactory::create_context(std::function code, smx_actor_t // UContext -UContext::UContext(std::function code, smx_actor_t actor, SwappedContextFactory* factory) +UContext::UContext(std::function&& code, smx_actor_t actor, SwappedContextFactory* factory) : SwappedContext(std::move(code), actor, factory) { /* if the user provided a function for the actor then use it. If not, nothing to do for maestro. */ diff --git a/src/kernel/context/ContextUnix.hpp b/src/kernel/context/ContextUnix.hpp index a3cc5e1568..96a19143c9 100644 --- a/src/kernel/context/ContextUnix.hpp +++ b/src/kernel/context/ContextUnix.hpp @@ -25,7 +25,7 @@ namespace context { class UContext : public SwappedContext { public: - UContext(std::function code, smx_actor_t actor, SwappedContextFactory* factory); + UContext(std::function&& code, smx_actor_t actor, SwappedContextFactory* factory); void swap_into(SwappedContext* to) override; @@ -35,7 +35,7 @@ private: class UContextFactory : public SwappedContextFactory { public: - Context* create_context(std::function code, smx_actor_t actor) override; + Context* create_context(std::function&& code, smx_actor_t actor) override; }; }}} // namespace diff --git a/src/simgrid/Exception.cpp b/src/simgrid/Exception.cpp index cd4fc1fa22..d59d4874a1 100644 --- a/src/simgrid/Exception.cpp +++ b/src/simgrid/Exception.cpp @@ -16,7 +16,7 @@ void ForcefulKillException::do_throw() throw ForcefulKillException(); } -bool ForcefulKillException::try_n_catch(std::function try_block) +bool ForcefulKillException::try_n_catch(const std::function& try_block) { bool res; try { diff --git a/src/simix/ActorImpl.cpp b/src/simix/ActorImpl.cpp index 2d5dcaf14f..a9e3b1c2ca 100644 --- a/src/simix/ActorImpl.cpp +++ b/src/simix/ActorImpl.cpp @@ -456,7 +456,7 @@ ActorImplPtr ActorImpl::init(std::string name, s4u::Host* host) return ActorImplPtr(actor); } -ActorImpl* ActorImpl::start(simix::ActorCode code) +ActorImpl* ActorImpl::start(const simix::ActorCode& code) { xbt_assert(code && host_ != nullptr, "Invalid parameters"); @@ -469,7 +469,7 @@ ActorImpl* ActorImpl::start(simix::ActorCode code) this->code = code; XBT_VERB("Create context %s", get_cname()); - context_ = simix_global->context_factory->create_context(std::move(code), this); + context_ = simix_global->context_factory->create_context(simix::ActorCode(code), this); XBT_DEBUG("Start context '%s'", get_cname()); @@ -484,7 +484,7 @@ ActorImpl* ActorImpl::start(simix::ActorCode code) return this; } -ActorImplPtr ActorImpl::create(std::string name, simix::ActorCode code, void* data, s4u::Host* host, +ActorImplPtr ActorImpl::create(std::string name, const simix::ActorCode& code, void* data, s4u::Host* host, std::unordered_map* properties, ActorImpl* parent_actor) { XBT_DEBUG("Start actor %s@'%s'", name.c_str(), host->get_cname()); @@ -503,12 +503,12 @@ ActorImplPtr ActorImpl::create(std::string name, simix::ActorCode code, void* da for (auto const& kv : *properties) actor->set_property(kv.first, kv.second); - actor->start(std::move(code)); + actor->start(code); return actor; } -void create_maestro(simix::ActorCode code) +void create_maestro(const std::function& code) { /* Create maestro actor and initialize it */ ActorImpl* maestro = new ActorImpl(xbt::string(""), /*host*/ nullptr); @@ -516,7 +516,7 @@ void create_maestro(simix::ActorCode code) if (not code) { maestro->context_ = simix_global->context_factory->create_context(simix::ActorCode(), maestro); } else { - maestro->context_ = simix_global->context_factory->create_maestro(code, maestro); + maestro->context_ = simix_global->context_factory->create_maestro(simix::ActorCode(code), maestro); } maestro->simcall.issuer = maestro; diff --git a/src/simix/ActorImpl.hpp b/src/simix/ActorImpl.hpp index 3071474437..18ee93888c 100644 --- a/src/simix/ActorImpl.hpp +++ b/src/simix/ActorImpl.hpp @@ -106,9 +106,9 @@ public: s4u::Actor* ciface() { return &piface_; } ActorImplPtr init(std::string name, s4u::Host* host); - ActorImpl* start(simix::ActorCode code); + ActorImpl* start(const simix::ActorCode& code); - static ActorImplPtr create(std::string name, simix::ActorCode code, void* data, s4u::Host* host, + static ActorImplPtr create(std::string name, const simix::ActorCode& code, void* data, s4u::Host* host, std::unordered_map* properties, ActorImpl* parent_actor); static ActorImplPtr attach(std::string name, void* data, s4u::Host* host, std::unordered_map* properties); @@ -142,10 +142,11 @@ public: bool daemon_ = false; ProcessArg() = default; - explicit ProcessArg(std::string name, std::function code, void* data, s4u::Host* host, double kill_time, - std::shared_ptr> properties, bool auto_restart) + explicit ProcessArg(std::string name, const std::function& code, void* data, s4u::Host* host, + double kill_time, std::shared_ptr> properties, + bool auto_restart) : name(name) - , code(std::move(code)) + , code(code) , data(data) , host(host) , kill_time(kill_time) @@ -156,7 +157,7 @@ public: explicit ProcessArg(s4u::Host* host, ActorImpl* actor) : name(actor->get_name()) - , code(std::move(actor->code)) + , code(actor->code) , data(actor->get_user_data()) , host(host) , kill_time(actor->get_kill_time()) @@ -172,7 +173,7 @@ typedef boost::intrusive::list> SynchroList; -XBT_PUBLIC void create_maestro(std::function code); +XBT_PUBLIC void create_maestro(const std::function& code); } // namespace actor } // namespace kernel } // namespace simgrid -- 2.20.1