Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
19c4084f77f26a3de29d7da731cc88dc2973fb0c
[simgrid.git] / src / simix / popping_private.hpp
1 /* Copyright (c) 2007-2022. 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 #ifndef SG_POPPING_PRIVATE_HPP
7 #define SG_POPPING_PRIVATE_HPP
8
9 #include "simgrid/forward.h"
10 #include "src/kernel/activity/ActivityImpl.hpp"
11
12 #include <array>
13 #include <boost/intrusive_ptr.hpp>
14
15 /********************************* Simcalls *********************************/
16 #include "popping_enum.hpp" /* Definition of Simcall, with one value per simcall */
17
18 XBT_PUBLIC_DATA const std::array<const char*, simgrid::simix::NUM_SIMCALLS> simcall_names; /* Name of each simcall */
19
20 using simix_match_func_t     = bool (*)(void*, void*, simgrid::kernel::activity::CommImpl*);
21 using simix_copy_data_func_t = void (*)(simgrid::kernel::activity::CommImpl*, void*, size_t);
22 using simix_clean_func_t     = void (*)(void*);
23 using FPtr                   = void (*)(); // Hide the ugliness
24
25 /* Pack all possible scalar types in an union */
26 union u_smx_scalar {
27   bool b;
28   char c;
29   short s;
30   int i;
31   long l;
32   long long ll;
33   unsigned char uc;
34   unsigned short us;
35   unsigned int ui;
36   unsigned long ul;
37   unsigned long long ull;
38   double d;
39   void* dp;
40   FPtr fp;
41 };
42
43 /**
44  * @brief Represents a simcall to the kernel.
45  */
46 struct s_smx_simcall {
47   simgrid::simix::Simcall call_                      = simgrid::simix::Simcall::NONE;
48   smx_actor_t issuer_                                = nullptr;
49   simgrid::kernel::timer::Timer* timeout_cb_         = nullptr; // Callback to timeouts
50   simgrid::kernel::actor::SimcallObserver* observer_ = nullptr; // makes that simcall observable by the MC
51   unsigned int mc_max_consider_ =
52       0; // How many times this simcall should be used. If >1, this will be a fork in the state space.
53   std::array<u_smx_scalar, 11> args_      = {};
54   u_smx_scalar result_                    = {};
55 };
56
57 /******************************** General *************************************/
58
59 XBT_PRIVATE const char* SIMIX_simcall_name(const s_smx_simcall& simcall);
60 XBT_PRIVATE void SIMIX_run_kernel(std::function<void()> const* code);
61 XBT_PRIVATE void SIMIX_run_blocking(std::function<void()> const* code);
62
63 /* Defines the marshal/unmarshal functions for each type of parameters.
64  *
65  * There is a unmarshal_raw() function, which is exactly similar to unmarshal()
66  * for all types but boost::intrusive_ptr(T). For that type, the unmarshal()
67  * function builds a new intrusive_ptr wrapping the pointer (that is stored raw
68  * within the simcall) while the unmarshal_raw retrieves the raw pointer.
69  *
70  * This is used in <simcall>_getraw_<param> functions, that allow the
71  * model-checker, to read the data in the remote memory of the MCed.
72  */
73
74 namespace simgrid {
75 namespace simix {
76
77 template <class T> class type {
78   constexpr bool operator==(type) const { return true; }
79   template <class U> constexpr bool operator==(type<U>) const { return false; }
80   constexpr bool operator!=(type) const { return false; }
81   template <class U> constexpr bool operator!=(type<U>) const { return true; }
82 };
83
84 template <typename T> struct marshal_t {
85 };
86 #define SIMIX_MARSHAL(T, field)                                                                                        \
87   inline void marshal(type<T>, u_smx_scalar& simcall, T value) { simcall.field = value; }                              \
88   inline T unmarshal(type<T>, u_smx_scalar const& simcall) { return simcall.field; }                                   \
89   inline T unmarshal_raw(type<T>, u_smx_scalar const& simcall)                                                         \
90   { /* Exactly same as unmarshal. It differs only for intrusive_ptr */ return simcall.field; }
91
92 SIMIX_MARSHAL(bool, b)
93 SIMIX_MARSHAL(char, c)
94 SIMIX_MARSHAL(short, s)
95 SIMIX_MARSHAL(int, i)
96 SIMIX_MARSHAL(long, l)
97 SIMIX_MARSHAL(unsigned char, uc)
98 SIMIX_MARSHAL(unsigned short, us)
99 SIMIX_MARSHAL(unsigned int, ui)
100 SIMIX_MARSHAL(unsigned long, ul)
101 SIMIX_MARSHAL(unsigned long long, ull)
102 SIMIX_MARSHAL(long long, ll)
103 SIMIX_MARSHAL(float, d)
104 SIMIX_MARSHAL(double, d)
105 SIMIX_MARSHAL(FPtr, fp)
106
107 inline void unmarshal(type<void>, u_smx_scalar const& /*simcall*/)
108 {
109   /* Nothing to do for void data */
110 }
111 inline void unmarshal_raw(type<void>, u_smx_scalar const& /*simcall*/)
112 {
113   /* Nothing to do for void data */
114 }
115
116 template <class T> inline void marshal(type<T*>, u_smx_scalar& simcall, T* value)
117 {
118   simcall.dp = (void*)value;
119 }
120 template <class T> inline T* unmarshal(type<T*>, u_smx_scalar const& simcall)
121 {
122   return static_cast<T*>(simcall.dp);
123 }
124 template <class T> inline T* unmarshal_raw(type<T*>, u_smx_scalar const& simcall)
125 {
126   return static_cast<T*>(simcall.dp);
127 }
128
129 template <class T>
130 inline void marshal(type<boost::intrusive_ptr<T>>, u_smx_scalar& simcall, boost::intrusive_ptr<T> value)
131 {
132   if (value.get() == nullptr) { // Sometimes we return nullptr in an intrusive_ptr...
133     simcall.dp = nullptr;
134   } else {
135     intrusive_ptr_add_ref(value.get());
136     simcall.dp = static_cast<void*>(value.get());
137   }
138 }
139 template <class T> inline boost::intrusive_ptr<T> unmarshal(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
140 {
141   // refcount was already increased during the marshaling, thus the "false" as last argument
142   boost::intrusive_ptr<T> res = boost::intrusive_ptr<T>(static_cast<T*>(simcall.dp), false);
143   return res;
144 }
145 template <class T> inline T* unmarshal_raw(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
146 {
147   return static_cast<T*>(simcall.dp);
148 }
149
150 template <class R, class... T> inline void marshal(type<R (*)(T...)>, u_smx_scalar& simcall, R (*value)(T...))
151 {
152   simcall.fp = (FPtr)value;
153 }
154 template <class R, class... T> inline auto unmarshal(type<R (*)(T...)>, u_smx_scalar simcall) -> R (*)(T...)
155 {
156   return (R(*)(T...))simcall.fp;
157 }
158 template <class R, class... T> inline auto unmarshal_raw(type<R (*)(T...)>, u_smx_scalar simcall) -> R (*)(T...)
159 {
160   return (R(*)(T...))simcall.fp;
161 }
162
163 template <class T> inline void marshal(u_smx_scalar& simcall, T const& value)
164 {
165   return marshal(type<T>(), simcall, value);
166 }
167 template <class T> inline typename std::remove_reference_t<T> unmarshal(u_smx_scalar& simcall)
168 {
169   return unmarshal(type<T>(), simcall);
170 }
171 template <class T> inline typename std::remove_reference_t<T> unmarshal_raw(u_smx_scalar& simcall)
172 {
173   return unmarshal(type<T>(), simcall);
174 }
175
176 template <std::size_t I> inline void marshal_args(const s_smx_simcall* /*simcall*/)
177 {
178   /* Nothing to do when no args */
179 }
180
181 template <std::size_t I, class A> inline void marshal_args(smx_simcall_t simcall, A const& a)
182 {
183   marshal(simcall->args_[I], a);
184 }
185
186 template <std::size_t I, class A, class... B> inline void marshal_args(smx_simcall_t simcall, A const& a, B const&... b)
187 {
188   marshal(simcall->args_[I], a);
189   marshal_args<I + 1>(simcall, b...);
190 }
191
192 /** Initialize the simcall */
193 template <class... A> inline void marshal(smx_simcall_t simcall, Simcall call, A const&... a)
194 {
195   simcall->call_ = call;
196   memset(&simcall->result_, 0, sizeof simcall->result_);
197   memset(simcall->args_.data(), 0, simcall->args_.size() * sizeof simcall->args_[0]);
198   marshal_args<0>(simcall, a...);
199 }
200 }
201 }
202
203 #endif