Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 SG_BEGIN_DECL()
13
14 /********************************* Simcalls *********************************/
15 XBT_PUBLIC_DATA(const char*) simcall_names[]; /* Name of each simcall */
16
17 #include "popping_enum.h" /* Definition of e_smx_simcall_t, with one value per simcall */
18
19 typedef int (*simix_match_func_t)(void *, void *, smx_activity_t);
20 typedef void (*simix_copy_data_func_t)(smx_activity_t, 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   char            c;
27   short           s;
28   int             i;
29   long            l;
30   long long       ll;
31   unsigned char   uc;
32   unsigned short  us;
33   unsigned int    ui;
34   unsigned long   ul;
35   unsigned long long ull;
36   double          d;
37   void*           dp;
38   FPtr            fp;
39 };
40
41 /**
42  * \brief Represents a simcall to the kernel.
43  */
44 struct s_smx_simcall {
45   e_smx_simcall_t call;
46   smx_actor_t issuer;
47   smx_timer_t timer;
48   int mc_value;
49   union u_smx_scalar args[11];
50   union u_smx_scalar result;
51 };
52
53 #define SIMCALL_SET_MC_VALUE(simcall, value) ((simcall)->mc_value = (value))
54 #define SIMCALL_GET_MC_VALUE(simcall) ((simcall)->mc_value)
55
56 /******************************** General *************************************/
57
58 XBT_PRIVATE void SIMIX_simcall_answer(smx_simcall_t simcall);
59 XBT_PRIVATE void SIMIX_simcall_handle(smx_simcall_t simcall, int value);
60 XBT_PRIVATE void SIMIX_simcall_exit(smx_activity_t synchro);
61 XBT_PRIVATE const char *SIMIX_simcall_name(e_smx_simcall_t kind);
62 XBT_PRIVATE void SIMIX_run_kernel(std::function<void()> const* code);
63 XBT_PRIVATE void SIMIX_run_blocking(std::function<void()> const* code);
64
65 SG_END_DECL()
66
67 #ifdef __cplusplus
68
69 namespace simgrid {
70 namespace simix {
71
72 template<class T>
73 class type {
74   constexpr bool operator==(type) const    { return true; }
75   template<class U>
76   constexpr bool operator==(type<U>) const { return false; }
77   constexpr bool operator!=(type) const    { return false; }
78   template<class U>
79   constexpr bool operator!=(type<U>) const { return true; }
80 };
81
82 template<typename T> struct marshal_t {};
83 #define SIMIX_MARSHAL(T, field) \
84   inline void marshal(type<T>, u_smx_scalar& simcall, T value) \
85   { \
86     simcall.field = value; \
87   } \
88   inline T unmarshal(type<T>, u_smx_scalar const& simcall) \
89   { \
90     return simcall.field; \
91   }
92
93 SIMIX_MARSHAL(char, c);
94 SIMIX_MARSHAL(short, s);
95 SIMIX_MARSHAL(int, i);
96 SIMIX_MARSHAL(long, l);
97 SIMIX_MARSHAL(unsigned char, uc);
98 SIMIX_MARSHAL(unsigned short, us);
99 SIMIX_MARSHAL(unsigned int, ui);
100 SIMIX_MARSHAL(unsigned long, ul);
101 SIMIX_MARSHAL(unsigned long long, ull);
102 SIMIX_MARSHAL(long long, ll);
103 SIMIX_MARSHAL(float, d);
104 SIMIX_MARSHAL(double, d);
105 SIMIX_MARSHAL(FPtr, fp);
106
107 inline
108 void unmarshal(type<void>, u_smx_scalar const& simcall) {}
109
110 template<class T> inline
111 void marshal(type<T*>, u_smx_scalar& simcall, T* value)
112 {
113   simcall.dp = (void*) value;
114 }
115 template<class T> inline
116 T* unmarshal(type<T*>, u_smx_scalar const& simcall)
117 {
118   return static_cast<T*>(simcall.dp);
119 }
120
121 template<class R, class... T> inline
122 void marshal(type<R(*)(T...)>, u_smx_scalar& simcall, R(*value)(T...))
123 {
124   simcall.fp = (FPtr) value;
125 }
126 template<class R, class... T> inline
127 auto unmarshal(type<R(*)(T...)>, u_smx_scalar simcall) -> R(*)(T...)
128 {
129   return (R(*)(T...)) simcall.fp;
130 }
131
132 template<class T> inline
133 void marshal(u_smx_scalar& simcall, T const& value)
134 {
135   return marshal(type<T>(), simcall, value);
136 }
137 template<class T> inline
138 typename std::remove_reference<T>::type unmarshal(u_smx_scalar& simcall)
139 {
140   return unmarshal(type<T>(), simcall);
141 }
142
143 template<std::size_t I>
144 inline void marshalArgs(smx_simcall_t simcall) {}
145
146 template<std::size_t I, class A>
147 inline void marshalArgs(smx_simcall_t simcall, A const& a)
148 {
149   marshal(simcall->args[I], a);
150 }
151
152 template<std::size_t I, class A, class... B>
153 inline void marshalArgs(smx_simcall_t simcall, A const& a, B const&... b)
154 {
155   marshal(simcall->args[I], a);
156   marshalArgs<I+1>(simcall, b...);
157 }
158
159 /** Initialize the simcall */
160 template<class... A> inline
161 void marshal(smx_simcall_t simcall, e_smx_simcall_t call, A const&... a)
162 {
163   simcall->call = call;
164   memset(&simcall->result, 0, sizeof(simcall->result));
165   memset(simcall->args, 0, sizeof(simcall->args));
166   marshalArgs<0>(simcall, a...);
167 }
168
169 }
170 }
171
172 #endif
173
174 #include "popping_accessors.h"
175
176 #endif