Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[S4U] NULL -> nullptr substitution
[simgrid.git] / src / s4u / s4u_engine.cpp
1 /* s4u::Engine Simulation Engine and global functions. */
2
3 /* Copyright (c) 2006-2015. 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 #include "simgrid/simix.h"
9 #include "mc/mc.h"
10 #include "simgrid/s4u/As.hpp"
11 #include "simgrid/s4u/engine.hpp"
12 #include "simgrid/s4u/mailbox.hpp"
13 #include "simgrid/s4u/storage.hpp"
14
15 XBT_LOG_NEW_CATEGORY(s4u,"Log channels of the S4U (Simgrid for you) interface");
16 using namespace simgrid;
17
18 s4u::Engine *s4u::Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
19
20
21 s4u::Engine::Engine(int *argc, char **argv) {
22   xbt_assert(s4u::Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
23   s4u::Engine::instance_ = this;
24
25   SIMIX_global_init(argc, argv);
26 }
27
28 s4u::Engine *s4u::Engine::instance() {
29   if (s4u::Engine::instance_ == nullptr)
30     new Engine(0,nullptr);
31   return s4u::Engine::instance_;
32 }
33
34 void s4u::Engine::shutdown() {
35   delete s4u::Engine::instance_;
36   s4u::Engine::instance_ = nullptr;
37   delete s4u::Mailbox::mailboxes;
38   delete s4u::Storage::storages_;
39 }
40
41 double s4u::Engine::getClock()
42 {
43   return SIMIX_get_clock();
44 }
45
46 void s4u::Engine::loadPlatform(const char *platf)
47 {
48   SIMIX_create_environment(platf);
49 }
50
51 void s4u::Engine::registerFunction(const char*name, int (*code)(int,char**))
52 {
53   SIMIX_function_register(name,code);
54 }
55 void s4u::Engine::registerDefault(int (*code)(int,char**))
56 {
57   SIMIX_function_register_default(code);
58 }
59 void s4u::Engine::loadDeployment(const char *deploy)
60 {
61   SIMIX_launch_application(deploy);
62 }
63
64 void s4u::Engine::run() {
65   if (MC_is_active()) {
66     MC_run();
67   } else {
68     SIMIX_run();
69   }
70 }
71
72 #include "surf/surf.h"               // routing_platf. FIXME:KILLME. SOON
73 #include "src/surf/surf_routing.hpp" // routing_platf. FIXME:KILLME. SOON
74
75 s4u::As *s4u::Engine::rootAs()
76 {
77   return routing_platf->root_; // FIXME: get the root into the Engine directly (and kill the platf)
78 }
79
80 static s4u::As *asByNameRecursive(s4u::As *current, const char *name)
81 {
82   if(!strcmp(current->name(), name))
83     return current;
84
85   xbt_dict_cursor_t cursor = nullptr;
86   char *key;
87   AS_t elem;
88   xbt_dict_foreach(current->children(), cursor, key, elem) {
89     simgrid::s4u::As *tmp = asByNameRecursive(elem, name);
90     if (tmp != nullptr )
91         return tmp;
92   }
93   return nullptr;
94 }
95 /** @brief Retrieve the AS of the given name (or nullptr if not found) */
96 s4u::As *s4u::Engine::asByNameOrNull(const char *name) {
97   return asByNameRecursive(rootAs(),name);
98 }
99