From 268f33a4d5bc5c8eca704ce3028e4e3c283fdb02 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Wed, 9 Mar 2022 22:48:21 +0100 Subject: [PATCH] Use std::function for Comm callbacks. --- docs/source/app_s4u.rst | 4 ++-- include/simgrid/s4u/Comm.hpp | 23 +++++++++--------- include/simgrid/s4u/Engine.hpp | 3 ++- include/simgrid/s4u/Mailbox.hpp | 4 ++-- src/kernel/activity/CommImpl.cpp | 4 ++-- src/kernel/activity/CommImpl.hpp | 10 ++++---- src/kernel/activity/MailboxImpl.cpp | 5 ++-- src/kernel/activity/MailboxImpl.hpp | 6 ++--- src/kernel/actor/CommObserver.hpp | 37 ++++++++++++++++------------- src/s4u/s4u_Comm.cpp | 14 ++++++----- src/s4u/s4u_Engine.cpp | 3 ++- src/s4u/s4u_Mailbox.cpp | 4 ++-- 12 files changed, 63 insertions(+), 54 deletions(-) diff --git a/docs/source/app_s4u.rst b/docs/source/app_s4u.rst index 082c49b97c..5dd5358ca3 100644 --- a/docs/source/app_s4u.rst +++ b/docs/source/app_s4u.rst @@ -1069,7 +1069,7 @@ Receiving data .. doxygenfunction:: simgrid::s4u::Mailbox::get(double timeout) .. doxygenfunction:: simgrid::s4u::Mailbox::get_async(T **data) .. doxygenfunction:: simgrid::s4u::Mailbox::get_init() - .. doxygenfunction:: simgrid::s4u::Mailbox::iprobe(int type, bool(*match_fun)(void *, void *, kernel::activity::CommImpl *), void *data) + .. doxygenfunction:: simgrid::s4u::Mailbox::iprobe(int type, const std::function& match_fun, void *data) .. doxygenfunction:: simgrid::s4u::Mailbox::listen .. doxygenfunction:: simgrid::s4u::Mailbox::ready @@ -2176,7 +2176,7 @@ Querying info .. doxygenfunction:: simgrid::s4u::Comm::set_dst_data(void **buff) .. doxygenfunction:: simgrid::s4u::Comm::set_dst_data(void **buff, size_t size) .. doxygenfunction:: simgrid::s4u::Comm::detach() - .. doxygenfunction:: simgrid::s4u::Comm::detach(void(*clean_function)(void *)) + .. doxygenfunction:: simgrid::s4u::Comm::detach(const std::function& clean_function) .. doxygenfunction:: simgrid::s4u::Comm::set_payload_size(uint64_t bytes) .. doxygenfunction:: simgrid::s4u::Comm::set_rate(double rate) .. doxygenfunction:: simgrid::s4u::Comm::set_src_data(void *buff) diff --git a/include/simgrid/s4u/Comm.hpp b/include/simgrid/s4u/Comm.hpp index d4ea842c5f..60705438ff 100644 --- a/include/simgrid/s4u/Comm.hpp +++ b/include/simgrid/s4u/Comm.hpp @@ -32,9 +32,9 @@ class XBT_PUBLIC Comm : public Activity_T { /* FIXME: expose these elements in the API */ bool detached_ = false; - bool (*match_fun_)(void*, void*, kernel::activity::CommImpl*) = nullptr; - void (*clean_fun_)(void*) = nullptr; - void (*copy_data_function_)(kernel::activity::CommImpl*, void*, size_t) = nullptr; + std::function match_fun_; + std::function clean_fun_; + std::function copy_data_function_; Comm() = default; @@ -53,20 +53,21 @@ public: static void on_start_cb(const std::function& cb) { on_start.connect(cb); } static void on_completion_cb(const std::function& cb) { on_completion.connect(cb); } /* More callbacks */ - CommPtr set_copy_data_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t)); + CommPtr set_copy_data_callback(const std::function& callback); static void copy_buffer_callback(kernel::activity::CommImpl*, void*, size_t); static void copy_pointer_callback(kernel::activity::CommImpl*, void*, size_t); ~Comm() override; static void send(kernel::actor::ActorImpl* sender, const Mailbox* mbox, double task_size, double rate, void* src_buff, - size_t src_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), - void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data, - double timeout); + size_t src_buff_size, + const std::function& match_fun, + const std::function& copy_data_fun, + void* data, double timeout); static void recv(kernel::actor::ActorImpl* receiver, const Mailbox* mbox, void* dst_buff, size_t* dst_buff_size, - bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), - void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data, - double timeout, double rate); + const std::function& match_fun, + const std::function& copy_data_fun, + void* data, double timeout, double rate); /* "One-sided" communications. This way of communicating bypasses the mailbox and actors mechanism. It creates a * communication (vetoabled, asynchronous, or synchronous) directly between two hosts. There is really no limit on @@ -153,7 +154,7 @@ public: /** Start the comm, and ignore its result. It can be completely forgotten after that. */ Comm* detach(); /** Start the comm, and ignore its result. It can be completely forgotten after that. */ - Comm* detach(void (*clean_function)(void*)) + Comm* detach(const std::function& clean_function) { clean_fun_ = clean_function; return detach(); diff --git a/include/simgrid/s4u/Engine.hpp b/include/simgrid/s4u/Engine.hpp index 7716e47f4d..87e18151b3 100644 --- a/include/simgrid/s4u/Engine.hpp +++ b/include/simgrid/s4u/Engine.hpp @@ -200,7 +200,8 @@ public: static void set_config(const std::string& name, double value); static void set_config(const std::string& name, const std::string& value); - Engine* set_default_comm_data_copy_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t)); + Engine* + set_default_comm_data_copy_callback(const std::function& callback); /** Add a callback fired when the platform is created (ie, the xml file parsed), * right before the actual simulation starts. */ diff --git a/include/simgrid/s4u/Mailbox.hpp b/include/simgrid/s4u/Mailbox.hpp index 3070b0a6e7..cda0328d13 100644 --- a/include/simgrid/s4u/Mailbox.hpp +++ b/include/simgrid/s4u/Mailbox.hpp @@ -99,8 +99,8 @@ public: */ CommPtr put_async(void* data, uint64_t simulated_size_in_bytes); - kernel::activity::ActivityImplPtr iprobe(int type, bool (*match_fun)(void*, void*, kernel::activity::CommImpl*), - void* data); + kernel::activity::ActivityImplPtr + iprobe(int type, const std::function& match_fun, void* data); /** Blocking data transmission. * * Please note that if you send a pointer to some data, you must ensure that your data remains live during the diff --git a/src/kernel/activity/CommImpl.cpp b/src/kernel/activity/CommImpl.cpp index 08f4c316c1..a383818e10 100644 --- a/src/kernel/activity/CommImpl.cpp +++ b/src/kernel/activity/CommImpl.cpp @@ -49,9 +49,9 @@ namespace activity { xbt::signal CommImpl::on_start; xbt::signal CommImpl::on_completion; -void (*CommImpl::copy_data_callback_)(CommImpl*, void*, size_t) = &s4u::Comm::copy_pointer_callback; +std::function CommImpl::copy_data_callback_ = &s4u::Comm::copy_pointer_callback; -void CommImpl::set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t)) +void CommImpl::set_copy_data_callback(const std::function& callback) { copy_data_callback_ = callback; } diff --git a/src/kernel/activity/CommImpl.hpp b/src/kernel/activity/CommImpl.hpp index 71f44a7cb1..3912d23728 100644 --- a/src/kernel/activity/CommImpl.hpp +++ b/src/kernel/activity/CommImpl.hpp @@ -20,7 +20,7 @@ class XBT_PUBLIC CommImpl : public ActivityImpl_T { ~CommImpl() override; void cleanup_surf(); - static void (*copy_data_callback_)(CommImpl*, void*, size_t); + static std::function copy_data_callback_; double rate_ = -1.0; double size_ = 0.0; @@ -36,7 +36,7 @@ class XBT_PUBLIC CommImpl : public ActivityImpl_T { public: CommImpl() = default; - static void set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t)); + static void set_copy_data_callback(const std::function& callback); CommImpl& set_type(CommImplType type); CommImplType get_type() const { return type_; } @@ -75,11 +75,11 @@ public: void set_exception(actor::ActorImpl* issuer) override; void finish() override; - void (*clean_fun)(void*) = nullptr; /* Function to clean the detached src_buf if something goes wrong */ - bool (*match_fun)(void*, void*, CommImpl*) = nullptr; /* Filter function used by the other side. It is used when + std::function clean_fun; /* Function to clean the detached src_buf if something goes wrong */ + std::function match_fun; /* Filter function used by the other side. It is used when looking if a given communication matches my needs. For that, myself must match the expectations of the other side, too. See */ - void (*copy_data_fun)(CommImpl*, void*, size_t) = nullptr; + std::function copy_data_fun; /* Surf action data */ resource::Action* src_timeout_ = nullptr; /* Surf's actions to instrument the timeouts */ diff --git a/src/kernel/activity/MailboxImpl.cpp b/src/kernel/activity/MailboxImpl.cpp index 7af7743a77..cafb0362c3 100644 --- a/src/kernel/activity/MailboxImpl.cpp +++ b/src/kernel/activity/MailboxImpl.cpp @@ -88,7 +88,7 @@ void MailboxImpl::clear() } } -CommImplPtr MailboxImpl::iprobe(int type, bool (*match_fun)(void*, void*, CommImpl*), void* data) +CommImplPtr MailboxImpl::iprobe(int type, const std::function& match_fun, void* data) { XBT_DEBUG("iprobe from %p %p", this, &comm_queue_); @@ -123,7 +123,8 @@ CommImplPtr MailboxImpl::iprobe(int type, bool (*match_fun)(void*, void*, CommIm * @param remove_matching whether or not to clean the found object from the queue * @return The communication activity if found, nullptr otherwise */ -CommImplPtr MailboxImpl::find_matching_comm(CommImplType type, bool (*match_fun)(void*, void*, CommImpl*), +CommImplPtr MailboxImpl::find_matching_comm(CommImplType type, + const std::function& match_fun, void* this_user_data, const CommImplPtr& my_synchro, bool done, bool remove_matching) { diff --git a/src/kernel/activity/MailboxImpl.hpp b/src/kernel/activity/MailboxImpl.hpp index ce7355238d..6421973c77 100644 --- a/src/kernel/activity/MailboxImpl.hpp +++ b/src/kernel/activity/MailboxImpl.hpp @@ -57,9 +57,9 @@ public: void push_done(CommImplPtr done_comm) { done_comm_queue_.push_back(done_comm); } void remove(const CommImplPtr& comm); void clear(); - CommImplPtr iprobe(int type, bool (*match_fun)(void*, void*, CommImpl*), void* data); - CommImplPtr find_matching_comm(CommImplType type, bool (*match_fun)(void*, void*, CommImpl*), void* this_user_data, - const CommImplPtr& my_synchro, bool done, bool remove_matching); + CommImplPtr iprobe(int type, const std::function& match_fun, void* data); + CommImplPtr find_matching_comm(CommImplType type, const std::function& match_fun, + void* this_user_data, const CommImplPtr& my_synchro, bool done, bool remove_matching); bool is_permanent() const { return permanent_receiver_ != nullptr; } actor::ActorImplPtr get_permanent_receiver() const { return permanent_receiver_; } bool empty() const { return comm_queue_.empty(); } diff --git a/src/kernel/actor/CommObserver.hpp b/src/kernel/actor/CommObserver.hpp index c64f2b0b4d..dff6de5ab2 100644 --- a/src/kernel/actor/CommObserver.hpp +++ b/src/kernel/actor/CommObserver.hpp @@ -92,16 +92,18 @@ class CommIsendSimcall : public SimcallObserver { activity::CommImpl* comm_; int tag_; - bool (*match_fun_)(void*, void*, activity::CommImpl*); - void (*clean_fun_)(void*); // used to free the synchro in case of problem after a detached send - void (*copy_data_fun_)(activity::CommImpl*, void*, size_t); // used to copy data if not default one + std::function match_fun_; + std::function clean_fun_; // used to free the synchro in case of problem after a detached send + std::function copy_data_fun_; // used to copy data if not default one public: - CommIsendSimcall(ActorImpl* actor, activity::MailboxImpl* mbox, double payload_size, double rate, - unsigned char* src_buff, size_t src_buff_size, bool (*match_fun)(void*, void*, activity::CommImpl*), - void (*clean_fun)(void*), // used to free the synchro in case of problem after a detached send - void (*copy_data_fun)(activity::CommImpl*, void*, size_t), // used to copy data if not default one - void* payload, bool detached) + CommIsendSimcall( + ActorImpl* actor, activity::MailboxImpl* mbox, double payload_size, double rate, unsigned char* src_buff, + size_t src_buff_size, const std::function& match_fun, + const std::function& clean_fun, // used to free the synchro in case of problem after a detached send + const std::function& + copy_data_fun, // used to copy data if not default one + void* payload, bool detached) : SimcallObserver(actor) , mbox_(mbox) , payload_size_(payload_size) @@ -127,9 +129,9 @@ public: void set_comm(activity::CommImpl* comm) { comm_ = comm; } void set_tag(int tag) { tag_ = tag; } - auto get_match_fun() const { return match_fun_; } - auto get_clean_fun() const { return clean_fun_; } - auto get_copy_data_fun() const { return copy_data_fun_; } + auto const& get_match_fun() const { return match_fun_; } + auto const& get_clean_fun() const { return clean_fun_; } + auto const& get_copy_data_fun() const { return copy_data_fun_; } }; class CommIrecvSimcall : public SimcallObserver { @@ -141,13 +143,14 @@ class CommIrecvSimcall : public SimcallObserver { int tag_; activity::CommImpl* comm_; - bool (*match_fun_)(void*, void*, activity::CommImpl*); - void (*copy_data_fun_)(activity::CommImpl*, void*, size_t); // used to copy data if not default one + std::function match_fun_; + std::function copy_data_fun_; // used to copy data if not default one public: CommIrecvSimcall(ActorImpl* actor, activity::MailboxImpl* mbox, unsigned char* dst_buff, size_t* dst_buff_size, - bool (*match_fun)(void*, void*, activity::CommImpl*), - void (*copy_data_fun)(activity::CommImpl*, void*, size_t), void* payload, double rate) + const std::function& match_fun, + const std::function& copy_data_fun, void* payload, + double rate) : SimcallObserver(actor) , mbox_(mbox) , dst_buff_(dst_buff) @@ -168,8 +171,8 @@ public: void set_comm(activity::CommImpl* comm) { comm_ = comm; } void set_tag(int tag) { tag_ = tag; } - auto get_match_fun() const { return match_fun_; }; - auto get_copy_data_fun() const { return copy_data_fun_; } + auto const& get_match_fun() const { return match_fun_; }; + auto const& get_copy_data_fun() const { return copy_data_fun_; } }; } // namespace actor diff --git a/src/s4u/s4u_Comm.cpp b/src/s4u/s4u_Comm.cpp index 2443dd145d..eec97a78ad 100644 --- a/src/s4u/s4u_Comm.cpp +++ b/src/s4u/s4u_Comm.cpp @@ -24,7 +24,7 @@ xbt::signal Comm::on_send; xbt::signal Comm::on_recv; xbt::signal Comm::on_completion; -CommPtr Comm::set_copy_data_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t)) +CommPtr Comm::set_copy_data_callback(const std::function& callback) { copy_data_function_ = callback; return this; @@ -61,8 +61,10 @@ Comm::~Comm() } void Comm::send(kernel::actor::ActorImpl* sender, const Mailbox* mbox, double task_size, double rate, void* src_buff, - size_t src_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), - void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data, double timeout) + size_t src_buff_size, + const std::function& match_fun, + const std::function& copy_data_fun, + void* data, double timeout) { /* checking for infinite values */ xbt_assert(std::isfinite(task_size), "task_size is not finite!"); @@ -102,9 +104,9 @@ void Comm::send(kernel::actor::ActorImpl* sender, const Mailbox* mbox, double ta } void Comm::recv(kernel::actor::ActorImpl* receiver, const Mailbox* mbox, void* dst_buff, size_t* dst_buff_size, - bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), - void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data, double timeout, - double rate) + const std::function& match_fun, + const std::function& copy_data_fun, + void* data, double timeout, double rate) { xbt_assert(std::isfinite(timeout), "timeout is not finite!"); xbt_assert(mbox, "No rendez-vous point defined for recv"); diff --git a/src/s4u/s4u_Engine.cpp b/src/s4u/s4u_Engine.cpp index b7152fadfc..ea62433321 100644 --- a/src/s4u/s4u_Engine.cpp +++ b/src/s4u/s4u_Engine.cpp @@ -452,7 +452,8 @@ void Engine::set_config(const std::string& name, const std::string& value) config::set_value(name.c_str(), value); } -Engine* Engine::set_default_comm_data_copy_callback(void (*callback)(kernel::activity::CommImpl*, void*, size_t)) +Engine* Engine::set_default_comm_data_copy_callback( + const std::function& callback) { kernel::activity::CommImpl::set_copy_data_callback(callback); return this; diff --git a/src/s4u/s4u_Mailbox.cpp b/src/s4u/s4u_Mailbox.cpp index fd5a442d04..a0c99e5832 100644 --- a/src/s4u/s4u_Mailbox.cpp +++ b/src/s4u/s4u_Mailbox.cpp @@ -129,10 +129,10 @@ CommPtr Mailbox::get_init() } kernel::activity::ActivityImplPtr -Mailbox::iprobe(int type, bool (*match_fun)(void*, void*, kernel::activity::CommImpl*), void* data) +Mailbox::iprobe(int type, const std::function& match_fun, void* data) { return kernel::actor::simcall_answered( - [this, type, match_fun, data] { return pimpl_->iprobe(type, match_fun, data); }); + [this, type, &match_fun, data] { return pimpl_->iprobe(type, match_fun, data); }); } } // namespace s4u } // namespace simgrid -- 2.20.1