Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix a segfault when Host.getByName(null) is called
[simgrid.git] / teshsuite / msg / task_destroy_cancel.c
1 /* Copyright (c) 2010-2014. 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 <stdio.h>
8 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19
20 /** Emitter function  */
21 int master(int argc, char *argv[])
22 {
23   double task_comp_size = 5E7;
24   double task_comm_size = 1E6;
25   double timeout = 1;
26
27   char mailbox[256];
28   msg_task_t task = NULL;
29   msg_comm_t comm = NULL;
30   xbt_ex_t ex;
31
32   sprintf(mailbox, "jupi");
33
34   task = MSG_task_create("normal", task_comp_size, task_comm_size, NULL);
35   XBT_INFO("Sending task: \"%s\"", task->name);
36   MSG_task_send_with_timeout(task, mailbox, timeout);
37
38   task = MSG_task_create("cancel directly", task_comp_size, task_comm_size, NULL);
39   XBT_INFO("Canceling task \"%s\" directly", task->name);
40   MSG_task_cancel(task);
41   MSG_task_destroy(task);
42
43   task = MSG_task_create("destroy directly", task_comp_size, task_comm_size, NULL);
44   XBT_INFO("Destroying task \"%s\" directly", task->name);
45   MSG_task_destroy(task);
46
47   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
48   comm = MSG_task_isend(task, mailbox);
49   XBT_INFO("Canceling task \"%s\" during comm", task->name);
50   MSG_task_cancel(task);
51   TRY {
52     MSG_comm_wait(comm, -1);
53   }
54   CATCH (ex) {
55     xbt_ex_free(ex);
56     MSG_comm_destroy(comm);
57   }
58   MSG_task_destroy(task);
59
60   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
61   comm = MSG_task_isend(task, mailbox);
62   XBT_INFO("Destroying task \"%s\" during comm", task->name);
63   MSG_task_destroy(task);
64   TRY {
65     MSG_comm_wait(comm, -1);
66   }
67   CATCH (ex) {
68     xbt_ex_free(ex);
69     MSG_comm_destroy(comm);
70   }
71
72   task = MSG_task_create("cancel", task_comp_size, task_comm_size, NULL);
73   MSG_task_send_with_timeout(task, mailbox, timeout);
74
75   task = MSG_task_create("finalize", task_comp_size, task_comm_size, NULL);
76   MSG_task_send_with_timeout(task, mailbox, timeout);
77
78   XBT_INFO("Goodbye now!");
79   return 0;
80 }                               /* end_of_master */
81
82 static int worker_main(int argc, char *argv[])
83 {
84   msg_task_t task = MSG_process_get_data(MSG_process_self());
85   msg_error_t res;
86   XBT_INFO("Start %s", task->name);
87   res = MSG_task_execute(task);
88   XBT_INFO("Task %s", res == MSG_OK ? "done" : "failed");
89   MSG_task_destroy(task);
90   return 0;
91 }
92
93 /** Receiver function  */
94 int slave(int argc, char *argv[])
95 {
96   msg_task_t task;
97   _XBT_GNUC_UNUSED int res;
98   int id = -1;
99   char mailbox[80];
100   double start, end;
101   sprintf(mailbox, "jupi");
102
103   while (1) {
104     task = NULL;
105     res = MSG_task_receive(&(task), mailbox);
106     xbt_assert(res == MSG_OK, "MSG_task_get failed");
107     XBT_INFO("Handling task \"%s\"", MSG_task_get_name(task));
108
109     if (!strcmp(MSG_task_get_name(task), "finalize")) {
110       XBT_INFO("Destroying task \"%s\"", task->name);
111       MSG_task_destroy(task);
112       break;
113     }
114
115     if (!strcmp(MSG_task_get_name(task), "cancel")) {
116       MSG_process_create("worker1", worker_main, task, MSG_host_self());
117       XBT_INFO("Canceling task \"%s\"", task->name);
118       MSG_task_cancel(task);
119       continue;
120     }
121
122     start = MSG_get_clock();
123     MSG_task_execute(task);
124     end = MSG_get_clock();
125     XBT_INFO("Task \"%s\" done in %f (amount %f)"
126                 , MSG_task_get_name(task)
127                 , end - start
128                 , MSG_task_get_remaining_computation(task));
129
130     MSG_task_destroy(task);
131     task = NULL;
132     id--;
133   }
134   XBT_INFO("I'm done. See you!");
135   return 0;
136 }                               /* end_of_slave */
137
138 /** Main function */
139 int main(int argc, char *argv[])
140 {
141   msg_error_t res;
142   const char *platform_file;
143   const char *application_file;
144
145   MSG_init(&argc, argv);
146   if (argc != 3) {
147     printf("Usage: %s platform_file deployment_file\n", argv[0]);
148     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
149     exit(1);
150   }
151   platform_file = argv[1];
152   application_file = argv[2];
153
154   /* MSG_config("workstation/model","KCCFLN05"); */
155   {                             /*  Simulation setting */
156     MSG_create_environment(platform_file);
157   }
158   {                             /*   Application deployment */
159     MSG_function_register("master", master);
160     MSG_function_register("slave", slave);
161
162     MSG_launch_application(application_file);
163   }
164   res = MSG_main();
165
166   XBT_INFO("Simulation time %g", MSG_get_clock());
167
168   if (res == MSG_OK)
169     return 0;
170   else
171     return 1;
172 }                               /* end_of_main */