Logo AND Algorithmique Numérique Distribuée

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