Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Getting rid of C exceptions
[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 "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 static int master(int argc, char *argv[])
12 {
13   double task_comp_size = 5E7;
14   double task_comm_size = 1E6;
15   double timeout = 1;
16
17   const char * mailbox = "jupi";
18
19   msg_task_t task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
20   XBT_INFO("Sending task: \"%s\"", task->name);
21   MSG_task_send_with_timeout(task, mailbox, timeout);
22
23   task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
24   XBT_INFO("Canceling task \"%s\" directly", task->name);
25   MSG_task_cancel(task);
26   MSG_task_destroy(task);
27
28   task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
29   XBT_INFO("Destroying task \"%s\" directly", task->name);
30   MSG_task_destroy(task);
31
32   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
33   msg_comm_t comm = MSG_task_isend(task, mailbox);
34   XBT_INFO("Canceling task \"%s\" during comm", task->name);
35   MSG_task_cancel(task);
36   try {
37     MSG_comm_wait(comm, -1);
38   }
39   catch (xbt_ex& ex) {;
40     MSG_comm_destroy(comm);
41   }
42   MSG_task_destroy(task);
43
44   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
45   comm = MSG_task_isend(task, mailbox);
46   XBT_INFO("Destroying task \"%s\" during comm", task->name);
47   MSG_task_destroy(task);
48   try {
49     MSG_comm_wait(comm, -1);
50   }
51   catch (xbt_ex& ex) {;
52     MSG_comm_destroy(comm);
53   }
54
55   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
56   MSG_task_send_with_timeout(task, mailbox, timeout);
57
58   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
59   MSG_task_send_with_timeout(task, mailbox, timeout);
60
61   XBT_INFO("Goodbye now!");
62   return 0;
63 }
64
65 static int worker_main(int argc, char *argv[])
66 {
67   msg_task_t task = (msg_task_t) MSG_process_get_data(MSG_process_self());
68   msg_error_t res;
69   XBT_INFO("Start %s", task->name);
70   res = MSG_task_execute(task);
71   XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
72   MSG_task_destroy(task);
73   return 0;
74 }
75
76 static int slave(int argc, char *argv[])
77 {
78   msg_task_t task;
79   XBT_ATTRIB_UNUSED int res;
80   int id = -1;
81   const char * mailbox = "jupi";
82   double start, end;
83  
84   while (1) {
85     task = NULL;
86     res = MSG_task_receive(&(task), mailbox);
87     xbt_assert(res == MSG_OK, "MSG_task_get failed");
88     XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
89
90     if (!strcmp(MSG_task_get_name(task), "finalize")) {
91       XBT_INFO("Destroying task \"%s\"", task->name);
92       MSG_task_destroy(task);
93       break;
94     }
95
96     if (!strcmp(MSG_task_get_name(task), "cancel")) {
97       MSG_process_create("worker1", worker_main, task, MSG_host_self());
98       MSG_process_sleep(0.1);
99       XBT_INFO("Canceling task \"%s\"", task->name);
100       MSG_task_cancel(task);
101       continue;
102     }
103
104     start = MSG_get_clock();
105     MSG_task_execute(task);
106     end = MSG_get_clock();
107     XBT_INFO("Task \"%s\" done in %f (amount %f)", MSG_task_get_name(task), end - start,
108              MSG_task_get_flops_amount(task));
109
110     MSG_task_destroy(task);
111     task = NULL;
112     id--;
113   }
114   XBT_INFO("I'm done. See you!");
115   return 0;
116 }
117
118 int main(int argc, char *argv[])
119 {
120   msg_error_t res;
121
122   MSG_init(&argc, argv);
123   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s msg_platform.xml\n", argv[0], argv[0]);
124
125   MSG_create_environment(argv[1]);
126
127   MSG_process_create("master", master, NULL, MSG_get_host_by_name("Tremblay"));
128   MSG_process_create("slave", slave, NULL, MSG_get_host_by_name("Jupiter"));
129
130   res = MSG_main();
131
132   XBT_INFO("Simulation time %g", MSG_get_clock());
133
134   return res != MSG_OK;
135 }