Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Give a way to C users to not leak activities in activityset
[simgrid.git] / examples / c / activityset-testany / activityset-testany.c
1 /* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "simgrid/activity_set.h"
7 #include "simgrid/actor.h"
8 #include "simgrid/comm.h"
9 #include "simgrid/engine.h"
10 #include "simgrid/exec.h"
11 #include "simgrid/host.h"
12 #include "simgrid/mailbox.h"
13
14 #include "xbt/log.h"
15 #include "xbt/sysdep.h"
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_activity_testany, "Messages specific for this s4u example");
18
19 static void bob(int argc, char* argv[])
20 {
21   XBT_INFO("Create my asynchronous activities");
22   sg_exec_t exec = sg_actor_exec_init(5e9);
23   sg_exec_start(exec);
24
25   sg_mailbox_t mbox = sg_mailbox_by_name("mbox");
26   void* payload     = NULL;
27   sg_comm_t comm    = sg_mailbox_get_async(mbox, &payload);
28
29   sg_activity_set_t pending_activities = sg_activity_set_init();
30   sg_activity_set_push(pending_activities, (sg_activity_t)exec);
31   sg_activity_set_push(pending_activities, (sg_activity_t)comm);
32
33   XBT_INFO("Sleep_for a while");
34   sg_actor_sleep_for(1);
35
36   XBT_INFO("Test for completed activities");
37   while (!sg_activity_set_empty(pending_activities)) {
38     sg_activity_t completed_one = sg_activity_set_test_any(pending_activities);
39     if (completed_one != NULL) {
40       if (sg_comm_isinstance(completed_one))
41         XBT_INFO("Completed a Comm");
42       if (sg_exec_isinstance(completed_one))
43         XBT_INFO("Completed an Exec");
44       sg_activity_unref(completed_one);
45     } else {
46       XBT_INFO("Nothing matches, test again in 0.5s");
47       sg_actor_sleep_for(.5);
48     }
49   }
50   XBT_INFO("Last activity is complete");
51   free(payload);
52 }
53
54 static void alice(int argc, char* argv[])
55 {
56   char* payload = xbt_strdup("Message");
57   XBT_INFO("Send '%s'", payload);
58   sg_mailbox_put(sg_mailbox_by_name("mbox"), payload, 6e8);
59 }
60
61 int main(int argc, char* argv[])
62 {
63   simgrid_init(&argc, argv);
64   xbt_assert(argc > 1,
65              "Usage: %s platform_file\n"
66              "\tExample: %s hosts_with_disks.xml\n",
67              argv[0], argv[0]);
68
69   simgrid_load_platform(argv[1]);
70
71   sg_actor_create("alice", sg_host_by_name("alice"), alice, 0, NULL);
72   sg_actor_create("bob", sg_host_by_name("bob"), bob, 0, NULL);
73
74   simgrid_run();
75
76   return 0;
77 }