Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics : I activated -pedantic for a quick pass
[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_                     = SIMCALL_NONE;
47   smx_actor_t issuer_                       = nullptr;
48   smx_timer_t timeout_cb_                   = nullptr; // Callback to timeouts
49   simgrid::mc::SimcallInspector* inspector_ = nullptr; // makes that simcall observable by the MC
50   int mc_value_                             = 0;
51   u_smx_scalar args_[11]                    = {{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}};
52   u_smx_scalar result_                      = {0};
53 };
54
55 #define SIMCALL_SET_MC_VALUE(simcall, value) ((simcall).mc_value_ = (value))
56 #define SIMCALL_GET_MC_VALUE(simcall) ((simcall).mc_value_)
57
58 /******************************** General *************************************/
59
60 XBT_PRIVATE const char* SIMIX_simcall_name(e_smx_simcall_t kind);
61 XBT_PRIVATE void SIMIX_run_kernel(std::function<void()> const* code);
62 XBT_PRIVATE void SIMIX_run_blocking(std::function<void()> const* code);
63
64 /* Defines the marshal/unmarshal functions for each type of parameters.
65  *
66  * They will be used in popping_accessors.hpp to define the functions allowing
67  * to retrieve/set each parameter of each simcall.
68  *
69  * There is a unmarshal_raw() function, which is exactly similar to unmarshal()
70  * for all types but boost::intrusive_ptr(T). For that type, the unmarshal()
71  * function builds a new intrusive_ptr wrapping the pointer (that is stored raw
72  * within the simcall) while the unmarshal_raw retrieves the raw pointer.
73  *
74  * This is used in <simcall>_getraw_<param> functions, that allow the
75  * model-checker, to read the data in the remote memory of the MCed.
76  */
77
78 namespace simgrid {
79 namespace simix {
80
81 template <class T> class type {
82   constexpr bool operator==(type) const { return true; }
83   template <class U> constexpr bool operator==(type<U>) const { return false; }
84   constexpr bool operator!=(type) const { return false; }
85   template <class U> constexpr bool operator!=(type<U>) const { return true; }
86 };
87
88 template <typename T> struct marshal_t {
89 };
90 #define SIMIX_MARSHAL(T, field)                                                                                        \
91   inline void marshal(type<T>, u_smx_scalar& simcall, T value) { simcall.field = value; }                              \
92   inline T unmarshal(type<T>, u_smx_scalar const& simcall) { return simcall.field; }                                   \
93   inline T unmarshal_raw(type<T>, u_smx_scalar const& simcall)                                                         \
94   { /* Exactly same as unmarshal. It differs only for intrusive_ptr */ return simcall.field; }
95
96 SIMIX_MARSHAL(bool, b)
97 SIMIX_MARSHAL(char, c)
98 SIMIX_MARSHAL(short, s)
99 SIMIX_MARSHAL(int, i)
100 SIMIX_MARSHAL(long, l)
101 SIMIX_MARSHAL(unsigned char, uc)
102 SIMIX_MARSHAL(unsigned short, us)
103 SIMIX_MARSHAL(unsigned int, ui)
104 SIMIX_MARSHAL(unsigned long, ul)
105 SIMIX_MARSHAL(unsigned long long, ull)
106 SIMIX_MARSHAL(long long, ll)
107 SIMIX_MARSHAL(float, d)
108 SIMIX_MARSHAL(double, d)
109 SIMIX_MARSHAL(FPtr, fp)
110
111 inline void unmarshal(type<void>, u_smx_scalar const& simcall)
112 {
113   /* Nothing to do for void data */
114 }
115 inline void unmarshal_raw(type<void>, u_smx_scalar const& simcall)
116 {
117   /* Nothing to do for void data */
118 }
119
120 template <class T> inline void marshal(type<T*>, u_smx_scalar& simcall, T* value)
121 {
122   simcall.dp = (void*)value;
123 }
124 template <class T> inline T* unmarshal(type<T*>, u_smx_scalar const& simcall)
125 {
126   return static_cast<T*>(simcall.dp);
127 }
128 template <class T> inline T* unmarshal_raw(type<T*>, u_smx_scalar const& simcall)
129 {
130   return static_cast<T*>(simcall.dp);
131 }
132
133 template <class T>
134 inline void marshal(type<boost::intrusive_ptr<T>>, u_smx_scalar& simcall, boost::intrusive_ptr<T> value)
135 {
136   if (value.get() == nullptr) { // Sometimes we return nullptr in an intrusive_ptr...
137     simcall.dp = nullptr;
138   } else {
139     intrusive_ptr_add_ref(value.get());
140     simcall.dp = static_cast<void*>(value.get());
141   }
142 }
143 template <class T> inline boost::intrusive_ptr<T> unmarshal(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
144 {
145   // refcount was already increased during the marshaling, thus the "false" as last argument
146   boost::intrusive_ptr<T> res = boost::intrusive_ptr<T>(static_cast<T*>(simcall.dp), false);
147   return res;
148 }
149 template <class T> inline T* unmarshal_raw(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
150 {
151   return static_cast<T*>(simcall.dp);
152 }
153
154 template <class R, class... T> inline void marshal(type<R (*)(T...)>, u_smx_scalar& simcall, R (*value)(T...))
155 {
156   simcall.fp = (FPtr)value;
157 }
158 template <class R, class... T> inline auto unmarshal(type<R (*)(T...)>, u_smx_scalar simcall) -> R (*)(T...)
159 {
160   return (R(*)(T...))simcall.fp;
161 }
162 template <class R, class... T> inline auto unmarshal_raw(type<R (*)(T...)>, u_smx_scalar simcall) -> R (*)(T...)
163 {
164   return (R(*)(T...))simcall.fp;
165 }
166
167 template <class T> inline void marshal(u_smx_scalar& simcall, T const& value)
168 {
169   return marshal(type<T>(), simcall, value);
170 }
171 template <class T> inline typename std::remove_reference<T>::type unmarshal(u_smx_scalar& simcall)
172 {
173   return unmarshal(type<T>(), simcall);
174 }
175 template <class T> inline typename std::remove_reference<T>::type unmarshal_raw(u_smx_scalar& simcall)
176 {
177   return unmarshal(type<T>(), simcall);
178 }
179
180 template <std::size_t I> inline void marshal_args(smx_simcall_t simcall)
181 {
182   /* Nothing to do when no args */
183 }
184
185 template <std::size_t I, class A> inline void marshal_args(smx_simcall_t simcall, A const& a)
186 {
187   marshal(simcall->args_[I], a);
188 }
189
190 template <std::size_t I, class A, class... B> inline void marshal_args(smx_simcall_t simcall, A const& a, B const&... b)
191 {
192   marshal(simcall->args_[I], a);
193   marshal_args<I + 1>(simcall, b...);
194 }
195
196 /** Initialize the simcall */
197 template <class... A> inline void marshal(smx_simcall_t simcall, e_smx_simcall_t call, A const&... a)
198 {
199   simcall->call_ = call;
200   memset(&simcall->result_, 0, sizeof(simcall->result_));
201   memset(simcall->args_, 0, sizeof(simcall->args_));
202   marshal_args<0>(simcall, a...);
203 }
204 }
205 }
206
207 #include "popping_accessors.hpp"
208
209 #endif