Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
create a singleton for s4u::Engine
[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/engine.hpp"
11
12 XBT_LOG_NEW_CATEGORY(s4u,"Log channels of the S4U (Simgrid for you) interface");
13 using namespace simgrid;
14
15 s4u::Engine *s4u::Engine::instance_ = nullptr; /* That singleton is awful, but I don't see no other solution right now. */
16
17
18 s4u::Engine::Engine(int *argc, char **argv) {
19   xbt_assert(s4u::Engine::instance_ == nullptr, "It is currently forbidden to create more than one instance of s4u::Engine");
20   s4u::Engine::instance_ = this;
21
22   SIMIX_global_init(argc, argv);
23 }
24
25 s4u::Engine *s4u::Engine::instance() {
26   if (s4u::Engine::instance_ == nullptr)
27     new Engine(0,nullptr);
28   return s4u::Engine::instance_;
29 }
30
31 double s4u::Engine::getClock() {
32   return SIMIX_get_clock();
33 }
34
35 void s4u::Engine::loadPlatform(const char *platf) {
36   SIMIX_create_environment(platf);
37 }
38
39 void s4u::Engine::registerFunction(const char*name, int (*code)(int,char**)) {
40   SIMIX_function_register(name,code);
41 }
42 void s4u::Engine::registerDefault(int (*code)(int,char**)) {
43   SIMIX_function_register_default(code);
44 }
45 void s4u::Engine::loadDeployment(const char *deploy) {
46   SIMIX_launch_application(deploy);
47 }
48
49 void s4u::Engine::run() {
50   if (MC_is_active()) {
51     MC_run();
52   } else {
53     SIMIX_run();
54   }
55 }