Logo AND Algorithmique Numérique Distribuée

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