From 308bf9e4897e3606f017559cefabd29fb7eaec03 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Thu, 17 Dec 2020 12:01:00 +0100 Subject: [PATCH] Use typed Mailbox::get_async<>() instead of using casts everywhere. --- examples/s4u/app-bittorrent/s4u-peer.cpp | 8 ++--- examples/s4u/app-bittorrent/s4u-tracker.cpp | 7 ++--- .../s4u/app-chainsend/s4u-app-chainsend.cpp | 6 ++-- examples/s4u/comm-wait/s4u-comm-wait.cpp | 5 ++- examples/s4u/dht-chord/s4u-dht-chord-node.cpp | 31 +++++++++---------- examples/s4u/dht-kademlia/node.cpp | 30 +++++++++--------- examples/s4u/dht-kademlia/node.hpp | 2 +- .../s4u/dht-kademlia/s4u-dht-kademlia.cpp | 9 +++--- examples/s4u/energy-link/s4u-energy-link.cpp | 4 +-- .../s4u-mc-electric-fence.cpp | 12 +++---- .../s4u/activity-lifecycle/testing_comm.cpp | 2 +- .../s4u/comm-get-sender/comm-get-sender.cpp | 6 ++-- teshsuite/s4u/wait-any-for/wait-any-for.cpp | 8 ++--- 13 files changed, 59 insertions(+), 71 deletions(-) diff --git a/examples/s4u/app-bittorrent/s4u-peer.cpp b/examples/s4u/app-bittorrent/s4u-peer.cpp index 06cc7b5175..2f0f8ca7c2 100644 --- a/examples/s4u/app-bittorrent/s4u-peer.cpp +++ b/examples/s4u/app-bittorrent/s4u-peer.cpp @@ -251,13 +251,11 @@ void Peer::leech() sendHandshakeToAllPeers(); XBT_DEBUG("Starting main leech loop listening on mailbox: %s", mailbox_->get_cname()); - void* data = nullptr; while (simgrid::s4u::Engine::get_clock() < deadline && countPieces(bitfield_) < FILE_PIECES) { if (comm_received == nullptr) { - comm_received = mailbox_->get_async(&data); + comm_received = mailbox_->get_async(&message); } if (comm_received->test()) { - message = static_cast(data); handleMessage(); delete message; comm_received = nullptr; @@ -280,13 +278,11 @@ void Peer::seed() double next_choked_update = simgrid::s4u::Engine::get_clock() + UPDATE_CHOKED_INTERVAL; XBT_DEBUG("Start seeding."); // start the main seed loop - void* data = nullptr; while (simgrid::s4u::Engine::get_clock() < deadline) { if (comm_received == nullptr) { - comm_received = mailbox_->get_async(&data); + comm_received = mailbox_->get_async(&message); } if (comm_received->test()) { - message = static_cast(data); handleMessage(); delete message; comm_received = nullptr; diff --git a/examples/s4u/app-bittorrent/s4u-tracker.cpp b/examples/s4u/app-bittorrent/s4u-tracker.cpp index ffe71de08c..f3a1e67fac 100644 --- a/examples/s4u/app-bittorrent/s4u-tracker.cpp +++ b/examples/s4u/app-bittorrent/s4u-tracker.cpp @@ -29,14 +29,13 @@ Tracker::Tracker(std::vector args) void Tracker::operator()() { simgrid::s4u::CommPtr comm = nullptr; - void* received = nullptr; + TrackerQuery* query = nullptr; while (simgrid::s4u::Engine::get_clock() < deadline) { if (comm == nullptr) - comm = mailbox->get_async(&received); + comm = mailbox->get_async(&query); if (comm->test()) { // Retrieve the data sent by the peer. - xbt_assert(received != nullptr); - auto* query = static_cast(received); + xbt_assert(query != nullptr); // Add the peer to our peer list, if not already known. if (known_peers.find(query->getPeerId()) == known_peers.end()) { diff --git a/examples/s4u/app-chainsend/s4u-app-chainsend.cpp b/examples/s4u/app-chainsend/s4u-app-chainsend.cpp index cf028a1661..44f90eb0d2 100644 --- a/examples/s4u/app-chainsend/s4u-app-chainsend.cpp +++ b/examples/s4u/app-chainsend/s4u-app-chainsend.cpp @@ -58,11 +58,11 @@ public: void forwardFile() { - void* received; + FilePiece* received; bool done = false; while (not done) { - simgrid::s4u::CommPtr comm = me->get_async(&received); + simgrid::s4u::CommPtr comm = me->get_async(&received); pending_recvs.push_back(comm); int idx = simgrid::s4u::Comm::wait_any(&pending_recvs); @@ -75,7 +75,7 @@ public: simgrid::s4u::CommPtr send = next->put_async(received, MESSAGE_SEND_DATA_HEADER_SIZE + PIECE_SIZE); pending_sends.push_back(send); } else - delete static_cast(received); + delete received; received_pieces++; received_bytes += PIECE_SIZE; diff --git a/examples/s4u/comm-wait/s4u-comm-wait.cpp b/examples/s4u/comm-wait/s4u-comm-wait.cpp index b2e5314011..1e70eb52bf 100644 --- a/examples/s4u/comm-wait/s4u-comm-wait.cpp +++ b/examples/s4u/comm-wait/s4u-comm-wait.cpp @@ -68,8 +68,8 @@ static void receiver(int, char**) XBT_INFO("Wait for my first message"); for (bool cont = true; cont;) { - void* payload; - simgrid::s4u::CommPtr comm = mbox->get_async(&payload); + std::string* received; + simgrid::s4u::CommPtr comm = mbox->get_async(&received); if (sleep_test_time > 0) { /* - "test_time" is set to 0, wait */ while (not comm->test()) { /* - Call test() every "sleep_test_time" otherwise */ @@ -79,7 +79,6 @@ static void receiver(int, char**) comm->wait(); } - const auto* received = static_cast(payload); XBT_INFO("I got a '%s'.", received->c_str()); if (*received == "finalize") cont = false; // If it's a finalize message, we're done. diff --git a/examples/s4u/dht-chord/s4u-dht-chord-node.cpp b/examples/s4u/dht-chord/s4u-dht-chord-node.cpp index d727cf3d76..dc2ef54b48 100644 --- a/examples/s4u/dht-chord/s4u-dht-chord-node.cpp +++ b/examples/s4u/dht-chord/s4u-dht-chord-node.cpp @@ -201,7 +201,6 @@ void Node::printFingerTable() void Node::checkPredecessor() { XBT_DEBUG("Checking whether my predecessor is alive"); - void* data = nullptr; if (pred_id_ == -1) return; @@ -224,12 +223,13 @@ void Node::checkPredecessor() // receive the answer XBT_DEBUG("Sent 'Predecessor Alive' request to %d, waiting for the answer on my mailbox '%s'", pred_id_, message->answer_to->get_cname()); - simgrid::s4u::CommPtr comm = return_mailbox->get_async(&data); + ChordMessage* answer = nullptr; + simgrid::s4u::CommPtr comm = return_mailbox->get_async(&answer); try { comm->wait_for(timeout); XBT_DEBUG("Received the answer to my 'Predecessor Alive': my predecessor %d is alive", pred_id_); - delete static_cast(data); + delete answer; } catch (const simgrid::TimeoutException&) { XBT_DEBUG("Failed to receive the answer to my 'Predecessor Alive' request"); pred_id_ = -1; @@ -244,7 +244,6 @@ void Node::checkPredecessor() int Node::remoteGetPredecessor(int ask_to) { int predecessor_id = -1; - void* data = nullptr; simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(ask_to)); simgrid::s4u::Mailbox* return_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id_) + "_pred"); @@ -265,18 +264,18 @@ int Node::remoteGetPredecessor(int ask_to) // receive the answer XBT_DEBUG("Sent 'Get Predecessor' request to %d, waiting for the answer on my mailbox '%s'", ask_to, message->answer_to->get_cname()); - simgrid::s4u::CommPtr comm = return_mailbox->get_async(&data); + ChordMessage* answer = nullptr; + simgrid::s4u::CommPtr comm = return_mailbox->get_async(&answer); try { comm->wait_for(timeout); - const auto* answer = static_cast(data); XBT_DEBUG("Received the answer to my 'Get Predecessor' request: the predecessor of node %d is %d", ask_to, answer->answer_id); predecessor_id = answer->answer_id; delete answer; } catch (const simgrid::TimeoutException&) { XBT_DEBUG("Failed to receive the answer to my 'Get Predecessor' request"); - delete static_cast(data); + delete answer; } return predecessor_id; @@ -316,7 +315,6 @@ int Node::findSuccessor(int id) int Node::remoteFindSuccessor(int ask_to, int id) { int successor = -1; - ChordMessage* data = nullptr; simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(ask_to)); simgrid::s4u::Mailbox* return_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id_) + "_succ"); @@ -335,18 +333,18 @@ int Node::remoteFindSuccessor(int ask_to, int id) } // receive the answer XBT_DEBUG("Sent a 'Find Successor' request to %d for key %d, waiting for the answer", ask_to, id); - simgrid::s4u::CommPtr comm = return_mailbox->get_async(reinterpret_cast(&data)); + ChordMessage* answer = nullptr; + simgrid::s4u::CommPtr comm = return_mailbox->get_async(&answer); try { comm->wait_for(timeout); - const ChordMessage* answer = data; XBT_DEBUG("Received the answer to my 'Find Successor' request for id %d: the successor of key %d is %d", answer->request_id, id_, answer->answer_id); successor = answer->answer_id; delete answer; } catch (const simgrid::TimeoutException&) { XBT_DEBUG("Failed to receive the answer to my 'Find Successor' request"); - delete data; + delete answer; } return successor; @@ -491,7 +489,7 @@ void Node::operator()() if (not joined) return; - void* data = nullptr; + ChordMessage* message = nullptr; double now = simgrid::s4u::Engine::get_clock(); double next_stabilize_date = start_time_ + PERIODIC_STABILIZE_DELAY; double next_fix_fingers_date = start_time_ + PERIODIC_FIX_FINGERS_DELAY; @@ -500,7 +498,7 @@ void Node::operator()() simgrid::s4u::CommPtr comm_receive = nullptr; while (now < std::min(start_time_ + deadline_, MAX_SIMULATION_TIME)) { if (comm_receive == nullptr) - comm_receive = mailbox_->get_async(&data); + comm_receive = mailbox_->get_async(&message); bool comm_completed = true; try { if (not comm_receive->test()) @@ -510,10 +508,9 @@ void Node::operator()() } if (comm_completed) { - if (data != nullptr) { - auto* message = static_cast(data); + if (message != nullptr) { handleMessage(message); - data = nullptr; + message = nullptr; } comm_receive = nullptr; } else { @@ -541,7 +538,7 @@ void Node::operator()() if (comm_receive != nullptr) { try { if (comm_receive->test()) - delete static_cast(data); + delete message; else comm_receive->cancel(); } catch (const simgrid::TimeoutException&) { diff --git a/examples/s4u/dht-kademlia/node.cpp b/examples/s4u/dht-kademlia/node.cpp index d72a5c3028..067eed5358 100644 --- a/examples/s4u/dht-kademlia/node.cpp +++ b/examples/s4u/dht-kademlia/node.cpp @@ -33,20 +33,19 @@ bool Node::join(unsigned int known_id) simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id_)); do { if (receive_comm == nullptr) - receive_comm = mailbox->get_async(&received_msg); + receive_comm = mailbox->get_async(&received_msg); if (receive_comm->test()) { XBT_DEBUG("Received an answer from the node I know."); got_answer = true; // retrieve the node list and ping them. - const auto* msg = static_cast(received_msg); - const Answer* node_list = msg->answer_.get(); + const Answer* node_list = received_msg->answer_.get(); if (node_list) { for (auto const& contact : node_list->getNodes()) routingTableUpdate(contact.first); } else { - handleFindNode(msg); + handleFindNode(received_msg); } - delete msg; + delete received_msg; receive_comm = nullptr; } else simgrid::s4u::this_actor::sleep_for(1); @@ -199,33 +198,32 @@ bool Node::findNode(unsigned int id_to_find, bool count_in_stats) simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id_)); do { if (receive_comm == nullptr) - receive_comm = mailbox->get_async(&received_msg); + receive_comm = mailbox->get_async(&received_msg); if (receive_comm->test()) { - const auto* msg = static_cast(received_msg); // Check if what we have received is what we are looking for. - if (msg->answer_ && msg->answer_->getDestinationId() == id_to_find) { - routingTableUpdate(msg->sender_id_); + if (received_msg->answer_ && received_msg->answer_->getDestinationId() == id_to_find) { + routingTableUpdate(received_msg->sender_id_); // Handle the answer for (auto const& contact : node_list->getNodes()) routingTableUpdate(contact.first); answers++; - nodes_added = node_list->merge(msg->answer_.get()); - XBT_DEBUG("Received an answer from %s (%s) with %zu nodes on it", msg->answer_to_->get_cname(), - msg->issuer_host_name_.c_str(), msg->answer_->getSize()); + nodes_added = node_list->merge(received_msg->answer_.get()); + XBT_DEBUG("Received an answer from %s (%s) with %zu nodes on it", received_msg->answer_to_->get_cname(), + received_msg->issuer_host_name_.c_str(), received_msg->answer_->getSize()); } else { - if (msg->answer_) { - routingTableUpdate(msg->sender_id_); + if (received_msg->answer_) { + routingTableUpdate(received_msg->sender_id_); XBT_DEBUG("Received a wrong answer for a FIND_NODE"); } else { - handleFindNode(msg); + handleFindNode(received_msg); } // Update the timeout if we didn't have our answer timeout += simgrid::s4u::Engine::get_clock() - time_beginreceive; time_beginreceive = simgrid::s4u::Engine::get_clock(); } - delete msg; + delete received_msg; receive_comm = nullptr; } else { simgrid::s4u::this_actor::sleep_for(1); diff --git a/examples/s4u/dht-kademlia/node.hpp b/examples/s4u/dht-kademlia/node.hpp index c508c06820..4ee667d02b 100644 --- a/examples/s4u/dht-kademlia/node.hpp +++ b/examples/s4u/dht-kademlia/node.hpp @@ -22,7 +22,7 @@ class Node { unsigned int find_node_failed = 0; // Number of find_node which have failed. public: simgrid::s4u::CommPtr receive_comm = nullptr; - void* received_msg = nullptr; + Message* received_msg = nullptr; explicit Node(unsigned int node_id) : id_(node_id), table(node_id) {} Node(const Node&) = delete; Node& operator=(const Node&) = delete; diff --git a/examples/s4u/dht-kademlia/s4u-dht-kademlia.cpp b/examples/s4u/dht-kademlia/s4u-dht-kademlia.cpp index e8fcbcda6f..7c9b57a198 100644 --- a/examples/s4u/dht-kademlia/s4u-dht-kademlia.cpp +++ b/examples/s4u/dht-kademlia/s4u-dht-kademlia.cpp @@ -48,14 +48,13 @@ static void node(std::vector args) while (simgrid::s4u::Engine::get_clock() < deadline) { if (node.receive_comm == nullptr) - node.receive_comm = mailbox->get_async(&node.received_msg); + node.receive_comm = mailbox->get_async(&node.received_msg); if (node.receive_comm->test()) { // There has been a message, we need to handle it ! - const auto* msg = static_cast(node.received_msg); - if (msg) { - node.handleFindNode(msg); - delete msg; + if (node.received_msg) { + node.handleFindNode(node.received_msg); + delete node.received_msg; node.receive_comm = nullptr; } else simgrid::s4u::this_actor::sleep_for(1); diff --git a/examples/s4u/energy-link/s4u-energy-link.cpp b/examples/s4u/energy-link/s4u-energy-link.cpp index f8da5b7a77..c5fd7a3e90 100644 --- a/examples/s4u/energy-link/s4u-energy-link.cpp +++ b/examples/s4u/energy-link/s4u-energy-link.cpp @@ -52,12 +52,12 @@ static void receiver(std::vector args) char* res = mailbox->get(); xbt_free(res); } else { - std::vector data(flow_amount); + std::vector data(flow_amount); // Start all comms in parallel, and wait for their completion in one shot std::vector comms; for (int i = 0; i < flow_amount; i++) - comms.push_back(mailbox->get_async(&data[i])); + comms.push_back(mailbox->get_async(&data[i])); simgrid::s4u::Comm::wait_all(&comms); for (int i = 0; i < flow_amount; i++) diff --git a/examples/s4u/mc-electric-fence/s4u-mc-electric-fence.cpp b/examples/s4u/mc-electric-fence/s4u-mc-electric-fence.cpp index 658363551e..bf3947b9e5 100644 --- a/examples/s4u/mc-electric-fence/s4u-mc-electric-fence.cpp +++ b/examples/s4u/mc-electric-fence/s4u-mc-electric-fence.cpp @@ -17,17 +17,17 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(electric_fence, "Example to check the soundness of static void server() { - void* data1 = nullptr; - void* data2 = nullptr; - simgrid::s4u::CommPtr comm_received1 = simgrid::s4u::Mailbox::by_name("mymailbox")->get_async(&data1); - simgrid::s4u::CommPtr comm_received2 = simgrid::s4u::Mailbox::by_name("mymailbox")->get_async(&data2); + int* data1 = nullptr; + int* data2 = nullptr; + simgrid::s4u::CommPtr comm_received1 = simgrid::s4u::Mailbox::by_name("mymailbox")->get_async(&data1); + simgrid::s4u::CommPtr comm_received2 = simgrid::s4u::Mailbox::by_name("mymailbox")->get_async(&data2); comm_received1->wait(); comm_received2->wait(); XBT_INFO("OK"); - delete static_cast(data1); - delete static_cast(data2); + delete data1; + delete data2; } static void client(int id) diff --git a/teshsuite/s4u/activity-lifecycle/testing_comm.cpp b/teshsuite/s4u/activity-lifecycle/testing_comm.cpp index 9dc9347b2f..325647473e 100644 --- a/teshsuite/s4u/activity-lifecycle/testing_comm.cpp +++ b/teshsuite/s4u/activity-lifecycle/testing_comm.cpp @@ -282,7 +282,7 @@ TEST_CASE("Activity lifecycle: comm activities") simgrid::s4u::ActorPtr receiver = simgrid::s4u::Actor::create("receiver", all_hosts[1], []() { assert_exit(true, 2); int* data; - simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name("mb")->get_async((void**)&data); + simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name("mb")->get_async(&data); std::vector pending_comms = {comm}; REQUIRE_NETWORK_FAILURE(simgrid::s4u::Comm::wait_any(&pending_comms)); }); diff --git a/teshsuite/s4u/comm-get-sender/comm-get-sender.cpp b/teshsuite/s4u/comm-get-sender/comm-get-sender.cpp index 28e7e71155..dd903f79f4 100644 --- a/teshsuite/s4u/comm-get-sender/comm-get-sender.cpp +++ b/teshsuite/s4u/comm-get-sender/comm-get-sender.cpp @@ -20,14 +20,14 @@ static void sender_fun() static void receiver_fun() { XBT_INFO("Receiving"); - void* payload = nullptr; - simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name("Tremblay")->get_async(&payload); + std::string* payload = nullptr; + simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name("Tremblay")->get_async(&payload); comm->wait(); xbt_assert(comm->get_sender(), "No sender received"); XBT_INFO("Got a message sent by '%s'", comm->get_sender()->get_cname()); simgrid::s4u::this_actor::sleep_for(2.0); XBT_INFO("Did I tell you that I got a message sent by '%s'?", comm->get_sender()->get_cname()); - delete static_cast(payload); + delete payload; } int main(int argc, char* argv[]) diff --git a/teshsuite/s4u/wait-any-for/wait-any-for.cpp b/teshsuite/s4u/wait-any-for/wait-any-for.cpp index a206d6a5d0..494d56179d 100644 --- a/teshsuite/s4u/wait-any-for/wait-any-for.cpp +++ b/teshsuite/s4u/wait-any-for/wait-any-for.cpp @@ -16,11 +16,11 @@ static void worker() auto put1 = mbox->put_async(&input1, 1000 * 1000 * 500); auto put2 = mbox->put_async(&input2, 1000 * 1000 * 1000); - int * out1; - auto get1 = mbox->get_async((void**)&out1); + int* out1; + auto get1 = mbox->get_async(&out1); - int * out2; - auto get2 = mbox->get_async((void**)&out2); + int* out2; + auto get2 = mbox->get_async(&out2); XBT_INFO("All comms have started"); std::vector comms = {put1, put2, get1, get2}; -- 2.20.1