Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] DeXFTification of Synchro: use std::list<> instead of xbt_fifo_t
[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_synchro_t);
21 typedef void (*simix_copy_data_func_t)(smx_synchro_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_process_t issuer;
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_synchro_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
64 SG_END_DECL()
65
66 #ifdef __cplusplus
67
68 namespace simgrid {
69 namespace simix {
70
71 template<class T>
72 class type {
73   constexpr bool operator==(type) const    { return true; }
74   template<class U>
75   constexpr bool operator==(type<U>) const { return false; }
76   constexpr bool operator!=(type) const    { return false; }
77   template<class U>
78   constexpr bool operator!=(type<U>) const { return true; }
79 };
80
81 template<typename T> struct marshal_t {};
82 #define SIMIX_MARSHAL(T, field) \
83   inline void marshal(type<T>, u_smx_scalar& simcall, T value) \
84   { \
85     simcall.field = value; \
86   } \
87   inline T unmarshal(type<T>, u_smx_scalar const& simcall) \
88   { \
89     return simcall.field; \
90   }
91
92 SIMIX_MARSHAL(char, c);
93 SIMIX_MARSHAL(short, s);
94 SIMIX_MARSHAL(int, i);
95 SIMIX_MARSHAL(long, l);
96 SIMIX_MARSHAL(unsigned char, uc);
97 SIMIX_MARSHAL(unsigned short, us);
98 SIMIX_MARSHAL(unsigned int, ui);
99 SIMIX_MARSHAL(unsigned long, ul);
100 SIMIX_MARSHAL(unsigned long long, ull);
101 SIMIX_MARSHAL(long long, ll);
102 SIMIX_MARSHAL(float, d);
103 SIMIX_MARSHAL(double, d);
104 SIMIX_MARSHAL(FPtr, fp);
105
106 inline
107 void unmarshal(type<void>, u_smx_scalar const& simcall) {}
108
109 template<class T> inline
110 void marshal(type<T*>, u_smx_scalar& simcall, T* value)
111 {
112   simcall.dp = (void*) value;
113 }
114 template<class T> inline
115 T* unmarshal(type<T*>, u_smx_scalar const& simcall)
116 {
117   return static_cast<T*>(simcall.dp);
118 }
119
120 template<class R, class... T> inline
121 void marshal(type<R(*)(T...)>, u_smx_scalar& simcall, R(*value)(T...))
122 {
123   simcall.fp = (FPtr) value;
124 }
125 template<class R, class... T> inline
126 auto unmarshal(type<R(*)(T...)>, u_smx_scalar simcall) -> R(*)(T...)
127 {
128   return (R(*)(T...)) simcall.fp;
129 }
130
131 template<class T> inline
132 void marshal(u_smx_scalar& simcall, T const& value)
133 {
134   return marshal(type<T>(), simcall, value);
135 }
136 template<class T> inline
137 typename std::remove_reference<T>::type unmarshal(u_smx_scalar& simcall)
138 {
139   return unmarshal(type<T>(), simcall);
140 }
141
142 template<std::size_t I>
143 inline void marshalArgs(smx_simcall_t simcall) {}
144
145 template<std::size_t I, class A>
146 inline void marshalArgs(smx_simcall_t simcall, A const& a)
147 {
148   marshal(simcall->args[I], a);
149 }
150
151 template<std::size_t I, class A, class... B>
152 inline void marshalArgs(smx_simcall_t simcall, A const& a, B const&... b)
153 {
154   marshal(simcall->args[I], a);
155   marshalArgs<I+1>(simcall, b...);
156 }
157
158 /** Initialize the simcall */
159 template<class... A> inline
160 void marshal(smx_simcall_t simcall, e_smx_simcall_t call, A const&... a)
161 {
162   simcall->call = call;
163   memset(&simcall->result, 0, sizeof(simcall->result));
164   memset(simcall->args, 0, sizeof(simcall->args));
165   marshalArgs<0>(simcall, a...);
166 }
167
168 }
169 }
170
171 #endif
172
173 #include "popping_accessors.h"
174
175 #endif