Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
389f89b407277de0373b6435b810df20f08c349d
[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 namespace simgrid {
75 namespace simix {
76
77 template<class T>
78 class type {
79   constexpr bool operator==(type) const    { return true; }
80   template<class U>
81   constexpr bool operator==(type<U>) const { return false; }
82   constexpr bool operator!=(type) const    { return false; }
83   template<class U>
84   constexpr bool operator!=(type<U>) const { return true; }
85 };
86
87 template<typename T> struct marshal_t {};
88 #define SIMIX_MARSHAL(T, field) \
89   inline void marshal(type<T>, u_smx_scalar& simcall, T value) \
90   { \
91     simcall.field = value; \
92   } \
93   inline T unmarshal(type<T>, u_smx_scalar const& simcall) \
94   { \
95     return simcall.field; \
96   }
97
98 SIMIX_MARSHAL(char, c);
99 SIMIX_MARSHAL(short, s);
100 SIMIX_MARSHAL(int, i);
101 SIMIX_MARSHAL(long, l);
102 SIMIX_MARSHAL(unsigned char, uc);
103 SIMIX_MARSHAL(unsigned short, us);
104 SIMIX_MARSHAL(unsigned int, ui);
105 SIMIX_MARSHAL(unsigned long, ul);
106 SIMIX_MARSHAL(unsigned long long, ull);
107 SIMIX_MARSHAL(long long, ll);
108 SIMIX_MARSHAL(float, d);
109 SIMIX_MARSHAL(double, d);
110 SIMIX_MARSHAL(FPtr, fp);
111
112 inline
113 void unmarshal(type<void>, u_smx_scalar const& simcall) {}
114
115 template<class T> inline
116 void marshal(type<T*>, u_smx_scalar& simcall, T* value)
117 {
118   simcall.dp = (void*) value;
119 }
120 template<class T> inline
121 T* unmarshal(type<T*>, u_smx_scalar const& simcall)
122 {
123   return static_cast<T*>(simcall.dp);
124 }
125
126 template <class T>
127 inline void marshal(type<boost::intrusive_ptr<T>>, u_smx_scalar& simcall, boost::intrusive_ptr<T> value)
128 {
129   intrusive_ptr_add_ref(&*value);
130   simcall.dp = static_cast<void*>(&*value);
131 }
132 template <class T> inline boost::intrusive_ptr<T> unmarshal(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
133 {
134   boost::intrusive_ptr<T> res = boost::intrusive_ptr<T>(static_cast<T*>(simcall.dp), false);
135   intrusive_ptr_release(&*res);
136   return res;
137 }
138
139 template<class R, class... T> inline
140 void marshal(type<R(*)(T...)>, u_smx_scalar& simcall, R(*value)(T...))
141 {
142   simcall.fp = (FPtr) value;
143 }
144 template<class R, class... T> inline
145 auto unmarshal(type<R(*)(T...)>, u_smx_scalar simcall) -> R(*)(T...)
146 {
147   return (R(*)(T...)) simcall.fp;
148 }
149
150 template<class T> inline
151 void marshal(u_smx_scalar& simcall, T const& value)
152 {
153   return marshal(type<T>(), simcall, value);
154 }
155 template<class T> inline
156 typename std::remove_reference<T>::type unmarshal(u_smx_scalar& simcall)
157 {
158   return unmarshal(type<T>(), simcall);
159 }
160
161 template<std::size_t I>
162 inline void marshalArgs(smx_simcall_t simcall) {}
163
164 template<std::size_t I, class A>
165 inline void marshalArgs(smx_simcall_t simcall, A const& a)
166 {
167   marshal(simcall->args[I], a);
168 }
169
170 template<std::size_t I, class A, class... B>
171 inline void marshalArgs(smx_simcall_t simcall, A const& a, B const&... b)
172 {
173   marshal(simcall->args[I], a);
174   marshalArgs<I+1>(simcall, b...);
175 }
176
177 /** Initialize the simcall */
178 template<class... A> inline
179 void marshal(smx_simcall_t simcall, e_smx_simcall_t call, A const&... a)
180 {
181   simcall->call = call;
182   memset(&simcall->result, 0, sizeof(simcall->result));
183   memset(simcall->args, 0, sizeof(simcall->args));
184   marshalArgs<0>(simcall, a...);
185 }
186
187 }
188 }
189
190 #endif
191
192 #include "popping_accessors.h"
193
194 #endif