1 /* Copyright (c) 2007-2017. 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_H
7 #define SG_POPPING_PRIVATE_H
10 #include <simgrid/simix.h>
12 #include <src/kernel/activity/ActivityImpl.hpp>
13 #include <src/kernel/activity/CommImpl.hpp>
15 #include <boost/intrusive_ptr.hpp>
19 /********************************* Simcalls *********************************/
20 XBT_PUBLIC_DATA(const char*) simcall_names[]; /* Name of each simcall */
22 #include "popping_enum.h" /* Definition of e_smx_simcall_t, with one value per simcall */
24 typedef int (*simix_match_func_t)(void *, void *, smx_activity_t);
25 typedef void (*simix_copy_data_func_t)(smx_activity_t, void*, size_t);
26 typedef void (*simix_clean_func_t)(void *);
27 typedef void (*FPtr)(void); // Hide the ugliness
29 /* Pack all possible scalar types in an union */
40 unsigned long long ull;
47 * \brief Represents a simcall to the kernel.
49 struct s_smx_simcall {
54 union u_smx_scalar args[11];
55 union u_smx_scalar result;
58 #define SIMCALL_SET_MC_VALUE(simcall, value) ((simcall)->mc_value = (value))
59 #define SIMCALL_GET_MC_VALUE(simcall) ((simcall)->mc_value)
61 /******************************** General *************************************/
63 XBT_PRIVATE void SIMIX_simcall_answer(smx_simcall_t simcall);
64 XBT_PRIVATE void SIMIX_simcall_handle(smx_simcall_t simcall, int value);
65 XBT_PRIVATE void SIMIX_simcall_exit(smx_activity_t synchro);
66 XBT_PRIVATE const char *SIMIX_simcall_name(e_smx_simcall_t kind);
67 XBT_PRIVATE void SIMIX_run_kernel(std::function<void()> const* code);
68 XBT_PRIVATE void SIMIX_run_blocking(std::function<void()> const* code);
74 /* Defines the marshal/unmarshal functions for each type of parameters.
76 * They will be used in popping_accessors.h to define the functions allowing
77 * to retrieve/set each parameter of each simcall.
79 * There is a unmarshal_raw() function, which is exactly similar to unmarshal()
80 * for all types but boost::intrusive_ptr(T). For that type, the unmarshal()
81 * function builds a new intrusive_ptr wrapping the pointer (that is stored raw
82 * within the simcall) while the unmarshal_raw retrieves the raw pointer.
84 * This is used in <simcall>_getraw_<param> functions, that allow the
85 * model-checker, to read the data in the remote memory of the MCed.
93 constexpr bool operator==(type) const { return true; }
95 constexpr bool operator==(type<U>) const { return false; }
96 constexpr bool operator!=(type) const { return false; }
98 constexpr bool operator!=(type<U>) const { return true; }
101 template<typename T> struct marshal_t {};
102 #define SIMIX_MARSHAL(T, field) \
103 inline void marshal(type<T>, u_smx_scalar& simcall, T value) { simcall.field = value; } \
104 inline T unmarshal(type<T>, u_smx_scalar const& simcall) { return simcall.field; } \
105 inline T unmarshal_raw(type<T>, u_smx_scalar const& simcall) \
106 { /* Exactly same as unmarshal. It differs only for intrusive_ptr */ return simcall.field; }
108 SIMIX_MARSHAL(char, c);
109 SIMIX_MARSHAL(short, s);
110 SIMIX_MARSHAL(int, i);
111 SIMIX_MARSHAL(long, l);
112 SIMIX_MARSHAL(unsigned char, uc);
113 SIMIX_MARSHAL(unsigned short, us);
114 SIMIX_MARSHAL(unsigned int, ui);
115 SIMIX_MARSHAL(unsigned long, ul);
116 SIMIX_MARSHAL(unsigned long long, ull);
117 SIMIX_MARSHAL(long long, ll);
118 SIMIX_MARSHAL(float, d);
119 SIMIX_MARSHAL(double, d);
120 SIMIX_MARSHAL(FPtr, fp);
122 inline void unmarshal(type<void>, u_smx_scalar const& simcall)
125 inline void unmarshal_raw(type<void>, u_smx_scalar const& simcall)
129 template<class T> inline
130 void marshal(type<T*>, u_smx_scalar& simcall, T* value)
132 simcall.dp = (void*) value;
134 template<class T> inline
135 T* unmarshal(type<T*>, u_smx_scalar const& simcall)
137 return static_cast<T*>(simcall.dp);
139 template <class T> inline T* unmarshal_raw(type<T*>, u_smx_scalar const& simcall)
141 return static_cast<T*>(simcall.dp);
145 inline void marshal(type<boost::intrusive_ptr<T>>, u_smx_scalar& simcall, boost::intrusive_ptr<T> value)
147 if (value.get() == nullptr) { // Sometimes we return nullptr in an intrusive_ptr...
148 simcall.dp = nullptr;
150 intrusive_ptr_add_ref(&*value);
151 simcall.dp = static_cast<void*>(&*value);
154 template <class T> inline boost::intrusive_ptr<T> unmarshal(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
156 // refcount was already increased during the marshaling, thus the "false" as last argument
157 boost::intrusive_ptr<T> res = boost::intrusive_ptr<T>(static_cast<T*>(simcall.dp), false);
160 template <class T> inline T* unmarshal_raw(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
162 return static_cast<T*>(simcall.dp);
165 template<class R, class... T> inline
166 void marshal(type<R(*)(T...)>, u_smx_scalar& simcall, R(*value)(T...))
168 simcall.fp = (FPtr) value;
170 template<class R, class... T> inline
171 auto unmarshal(type<R(*)(T...)>, u_smx_scalar simcall) -> R(*)(T...)
173 return (R(*)(T...)) simcall.fp;
175 template <class R, class... T> inline auto unmarshal_raw(type<R (*)(T...)>, u_smx_scalar simcall) -> R (*)(T...)
177 return (R(*)(T...))simcall.fp;
180 template<class T> inline
181 void marshal(u_smx_scalar& simcall, T const& value)
183 return marshal(type<T>(), simcall, value);
185 template<class T> inline
186 typename std::remove_reference<T>::type unmarshal(u_smx_scalar& simcall)
188 return unmarshal(type<T>(), simcall);
190 template <class T> inline typename std::remove_reference<T>::type unmarshal_raw(u_smx_scalar& simcall)
192 return unmarshal(type<T>(), simcall);
195 template<std::size_t I>
196 inline void marshalArgs(smx_simcall_t simcall) {}
198 template<std::size_t I, class A>
199 inline void marshalArgs(smx_simcall_t simcall, A const& a)
201 marshal(simcall->args[I], a);
204 template<std::size_t I, class A, class... B>
205 inline void marshalArgs(smx_simcall_t simcall, A const& a, B const&... b)
207 marshal(simcall->args[I], a);
208 marshalArgs<I+1>(simcall, b...);
211 /** Initialize the simcall */
212 template<class... A> inline
213 void marshal(smx_simcall_t simcall, e_smx_simcall_t call, A const&... a)
215 simcall->call = call;
216 memset(&simcall->result, 0, sizeof(simcall->result));
217 memset(simcall->args, 0, sizeof(simcall->args));
218 marshalArgs<0>(simcall, a...);
226 #include "popping_accessors.h"