Logo AND Algorithmique Numérique Distribuée

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