Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / kernel / actor / Simcall.cpp
1 /* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/kernel/actor/Simcall.hpp"
7 #include "simgrid/modelchecker.h"
8 #include "simgrid/s4u/Host.hpp"
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11 #include "src/kernel/actor/SimcallObserver.hpp"
12 #include "src/kernel/context/Context.hpp"
13 #include "src/mc/mc_replay.hpp"
14 #include "xbt/log.h"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_simcall, kernel, "transmuting from user request into kernel handlers");
17
18 namespace simgrid::kernel::actor {
19
20 /** @brief returns a printable string representing a simcall */
21 const char* Simcall::get_cname() const
22 {
23   if (observer_ != nullptr) {
24     static std::string name;
25     name              = boost::core::demangle(typeid(*observer_).name());
26     const char* cname = name.c_str();
27     if (name.rfind("simgrid::kernel::", 0) == 0)
28       cname += 17; // strip prefix "simgrid::kernel::"
29     return cname;
30   } else {
31     return to_c_str(call_);
32   }
33 }
34 ObjectAccessSimcallItem::ObjectAccessSimcallItem()
35 {
36   take_ownership();
37 }
38 void ObjectAccessSimcallItem::take_ownership()
39 {
40   simcall_owner_ = ActorImpl::self();
41 }
42
43 } // namespace simgrid::kernel::actor
44
45 static void simcall(simgrid::kernel::actor::Simcall::Type call, std::function<void()> const& code,
46                     simgrid::kernel::actor::SimcallObserver* observer)
47 {
48   auto* self               = simgrid::kernel::actor::ActorImpl::self();
49   self->simcall_.call_     = call;
50   self->simcall_.code_     = &code;
51   self->simcall_.observer_ = observer;
52   if (simgrid::kernel::EngineImpl::get_instance()->is_maestro(self)) {
53     self->simcall_handle(0);
54   } else {
55     XBT_DEBUG("Yield process '%s' on simcall %s", self->get_cname(), self->simcall_.get_cname());
56     self->yield();
57   }
58   self->simcall_.observer_ = nullptr;
59 }
60
61 void simcall_run_answered(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
62 {
63   // The function `code` is called in kernel mode (either because we are already in maestor or after a context switch)
64   // and simcall_answer() is called
65   simcall(simgrid::kernel::actor::Simcall::Type::RUN_ANSWERED, code, observer);
66 }
67
68 void simcall_run_blocking(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
69 {
70   // The function `code` is called in kernel mode (either because we are already in maestor or after a context switch)
71   // BUT simcall_answer IS NOT CALLED
72   simcall(simgrid::kernel::actor::Simcall::Type::RUN_BLOCKING, code, observer);
73 }
74
75 void simcall_run_object_access(std::function<void()> const& code, simgrid::kernel::actor::ObjectAccessSimcallItem* item)
76 {
77   auto* self = simgrid::kernel::actor::ActorImpl::self();
78
79   // We only need a simcall if the order of the setters is important (parallel run or MC execution).
80   // Otherwise, just call the function with no simcall
81
82   if (simgrid::kernel::context::Context::is_parallel() || MC_is_active() || MC_record_replay_is_active()) {
83     simgrid::kernel::actor::ObjectAccessSimcallObserver observer{self, item};
84     simcall(simgrid::kernel::actor::Simcall::Type::RUN_ANSWERED, code, &observer);
85     item->take_ownership();
86   } else {
87     // don't return from the context-switch we don't do
88     self->simcall_.call_     = simgrid::kernel::actor::Simcall::Type::RUN_BLOCKING;
89     self->simcall_.code_     = &code;
90     self->simcall_.observer_ = nullptr;
91     self->simcall_handle(0);
92   }
93 }