Logo AND Algorithmique Numérique Distribuée

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