Logo AND Algorithmique Numérique Distribuée

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