Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix dist
[simgrid.git] / src / simix / popping_private.h
1 /* Copyright (c) 2007-2010, 2012-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef _POPPING_PRIVATE_H
8 #define _POPPING_PRIVATE_H
9
10 #include <xbt/base.h>
11 #include <simgrid/simix.h>
12
13 SG_BEGIN_DECL()
14
15 /********************************* Simcalls *********************************/
16 XBT_PUBLIC_DATA(const char*) simcall_names[]; /* Name of each simcall */
17
18 #include "popping_enum.h" /* Definition of e_smx_simcall_t, with one value per simcall */
19
20 typedef int (*simix_match_func_t)(void *, void *, smx_activity_t);
21 typedef void (*simix_copy_data_func_t)(smx_activity_t, void*, size_t);
22 typedef void (*simix_clean_func_t)(void *);
23 typedef void (*FPtr)(void); // Hide the ugliness
24
25 /* Pack all possible scalar types in an union */
26 union u_smx_scalar {
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;
47   smx_actor_t issuer;
48   smx_timer_t timer;
49   int mc_value;
50   union u_smx_scalar args[11];
51   union u_smx_scalar result;
52 };
53
54 #define SIMCALL_SET_MC_VALUE(simcall, value) ((simcall)->mc_value = (value))
55 #define SIMCALL_GET_MC_VALUE(simcall) ((simcall)->mc_value)
56
57 /******************************** General *************************************/
58
59 XBT_PRIVATE void SIMIX_simcall_answer(smx_simcall_t simcall);
60 XBT_PRIVATE void SIMIX_simcall_handle(smx_simcall_t simcall, int value);
61 XBT_PRIVATE void SIMIX_simcall_exit(smx_activity_t synchro);
62 XBT_PRIVATE const char *SIMIX_simcall_name(e_smx_simcall_t kind);
63 XBT_PRIVATE void SIMIX_run_kernel(std::function<void()> const* code);
64 XBT_PRIVATE void SIMIX_run_blocking(std::function<void()> const* code);
65
66 SG_END_DECL()
67
68 #ifdef __cplusplus
69
70 namespace simgrid {
71 namespace simix {
72
73 template<class T>
74 class type {
75   constexpr bool operator==(type) const    { return true; }
76   template<class U>
77   constexpr bool operator==(type<U>) const { return false; }
78   constexpr bool operator!=(type) const    { return false; }
79   template<class U>
80   constexpr bool operator!=(type<U>) const { return true; }
81 };
82
83 template<typename T> struct marshal_t {};
84 #define SIMIX_MARSHAL(T, field) \
85   inline void marshal(type<T>, u_smx_scalar& simcall, T value) \
86   { \
87     simcall.field = value; \
88   } \
89   inline T unmarshal(type<T>, u_smx_scalar const& simcall) \
90   { \
91     return simcall.field; \
92   }
93
94 SIMIX_MARSHAL(char, c);
95 SIMIX_MARSHAL(short, s);
96 SIMIX_MARSHAL(int, i);
97 SIMIX_MARSHAL(long, l);
98 SIMIX_MARSHAL(unsigned char, uc);
99 SIMIX_MARSHAL(unsigned short, us);
100 SIMIX_MARSHAL(unsigned int, ui);
101 SIMIX_MARSHAL(unsigned long, ul);
102 SIMIX_MARSHAL(unsigned long long, ull);
103 SIMIX_MARSHAL(long long, ll);
104 SIMIX_MARSHAL(float, d);
105 SIMIX_MARSHAL(double, d);
106 SIMIX_MARSHAL(FPtr, fp);
107
108 inline
109 void unmarshal(type<void>, u_smx_scalar const& simcall) {}
110
111 template<class T> inline
112 void marshal(type<T*>, u_smx_scalar& simcall, T* value)
113 {
114   simcall.dp = (void*) value;
115 }
116 template<class T> inline
117 T* unmarshal(type<T*>, u_smx_scalar const& simcall)
118 {
119   return static_cast<T*>(simcall.dp);
120 }
121
122 template<class R, class... T> inline
123 void marshal(type<R(*)(T...)>, u_smx_scalar& simcall, R(*value)(T...))
124 {
125   simcall.fp = (FPtr) value;
126 }
127 template<class R, class... T> inline
128 auto unmarshal(type<R(*)(T...)>, u_smx_scalar simcall) -> R(*)(T...)
129 {
130   return (R(*)(T...)) simcall.fp;
131 }
132
133 template<class T> inline
134 void marshal(u_smx_scalar& simcall, T const& value)
135 {
136   return marshal(type<T>(), simcall, value);
137 }
138 template<class T> inline
139 typename std::remove_reference<T>::type unmarshal(u_smx_scalar& simcall)
140 {
141   return unmarshal(type<T>(), simcall);
142 }
143
144 template<std::size_t I>
145 inline void marshalArgs(smx_simcall_t simcall) {}
146
147 template<std::size_t I, class A>
148 inline void marshalArgs(smx_simcall_t simcall, A const& a)
149 {
150   marshal(simcall->args[I], a);
151 }
152
153 template<std::size_t I, class A, class... B>
154 inline void marshalArgs(smx_simcall_t simcall, A const& a, B const&... b)
155 {
156   marshal(simcall->args[I], a);
157   marshalArgs<I+1>(simcall, b...);
158 }
159
160 /** Initialize the simcall */
161 template<class... A> inline
162 void marshal(smx_simcall_t simcall, e_smx_simcall_t call, A const&... a)
163 {
164   simcall->call = call;
165   memset(&simcall->result, 0, sizeof(simcall->result));
166   memset(simcall->args, 0, sizeof(simcall->args));
167   marshalArgs<0>(simcall, a...);
168 }
169
170 }
171 }
172
173 #endif
174
175 #include "popping_accessors.h"
176
177 #endif