Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try to please sonar in a new and innovative way
[simgrid.git] / teshsuite / msg / task_destroy_cancel / task_destroy_cancel.cpp
1 /* Copyright (c) 2010-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <xbt/ex.hpp>
8 #include "simgrid/msg.h"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11
12 static int master(int /*argc*/, char* /*argv*/ [])
13 {
14   double task_comp_size = 5E7;
15   double task_comm_size = 1E6;
16   double timeout = 1;
17
18   msg_task_t task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
19   XBT_INFO("Sending task: \"%s\"", task->name);
20   MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
21
22   task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
23   XBT_INFO("Canceling task \"%s\" directly", task->name);
24   MSG_task_cancel(task);
25   MSG_task_destroy(task);
26
27   task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
28   XBT_INFO("Destroying task \"%s\" directly", task->name);
29   MSG_task_destroy(task);
30
31   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
32   msg_comm_t comm = MSG_task_isend(task, "worker_mailbox");
33   XBT_INFO("Canceling task \"%s\" during comm", task->name);
34   MSG_task_cancel(task);
35   try {
36     MSG_comm_wait(comm, -1);
37   }
38   catch (xbt_ex& ex) {
39     MSG_comm_destroy(comm);
40   }
41   MSG_task_destroy(task);
42
43   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
44   comm = MSG_task_isend(task, "worker_mailbox");
45   XBT_INFO("Destroying task \"%s\" during comm", task->name);
46   MSG_task_destroy(task);
47   try {
48     MSG_comm_wait(comm, -1);
49   }
50   catch (xbt_ex& ex) {
51     MSG_comm_destroy(comm);
52   }
53
54   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
55   MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
56
57   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
58   MSG_task_send_with_timeout(task, "worker_mailbox", timeout);
59
60   XBT_INFO("Goodbye now!");
61   return 0;
62 }
63
64 static int worker_main(int /*argc*/, char* /*argv*/ [])
65 {
66   msg_task_t task = (msg_task_t) MSG_process_get_data(MSG_process_self());
67   msg_error_t res;
68   XBT_INFO("Start %s", task->name);
69   res = MSG_task_execute(task);
70   XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
71   MSG_task_destroy(task);
72   return 0;
73 }
74
75 static int worker(int /*argc*/, char* /*argv*/ [])
76 {
77   while (1) {
78     msg_task_t task = NULL;
79     XBT_ATTRIB_UNUSED int res = MSG_task_receive(&(task), "worker_mailbox");
80     xbt_assert(res == MSG_OK, "MSG_task_get failed");
81     XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
82
83     if (not strcmp(MSG_task_get_name(task), "finalize")) {
84       XBT_INFO("Destroying task \"%s\"", task->name);
85       MSG_task_destroy(task);
86       break;
87     }
88
89     if (not strcmp(MSG_task_get_name(task), "cancel")) {
90       MSG_process_create("worker1", worker_main, task, MSG_host_self());
91       MSG_process_sleep(0.1);
92       XBT_INFO("Canceling task \"%s\"", task->name);
93       MSG_task_cancel(task);
94       continue;
95     }
96
97     double start = MSG_get_clock();
98     MSG_task_execute(task);
99     double end = MSG_get_clock();
100     XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
101              MSG_task_get_flops_amount(task));
102
103     MSG_task_destroy(task);
104   }
105   XBT_INFO("I'm done. See you!");
106   return 0;
107 }
108
109 int main(int argc, char *argv[])
110 {
111   MSG_init(&argc, argv);
112   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
113
114   MSG_create_environment(argv[1]);
115
116   MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
117   MSG_process_create("worker", worker, NULL, MSG_get_host_by_name("Jupiter"));
118
119   msg_error_t res = MSG_main();
120
121   XBT_INFO("Simulation time %g", MSG_get_clock());
122
123   return res != MSG_OK;
124 }