1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SG_POPPING_PRIVATE_HPP
7 #define SG_POPPING_PRIVATE_HPP
9 #include "simgrid/forward.h"
10 #include "src/kernel/activity/ActivityImpl.hpp"
13 #include <boost/intrusive_ptr.hpp>
15 /********************************* Simcalls *********************************/
16 #include "popping_enum.hpp" /* Definition of Simcall, with one value per simcall */
18 XBT_PUBLIC_DATA const std::array<const char*, simgrid::simix::NUM_SIMCALLS> simcall_names; /* Name of each simcall */
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
25 /* Pack all possible scalar types in an union */
37 unsigned long long ull;
44 * @brief Represents a simcall to the kernel.
46 struct s_smx_simcall {
47 simgrid::simix::Simcall call_ = simgrid::simix::Simcall::NONE;
48 smx_actor_t issuer_ = nullptr;
49 smx_timer_t timeout_cb_ = nullptr; // Callback to timeouts
50 simgrid::mc::SimcallInspector* inspector_ = nullptr; // makes that simcall observable by the MC
51 unsigned int mc_max_consider_ = 0; // How many times this simcall should be used. If >1, this will be a fork.
53 std::array<u_smx_scalar, 11> args_ = {};
54 u_smx_scalar result_ = {};
57 #define SIMCALL_SET_MC_VALUE(simcall, value) ((simcall).mc_value_ = (value))
58 #define SIMCALL_GET_MC_VALUE(simcall) ((simcall).mc_value_)
60 /******************************** General *************************************/
62 XBT_PRIVATE const char* SIMIX_simcall_name(simgrid::simix::Simcall kind);
63 XBT_PRIVATE void SIMIX_run_kernel(std::function<void()> const* code);
64 XBT_PRIVATE void SIMIX_run_blocking(std::function<void()> const* code);
66 /* Defines the marshal/unmarshal functions for each type of parameters.
68 * They will be used in popping_accessors.hpp to define the functions allowing
69 * to retrieve/set each parameter of each simcall.
71 * There is a unmarshal_raw() function, which is exactly similar to unmarshal()
72 * for all types but boost::intrusive_ptr(T). For that type, the unmarshal()
73 * function builds a new intrusive_ptr wrapping the pointer (that is stored raw
74 * within the simcall) while the unmarshal_raw retrieves the raw pointer.
76 * This is used in <simcall>_getraw_<param> functions, that allow the
77 * model-checker, to read the data in the remote memory of the MCed.
83 template <class T> class type {
84 constexpr bool operator==(type) const { return true; }
85 template <class U> constexpr bool operator==(type<U>) const { return false; }
86 constexpr bool operator!=(type) const { return false; }
87 template <class U> constexpr bool operator!=(type<U>) const { return true; }
90 template <typename T> struct marshal_t {
92 #define SIMIX_MARSHAL(T, field) \
93 inline void marshal(type<T>, u_smx_scalar& simcall, T value) { simcall.field = value; } \
94 inline T unmarshal(type<T>, u_smx_scalar const& simcall) { return simcall.field; } \
95 inline T unmarshal_raw(type<T>, u_smx_scalar const& simcall) \
96 { /* Exactly same as unmarshal. It differs only for intrusive_ptr */ return simcall.field; }
98 SIMIX_MARSHAL(bool, b)
99 SIMIX_MARSHAL(char, c)
100 SIMIX_MARSHAL(short, s)
101 SIMIX_MARSHAL(int, i)
102 SIMIX_MARSHAL(long, l)
103 SIMIX_MARSHAL(unsigned char, uc)
104 SIMIX_MARSHAL(unsigned short, us)
105 SIMIX_MARSHAL(unsigned int, ui)
106 SIMIX_MARSHAL(unsigned long, ul)
107 SIMIX_MARSHAL(unsigned long long, ull)
108 SIMIX_MARSHAL(long long, ll)
109 SIMIX_MARSHAL(float, d)
110 SIMIX_MARSHAL(double, d)
111 SIMIX_MARSHAL(FPtr, fp)
113 inline void unmarshal(type<void>, u_smx_scalar const& /*simcall*/)
115 /* Nothing to do for void data */
117 inline void unmarshal_raw(type<void>, u_smx_scalar const& /*simcall*/)
119 /* Nothing to do for void data */
122 template <class T> inline void marshal(type<T*>, u_smx_scalar& simcall, T* value)
124 simcall.dp = (void*)value;
126 template <class T> inline T* unmarshal(type<T*>, u_smx_scalar const& simcall)
128 return static_cast<T*>(simcall.dp);
130 template <class T> inline T* unmarshal_raw(type<T*>, u_smx_scalar const& simcall)
132 return static_cast<T*>(simcall.dp);
136 inline void marshal(type<boost::intrusive_ptr<T>>, u_smx_scalar& simcall, boost::intrusive_ptr<T> value)
138 if (value.get() == nullptr) { // Sometimes we return nullptr in an intrusive_ptr...
139 simcall.dp = nullptr;
141 intrusive_ptr_add_ref(value.get());
142 simcall.dp = static_cast<void*>(value.get());
145 template <class T> inline boost::intrusive_ptr<T> unmarshal(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
147 // refcount was already increased during the marshaling, thus the "false" as last argument
148 boost::intrusive_ptr<T> res = boost::intrusive_ptr<T>(static_cast<T*>(simcall.dp), false);
151 template <class T> inline T* unmarshal_raw(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
153 return static_cast<T*>(simcall.dp);
156 template <class R, class... T> inline void marshal(type<R (*)(T...)>, u_smx_scalar& simcall, R (*value)(T...))
158 simcall.fp = (FPtr)value;
160 template <class R, class... T> inline auto unmarshal(type<R (*)(T...)>, u_smx_scalar simcall) -> R (*)(T...)
162 return (R(*)(T...))simcall.fp;
164 template <class R, class... T> inline auto unmarshal_raw(type<R (*)(T...)>, u_smx_scalar simcall) -> R (*)(T...)
166 return (R(*)(T...))simcall.fp;
169 template <class T> inline void marshal(u_smx_scalar& simcall, T const& value)
171 return marshal(type<T>(), simcall, value);
173 template <class T> inline typename std::remove_reference_t<T> unmarshal(u_smx_scalar& simcall)
175 return unmarshal(type<T>(), simcall);
177 template <class T> inline typename std::remove_reference_t<T> unmarshal_raw(u_smx_scalar& simcall)
179 return unmarshal(type<T>(), simcall);
182 template <std::size_t I> inline void marshal_args(const s_smx_simcall* /*simcall*/)
184 /* Nothing to do when no args */
187 template <std::size_t I, class A> inline void marshal_args(smx_simcall_t simcall, A const& a)
189 marshal(simcall->args_[I], a);
192 template <std::size_t I, class A, class... B> inline void marshal_args(smx_simcall_t simcall, A const& a, B const&... b)
194 marshal(simcall->args_[I], a);
195 marshal_args<I + 1>(simcall, b...);
198 /** Initialize the simcall */
199 template <class... A> inline void marshal(smx_simcall_t simcall, Simcall call, A const&... a)
201 simcall->call_ = call;
202 memset(&simcall->result_, 0, sizeof simcall->result_);
203 memset(simcall->args_.data(), 0, simcall->args_.size() * sizeof simcall->args_[0]);
204 marshal_args<0>(simcall, a...);
209 #include "popping_accessors.hpp"