Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
logs: protect finalization against multiple calls
[simgrid.git] / src / xbt / xbt_main.cpp
1 /* module handling                                                          */
2
3 /* Copyright (c) 2006-2023. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #define XBT_LOG_LOCALLY_DEFINE_XBT_CHANNEL /* MSVC don't want it to be declared extern in headers and local here */
9
10 #include "simgrid/config.h"
11 #include "src/internal_config.h"
12 #include "src/simgrid/sg_config.hpp"
13 #include "src/sthread/sthread.h" // sthread_inside_simgrid
14 #include "src/xbt/coverage.h"
15 #include "xbt/config.hpp"
16 #include "xbt/dynar.h"
17 #include "xbt/log.h"
18 #include "xbt/log.hpp"
19 #include "xbt/misc.h"
20 #include "xbt/sysdep.h"
21
22 #include <cmath>
23 #include <cstdio>
24 #if HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #include <string>
28 #include <vector>
29
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(module, xbt, "module handling");
31
32 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories"); /* lives here even if that's a bit odd to solve linking issues: this is used in xbt_log_file_appender to detect whether SMPI is used (and thus whether we should unbench the writing to disk) */
33
34 namespace simgrid::xbt {
35 std::string binary_name;          /* Name of the system process containing us (mandatory to retrieve neat backtraces) */
36 std::vector<std::string> cmdline; /* all we got in argv */
37 } // namespace simgrid::xbt
38
39 const int xbt_pagesize = static_cast<int>(sysconf(_SC_PAGESIZE));
40 const int xbt_pagebits = static_cast<int>(log2(xbt_pagesize));
41
42 XBT_ATTRIB_NOINLINE void sthread_enable()
43 { // These symbols are used from ContextSwapped in any case, but they are only useful
44   asm("");
45 }
46 XBT_ATTRIB_NOINLINE void sthread_disable()
47 { //  when libsthread is LD_PRELOADED. In this case, sthread's implem gets used instead.
48   asm("");
49 }
50
51 /* these two functions belong to xbt/sysdep.h, which have no corresponding .c file */
52 /** @brief like xbt_free, but you can be sure that it is a function  */
53 void xbt_free_f(void* p) noexcept(noexcept(::free))
54 {
55   xbt_free(p);
56 }
57
58 /** @brief should be given a pointer to pointer, and frees the second one */
59 void xbt_free_ref(void* d) noexcept(noexcept(::free))
60 {
61   xbt_free(*(void**)d);
62 }
63
64 /** @brief Kill the program in silence */
65 void xbt_abort()
66 {
67   /* Call __gcov_flush on abort when compiling with coverage options. */
68   coverage_checkpoint();
69   abort();
70 }
71
72 #ifndef HAVE_SMPI
73 int SMPI_is_inited()
74 {
75   return false;
76 }
77 #endif