Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rewrite the maestro-set test to something meaningful
[simgrid.git] / examples / msg / maestro-set / maestro-set.c
1 /* Copyright (c) 2007-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 #define _GNU_SOURCE         /* See feature_test_macros(7) */
12 #include <unistd.h>
13 #include <sys/syscall.h>
14 #include <sys/types.h> /* pid_t */
15
16 pid_t root_pid;
17
18 static void ensure_root_tid() {
19   pid_t my_pid = syscall(SYS_gettid);
20   xbt_assert(my_pid == root_pid, bprintf("I was supposed to be the main thread but %d != %d", my_pid, root_pid));
21   XBT_INFO("I am the main thread, as expected");
22 }
23 static void ensure_other_tid() {
24   pid_t my_pid = syscall(SYS_gettid);
25   xbt_assert(my_pid != root_pid, "I was NOT supposed to be the main thread");
26   XBT_INFO("I am not the main thread, as expected");
27 }
28
29 /** @addtogroup MSG_examples
30  *
31  *  - <b>maestro-set/maestro-set.c: Switch the system thread hosting our maestro</b>. 
32  *    That's a very advanced example in which we move the maestro thread to another process.
33  *    Not many users need it (maybe only one, actually), but this example is also a regression test.
34  */
35
36
37 static int sender(int argc, char *argv[])
38 {
39   ensure_root_tid();
40
41   msg_task_t task_la = MSG_task_create("Some task", 0.0, 10e8, NULL);
42   MSG_task_send(task_la, "some mailbox");
43
44   return 0;
45 }
46
47 static int receiver(int argc, char *argv[])
48 {
49   ensure_other_tid();
50
51   msg_task_t task_la = NULL;
52   xbt_assert(MSG_task_receive(&task_la,"some mailbox") == MSG_OK);
53   XBT_INFO("Task received");
54   MSG_task_destroy(task_la);
55
56   return 0;
57 }
58
59 static void maestro(void* data)
60 {
61   ensure_other_tid();
62   MSG_process_create("receiver",&receiver,NULL,MSG_host_by_name("Jupiter"));
63   MSG_main();
64 }
65
66 /** Main function */
67 int main(int argc, char *argv[])
68 {
69   root_pid = syscall(SYS_gettid);
70
71   SIMIX_set_maestro(maestro, NULL);
72   MSG_init(&argc, argv);
73
74   if (argc != 2) {
75     XBT_CRITICAL("Usage: %s platform_file\n", argv[0]);
76     xbt_die("example: %s msg_platform.xml\n",argv[0]);
77   }
78
79   MSG_create_environment(argv[1]);
80
81   /* Become one of the simulated process.
82    *
83    * This must be done after the creation of the platform because we are depending attaching to a host.*/
84   MSG_process_attach("sender", NULL, MSG_host_by_name("Tremblay"), NULL);
85   ensure_root_tid();
86
87   // Execute the sender code:
88   const char* subargv[3] = { "sender", "Jupiter", NULL };
89   sender(2, (char**) subargv);
90
91   MSG_process_detach(); // Become root thread again
92   XBT_INFO("Detached");
93   ensure_root_tid();
94
95   return 0;
96 }