Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar and simplify simcalls (before killing them)
[simgrid.git] / src / simix / popping_private.h
1 /* Copyright (c) 2007-2017. 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_H
7 #define SG_POPPING_PRIVATE_H
8
9 #include <xbt/base.h>
10 #include <simgrid/simix.h>
11
12 #include <src/kernel/activity/ActivityImpl.hpp>
13 #include <src/kernel/activity/CommImpl.hpp>
14
15 #include <boost/intrusive_ptr.hpp>
16
17 SG_BEGIN_DECL()
18
19 /********************************* Simcalls *********************************/
20 XBT_PUBLIC_DATA(const char*) simcall_names[]; /* Name of each simcall */
21
22 #include "popping_enum.h" /* Definition of e_smx_simcall_t, with one value per simcall */
23
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
28
29 /* Pack all possible scalar types in an union */
30 union u_smx_scalar {
31   char            c;
32   short           s;
33   int             i;
34   long            l;
35   long long       ll;
36   unsigned char   uc;
37   unsigned short  us;
38   unsigned int    ui;
39   unsigned long   ul;
40   unsigned long long ull;
41   double          d;
42   void*           dp;
43   FPtr            fp;
44 };
45
46 /**
47  * \brief Represents a simcall to the kernel.
48  */
49 struct s_smx_simcall {
50   e_smx_simcall_t call;
51   smx_actor_t issuer;
52   smx_timer_t timer;
53   int mc_value;
54   union u_smx_scalar args[11];
55   union u_smx_scalar result;
56 };
57
58 #define SIMCALL_SET_MC_VALUE(simcall, value) ((simcall)->mc_value = (value))
59 #define SIMCALL_GET_MC_VALUE(simcall) ((simcall)->mc_value)
60
61 /******************************** General *************************************/
62
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);
69
70 SG_END_DECL()
71
72 #ifdef __cplusplus
73
74 /* Defines the marshal/unmarshal functions for each type of parameters.
75  *
76  * They will be used in popping_accessors.h to define the functions allowing
77  * to retrieve/set each parameter of each simcall.
78  *
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.
83  *
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.
86  */
87
88 namespace simgrid {
89 namespace simix {
90
91 template<class T>
92 class type {
93   constexpr bool operator==(type) const    { return true; }
94   template<class U>
95   constexpr bool operator==(type<U>) const { return false; }
96   constexpr bool operator!=(type) const    { return false; }
97   template<class U>
98   constexpr bool operator!=(type<U>) const { return true; }
99 };
100
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; }
107
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);
121
122 inline void unmarshal(type<void>, u_smx_scalar const& simcall)
123 {
124   /* Nothing to do for void data */
125 }
126 inline void unmarshal_raw(type<void>, u_smx_scalar const& simcall)
127 {
128   /* Nothing to do for void data */
129 }
130
131 template<class T> inline
132 void marshal(type<T*>, u_smx_scalar& simcall, T* value)
133 {
134   simcall.dp = (void*) value;
135 }
136 template<class T> inline
137 T* unmarshal(type<T*>, u_smx_scalar const& simcall)
138 {
139   return static_cast<T*>(simcall.dp);
140 }
141 template <class T> inline T* unmarshal_raw(type<T*>, u_smx_scalar const& simcall)
142 {
143   return static_cast<T*>(simcall.dp);
144 }
145
146 template <class T>
147 inline void marshal(type<boost::intrusive_ptr<T>>, u_smx_scalar& simcall, boost::intrusive_ptr<T> value)
148 {
149   if (value.get() == nullptr) { // Sometimes we return nullptr in an intrusive_ptr...
150     simcall.dp = nullptr;
151   } else {
152     intrusive_ptr_add_ref(&*value);
153     simcall.dp = static_cast<void*>(&*value);
154   }
155 }
156 template <class T> inline boost::intrusive_ptr<T> unmarshal(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
157 {
158   // refcount was already increased during the marshaling, thus the "false" as last argument
159   boost::intrusive_ptr<T> res = boost::intrusive_ptr<T>(static_cast<T*>(simcall.dp), false);
160   return res;
161 }
162 template <class T> inline T* unmarshal_raw(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
163 {
164   return static_cast<T*>(simcall.dp);
165 }
166
167 template<class R, class... T> inline
168 void marshal(type<R(*)(T...)>, u_smx_scalar& simcall, R(*value)(T...))
169 {
170   simcall.fp = (FPtr) value;
171 }
172 template<class R, class... T> inline
173 auto unmarshal(type<R(*)(T...)>, u_smx_scalar simcall) -> R(*)(T...)
174 {
175   return (R(*)(T...)) simcall.fp;
176 }
177 template <class R, class... T> inline auto unmarshal_raw(type<R (*)(T...)>, u_smx_scalar simcall) -> R (*)(T...)
178 {
179   return (R(*)(T...))simcall.fp;
180 }
181
182 template<class T> inline
183 void marshal(u_smx_scalar& simcall, T const& value)
184 {
185   return marshal(type<T>(), simcall, value);
186 }
187 template<class T> inline
188 typename std::remove_reference<T>::type unmarshal(u_smx_scalar& simcall)
189 {
190   return unmarshal(type<T>(), simcall);
191 }
192 template <class T> inline typename std::remove_reference<T>::type unmarshal_raw(u_smx_scalar& simcall)
193 {
194   return unmarshal(type<T>(), simcall);
195 }
196
197 template<std::size_t I>
198 inline void marshalArgs(smx_simcall_t simcall) {}
199
200 template<std::size_t I, class A>
201 inline void marshalArgs(smx_simcall_t simcall, A const& a)
202 {
203   marshal(simcall->args[I], a);
204 }
205
206 template<std::size_t I, class A, class... B>
207 inline void marshalArgs(smx_simcall_t simcall, A const& a, B const&... b)
208 {
209   marshal(simcall->args[I], a);
210   marshalArgs<I+1>(simcall, b...);
211 }
212
213 /** Initialize the simcall */
214 template<class... A> inline
215 void marshal(smx_simcall_t simcall, e_smx_simcall_t call, A const&... a)
216 {
217   simcall->call = call;
218   memset(&simcall->result, 0, sizeof(simcall->result));
219   memset(simcall->args, 0, sizeof(simcall->args));
220   marshalArgs<0>(simcall, a...);
221 }
222
223 }
224 }
225
226 #endif
227
228 #include "popping_accessors.h"
229
230 #endif