Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
da053ee8244f6456412de88504fe4182df3f6a43
[simgrid.git] / src / xbt / xbt_main.c
1 /* module handling                                                          */
2
3 /* Copyright (c) 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "time.h"               /* to seed the random generator */
10
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13 #include "xbt/dynar.h"
14 #include "xbt/config.h"
15
16 #include "xbt/module.h"         /* this module */
17
18 #include "xbt_modinter.h"       /* prototype of other module's init/exit in XBT */
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(module, xbt, "module handling");
21
22 char *xbt_binary_name = NULL;   /* Mandatory to retrieve neat backtraces */
23 int xbt_initialized = 0;
24
25 XBT_LOG_EXTERNAL_CATEGORY(graphxml_parse);
26 XBT_LOG_EXTERNAL_CATEGORY(log);
27 XBT_LOG_EXTERNAL_CATEGORY(module);
28 XBT_LOG_EXTERNAL_CATEGORY(peer);
29 XBT_LOG_EXTERNAL_CATEGORY(strbuff);
30 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
31 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
32 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_add);
33 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_collapse);
34 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_cursor);
35 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_elm);
36 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_multi);
37 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_remove);
38 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict_search);
39 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
40 XBT_LOG_EXTERNAL_CATEGORY(xbt_ex);
41 XBT_LOG_EXTERNAL_CATEGORY(xbt_fifo);
42 XBT_LOG_EXTERNAL_CATEGORY(xbt_graph);
43 XBT_LOG_EXTERNAL_CATEGORY(xbt_matrix);
44 XBT_LOG_EXTERNAL_CATEGORY(xbt_queue);
45 XBT_LOG_EXTERNAL_CATEGORY(xbt_set);
46 XBT_LOG_EXTERNAL_CATEGORY(xbt_sync_os);
47
48 /** @brief Initialize the xbt mechanisms. */
49 void xbt_init(int *argc, char **argv)
50 {
51   xbt_initialized++;
52
53   if (xbt_initialized != 1)
54     return;
55
56   /* Connect our log channels: that must be done manually under windows */
57   XBT_LOG_CONNECT(graphxml_parse, xbt);
58   XBT_LOG_CONNECT(log, xbt);
59   XBT_LOG_CONNECT(module, xbt);
60   XBT_LOG_CONNECT(peer, xbt);
61   XBT_LOG_CONNECT(strbuff, xbt);
62   XBT_LOG_CONNECT(xbt_cfg, xbt);
63   XBT_LOG_CONNECT(xbt_dict, xbt);
64   XBT_LOG_CONNECT(xbt_dict_add, xbt_dict);
65   XBT_LOG_CONNECT(xbt_dict_collapse, xbt_dict);
66   XBT_LOG_CONNECT(xbt_dict_cursor, xbt_dict);
67   XBT_LOG_CONNECT(xbt_dict_elm, xbt_dict);
68   XBT_LOG_CONNECT(xbt_dict_multi, xbt_dict);
69   XBT_LOG_CONNECT(xbt_dict_remove, xbt_dict);
70   XBT_LOG_CONNECT(xbt_dict_search, xbt_dict);
71   XBT_LOG_CONNECT(xbt_dyn, xbt);
72   XBT_LOG_CONNECT(xbt_ex, xbt);
73   XBT_LOG_CONNECT(xbt_fifo, xbt);
74   XBT_LOG_CONNECT(xbt_graph, xbt);
75   XBT_LOG_CONNECT(xbt_matrix, xbt);
76   XBT_LOG_CONNECT(xbt_queue, xbt);
77   XBT_LOG_CONNECT(xbt_set, xbt);
78   XBT_LOG_CONNECT(xbt_sync_os, xbt);
79
80   xbt_binary_name = xbt_strdup(argv[0]);
81   srand((unsigned int) time(NULL));
82   VERB0("Initialize XBT");
83
84   xbt_backtrace_init();
85   xbt_log_init(argc, argv);
86   xbt_os_thread_mod_init();
87 }
88
89 /** @brief Finalize the xbt mechanisms. */
90 void xbt_exit()
91 {
92   xbt_initialized--;
93   if (xbt_initialized == 0) {
94     xbt_fifo_exit();
95     xbt_dict_exit();
96     xbt_os_thread_mod_exit();
97     xbt_log_exit();
98     xbt_backtrace_exit();
99     free(xbt_binary_name);
100   }
101 }
102
103
104 /* these two functions belong to xbt/sysdep.h, which have no corresponding .c file */
105 /** @brief like free, but you can be sure that it is a function  */
106 XBT_PUBLIC(void) xbt_free_f(void *p)
107 {
108   free(p);
109 }
110
111 /** @brief should be given a pointer to pointer, and frees the second one */
112 XBT_PUBLIC(void) xbt_free_ref(void *d)
113 {
114   free(*(void **) d);
115 }