1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SG_POPPING_PRIVATE_H
7 #define SG_POPPING_PRIVATE_H
10 #include <simgrid/simix.h>
12 #include <src/kernel/activity/ActivityImpl.hpp>
13 #include <src/kernel/activity/CommImpl.hpp>
15 #include <boost/intrusive_ptr.hpp>
19 /********************************* Simcalls *********************************/
20 XBT_PUBLIC_DATA(const char*) simcall_names[]; /* Name of each simcall */
22 #include "popping_enum.h" /* Definition of e_smx_simcall_t, with one value per simcall */
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
29 /* Pack all possible scalar types in an union */
40 unsigned long long ull;
47 * \brief Represents a simcall to the kernel.
49 struct s_smx_simcall {
54 union u_smx_scalar args[11];
55 union u_smx_scalar result;
58 #define SIMCALL_SET_MC_VALUE(simcall, value) ((simcall)->mc_value = (value))
59 #define SIMCALL_GET_MC_VALUE(simcall) ((simcall)->mc_value)
61 /******************************** General *************************************/
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);
79 constexpr bool operator==(type) const { return true; }
81 constexpr bool operator==(type<U>) const { return false; }
82 constexpr bool operator!=(type) const { return false; }
84 constexpr bool operator!=(type<U>) const { return true; }
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) \
91 simcall.field = value; \
93 inline T unmarshal(type<T>, u_smx_scalar const& simcall) \
95 return simcall.field; \
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);
113 void unmarshal(type<void>, u_smx_scalar const& simcall) {}
115 template<class T> inline
116 void marshal(type<T*>, u_smx_scalar& simcall, T* value)
118 simcall.dp = (void*) value;
120 template<class T> inline
121 T* unmarshal(type<T*>, u_smx_scalar const& simcall)
123 return static_cast<T*>(simcall.dp);
127 inline void marshal(type<boost::intrusive_ptr<T>>, u_smx_scalar& simcall, boost::intrusive_ptr<T> value)
129 intrusive_ptr_add_ref(&*value);
130 simcall.dp = static_cast<void*>(&*value);
132 template <class T> inline boost::intrusive_ptr<T> unmarshal(type<boost::intrusive_ptr<T>>, u_smx_scalar const& simcall)
134 boost::intrusive_ptr<T> res = boost::intrusive_ptr<T>(static_cast<T*>(simcall.dp), false);
135 intrusive_ptr_release(&*res);
139 template<class R, class... T> inline
140 void marshal(type<R(*)(T...)>, u_smx_scalar& simcall, R(*value)(T...))
142 simcall.fp = (FPtr) value;
144 template<class R, class... T> inline
145 auto unmarshal(type<R(*)(T...)>, u_smx_scalar simcall) -> R(*)(T...)
147 return (R(*)(T...)) simcall.fp;
150 template<class T> inline
151 void marshal(u_smx_scalar& simcall, T const& value)
153 return marshal(type<T>(), simcall, value);
155 template<class T> inline
156 typename std::remove_reference<T>::type unmarshal(u_smx_scalar& simcall)
158 return unmarshal(type<T>(), simcall);
161 template<std::size_t I>
162 inline void marshalArgs(smx_simcall_t simcall) {}
164 template<std::size_t I, class A>
165 inline void marshalArgs(smx_simcall_t simcall, A const& a)
167 marshal(simcall->args[I], a);
170 template<std::size_t I, class A, class... B>
171 inline void marshalArgs(smx_simcall_t simcall, A const& a, B const&... b)
173 marshal(simcall->args[I], a);
174 marshalArgs<I+1>(simcall, b...);
177 /** Initialize the simcall */
178 template<class... A> inline
179 void marshal(smx_simcall_t simcall, e_smx_simcall_t call, A const&... a)
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...);
192 #include "popping_accessors.h"