1 /* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/s4u.hpp"
10 namespace sg4 = simgrid::s4u;
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_activity_testany, "Messages specific for this s4u example");
16 sg4::Mailbox* mbox = sg4::Mailbox::by_name("mbox");
17 const sg4::Disk* disk = sg4::Host::current()->get_disks().front();
20 XBT_INFO("Create my asynchronous activities");
21 auto exec = sg4::this_actor::exec_async(5e9);
22 auto comm = mbox->get_async(&payload);
23 auto io = disk->read_async(3e8);
25 std::vector<sg4::ActivityPtr> pending_activities = {boost::dynamic_pointer_cast<sg4::Activity>(exec),
26 boost::dynamic_pointer_cast<sg4::Activity>(comm),
27 boost::dynamic_pointer_cast<sg4::Activity>(io)};
29 XBT_INFO("Sleep_for a while");
30 sg4::this_actor::sleep_for(1);
32 XBT_INFO("Test for completed activities");
33 while (not pending_activities.empty()) {
34 ssize_t changed_pos = sg4::Activity::test_any(pending_activities);
35 if (changed_pos != -1) {
36 auto* completed_one = pending_activities[changed_pos].get();
37 if (dynamic_cast<sg4::Comm*>(completed_one))
38 XBT_INFO("Completed a Comm");
39 if (dynamic_cast<sg4::Exec*>(completed_one))
40 XBT_INFO("Completed an Exec");
41 if (dynamic_cast<sg4::Io*>(completed_one))
42 XBT_INFO("Completed an I/O");
43 pending_activities.erase(pending_activities.begin() + changed_pos);
44 } else { // nothing matches, wait for a little bit
45 XBT_INFO("Nothing matches, test again in 0.5s");
46 sg4::this_actor::sleep_for(.5);
49 XBT_INFO("Last activity is complete");
55 auto* payload = new std::string("Message");
56 XBT_INFO("Send '%s'", payload->c_str());
57 sg4::Mailbox::by_name("mbox")->put(payload, 6e8);
60 int main(int argc, char* argv[])
62 sg4::Engine e(&argc, argv);
64 e.load_platform(argv[1]);
66 sg4::Actor::create("bob", e.host_by_name("bob"), bob);
67 sg4::Actor::create("alice", e.host_by_name("alice"), alice);