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_HPP
7 #define SG_POPPING_PRIVATE_HPP
10 #include <simgrid/simix.h>
12 #include <src/kernel/activity/ActivityImpl.hpp>
13 #include <src/kernel/activity/CommImpl.hpp>
14 #include <src/kernel/activity/ExecImpl.hpp>
16 #include <boost/intrusive_ptr.hpp>
20 /********************************* Simcalls *********************************/
21 XBT_PUBLIC_DATA(const char*) simcall_names[]; /* Name of each simcall */
23 #include "popping_enum.h" /* Definition of e_smx_simcall_t, with one value per simcall */
25 typedef int (*simix_match_func_t)(void*, void*, simgrid::kernel::activity::CommImpl*);
26 typedef void (*simix_copy_data_func_t)(smx_activity_t, void*, size_t);
27 typedef void (*simix_clean_func_t)(void *);
28 typedef void (*FPtr)(void); // Hide the ugliness
30 /* Pack all possible scalar types in an union */
41 unsigned long long ull;
48 * \brief Represents a simcall to the kernel.
50 struct s_smx_simcall {
55 union u_smx_scalar args[11];
56 union u_smx_scalar result;
59 #define SIMCALL_SET_MC_VALUE(simcall, value) ((simcall)->mc_value = (value))
60 #define SIMCALL_GET_MC_VALUE(simcall) ((simcall)->mc_value)
62 /******************************** General *************************************/
64 XBT_PRIVATE void SIMIX_simcall_answer(smx_simcall_t simcall);
65 XBT_PRIVATE void SIMIX_simcall_handle(smx_simcall_t simcall, int value);
66 XBT_PRIVATE void SIMIX_simcall_exit(smx_activity_t synchro);
67 XBT_PRIVATE const char *SIMIX_simcall_name(e_smx_simcall_t kind);
68 XBT_PRIVATE void SIMIX_run_kernel(std::function<void()> const* code);
69 XBT_PRIVATE void SIMIX_run_blocking(std::function<void()> const* code);
73 /* Defines the marshal/unmarshal functions for each type of parameters.
75 * They will be used in popping_accessors.hpp to define the functions allowing
76 * to retrieve/set each parameter of each simcall.
78 * There is a unmarshal_raw() function, which is exactly similar to unmarshal()
79 * for all types but boost::intrusive_ptr(T). For that type, the unmarshal()
80 * function builds a new intrusive_ptr wrapping the pointer (that is stored raw
81 * within the simcall) while the unmarshal_raw retrieves the raw pointer.
83 * This is used in <simcall>_getraw_<param> functions, that allow the
84 * model-checker, to read the data in the remote memory of the MCed.
92 constexpr bool operator==(type) const { return true; }
94 constexpr bool operator==(type<U>) const { return false; }
95 constexpr bool operator!=(type) const { return false; }
97 constexpr bool operator!=(type<U>) const { return true; }
100 template<typename T> struct marshal_t {};
101 #define SIMIX_MARSHAL(T, field) \
102 inline void marshal(type<T>, u_smx_scalar& simcall, T value) { simcall.field = value; } \
103 inline T unmarshal(type<T>, u_smx_scalar const& simcall) { return simcall.field; } \
104 inline T unmarshal_raw(type<T>, u_smx_scalar const& simcall) \
105 { /* Exactly same as unmarshal. It differs only for intrusive_ptr */ return simcall.field; }
107 SIMIX_MARSHAL(char, c);
108 SIMIX_MARSHAL(short, s);
109 SIMIX_MARSHAL(int, i);
110 SIMIX_MARSHAL(long, l);
111 SIMIX_MARSHAL(unsigned char, uc);
112 SIMIX_MARSHAL(unsigned short, us);
113 SIMIX_MARSHAL(unsigned int, ui);
114 SIMIX_MARSHAL(unsigned long, ul);
115 SIMIX_MARSHAL(unsigned long long, ull);
116 SIMIX_MARSHAL(long long, ll);
117 SIMIX_MARSHAL(float, d);
118 SIMIX_MARSHAL(double, d);
119 SIMIX_MARSHAL(FPtr, fp);
121 inline void unmarshal(type<void>, u_smx_scalar const& simcall)
123 /* Nothing to do for void data */
125 inline void unmarshal_raw(type<void>, u_smx_scalar const& simcall)
127 /* Nothing to do for void data */
130 template<class T> inline
131 void marshal(type<T*>, u_smx_scalar& simcall, T* value)
133 simcall.dp = (void*) value;
135 template<class T> inline
136 T* unmarshal(type<T*>, u_smx_scalar const& simcall)
138 return static_cast<T*>(simcall.dp);
140 template <class T> inline T* unmarshal_raw(type<T*>, u_smx_scalar const& simcall)
142 return static_cast<T*>(simcall.dp);
146 inline void marshal(type<boost::intrusive_ptr<T>>, u_smx_scalar& simcall, boost::intrusive_ptr<T> value)
148 if (value.get() == nullptr) { // Sometimes we return nullptr in an intrusive_ptr...
149 simcall.dp = nullptr;
151 intrusive_ptr_add_ref(&*value);
152 simcall.dp = static_cast<void*>(&*value);
155 template <class T> inline boost::intrusive_ptr<T> unmarshal(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
157 // refcount was already increased during the marshaling, thus the "false" as last argument
158 boost::intrusive_ptr<T> res = boost::intrusive_ptr<T>(static_cast<T*>(simcall.dp), false);
161 template <class T> inline T* unmarshal_raw(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
163 return static_cast<T*>(simcall.dp);
166 template<class R, class... T> inline
167 void marshal(type<R(*)(T...)>, u_smx_scalar& simcall, R(*value)(T...))
169 simcall.fp = (FPtr) value;
171 template<class R, class... T> inline
172 auto unmarshal(type<R(*)(T...)>, u_smx_scalar simcall) -> R(*)(T...)
174 return (R(*)(T...)) simcall.fp;
176 template <class R, class... T> inline auto unmarshal_raw(type<R (*)(T...)>, u_smx_scalar simcall) -> R (*)(T...)
178 return (R(*)(T...))simcall.fp;
181 template<class T> inline
182 void marshal(u_smx_scalar& simcall, T const& value)
184 return marshal(type<T>(), simcall, value);
186 template<class T> inline
187 typename std::remove_reference<T>::type unmarshal(u_smx_scalar& simcall)
189 return unmarshal(type<T>(), simcall);
191 template <class T> inline typename std::remove_reference<T>::type unmarshal_raw(u_smx_scalar& simcall)
193 return unmarshal(type<T>(), simcall);
196 template<std::size_t I>
197 inline void marshalArgs(smx_simcall_t simcall) {}
199 template<std::size_t I, class A>
200 inline void marshalArgs(smx_simcall_t simcall, A const& a)
202 marshal(simcall->args[I], a);
205 template<std::size_t I, class A, class... B>
206 inline void marshalArgs(smx_simcall_t simcall, A const& a, B const&... b)
208 marshal(simcall->args[I], a);
209 marshalArgs<I+1>(simcall, b...);
212 /** Initialize the simcall */
213 template<class... A> inline
214 void marshal(smx_simcall_t simcall, e_smx_simcall_t call, A const&... a)
216 simcall->call = call;
217 memset(&simcall->result, 0, sizeof(simcall->result));
218 memset(simcall->args, 0, sizeof(simcall->args));
219 marshalArgs<0>(simcall, a...);
225 #include "popping_accessors.hpp"