Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make mailbox matching functions return a bool.
[simgrid.git] / src / simix / libsmx.cpp
1 /* libsmx.c - public interface to simix                                       */
2 /* --------                                                                   */
3 /* These functions are the only ones that are visible from the higher levels  */
4 /* (most of them simply add some documentation to the generated simcall body) */
5 /*                                                                            */
6 /* This is somehow the "libc" of SimGrid                                      */
7
8 /* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved.          */
9
10 /* This program is free software; you can redistribute it and/or modify it
11  * under the terms of the license (GNU LGPL) which comes with this package. */
12
13 #include "mc/mc.h"
14 #include "simgrid/simix/blocking_simcall.hpp"
15 #include "src/kernel/activity/CommImpl.hpp"
16 #include "src/kernel/activity/ConditionVariableImpl.hpp"
17 #include "src/kernel/activity/ExecImpl.hpp"
18 #include "src/kernel/activity/IoImpl.hpp"
19 #include "src/kernel/activity/MailboxImpl.hpp"
20 #include "src/kernel/activity/MutexImpl.hpp"
21 #include "src/mc/mc_replay.hpp"
22 #include "src/plugins/vm/VirtualMachineImpl.hpp"
23
24 #include "popping_bodies.cpp"
25
26 /**
27  * @ingroup simix_host_management
28  * @brief Waits for the completion of an execution synchro and destroy it.
29  *
30  * @param execution The execution synchro
31  */
32 e_smx_state_t simcall_execution_wait(const smx_activity_t& execution, double timeout)
33 {
34   return (e_smx_state_t)simcall_BODY_execution_wait(static_cast<simgrid::kernel::activity::ExecImpl*>(execution.get()),
35                                                     timeout);
36 }
37
38 bool simcall_execution_test(const smx_activity_t& execution)
39 {
40   return simcall_BODY_execution_test(static_cast<simgrid::kernel::activity::ExecImpl*>(execution.get()));
41 }
42
43 unsigned int simcall_execution_waitany_for(simgrid::kernel::activity::ExecImpl* execs[], size_t count, double timeout)
44 {
45   return simcall_BODY_execution_waitany_for(execs, count, timeout);
46 }
47
48 void simcall_process_join(smx_actor_t process, double timeout) // XBT_DEPRECATED_v328
49 {
50   simgrid::kernel::actor::ActorImpl::self()->join(process, timeout);
51 }
52
53 void simcall_process_suspend(smx_actor_t process) // XBT_DEPRECATED_v328
54 {
55   process->iface()->suspend();
56 }
57
58 e_smx_state_t simcall_process_sleep(double duration) // XBT_DEPRECATED_v329
59 {
60   simgrid::kernel::actor::ActorImpl::self()->sleep(duration);
61   return simgrid::kernel::activity::State::DONE;
62 }
63
64 /**
65  * @ingroup simix_comm_management
66  */
67 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
68                        size_t src_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
69                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
70                        double timeout)
71 {
72   /* checking for infinite values */
73   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
74   xbt_assert(std::isfinite(rate), "rate is not finite!");
75   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
76
77   xbt_assert(mbox, "No rendez-vous point defined for send");
78
79   if (MC_is_active() || MC_record_replay_is_active()) {
80     /* the model-checker wants two separate simcalls */
81     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
82     comm = simcall_comm_isend(sender, mbox, task_size, rate,
83         src_buff, src_buff_size, match_fun, nullptr, copy_data_fun, data, 0);
84     simcall_comm_wait(comm, timeout);
85     comm = nullptr;
86   }
87   else {
88     simcall_BODY_comm_send(sender, mbox, task_size, rate, static_cast<unsigned char*>(src_buff), src_buff_size,
89                            match_fun, copy_data_fun, data, timeout);
90   }
91 }
92
93 /**
94  * @ingroup simix_comm_management
95  */
96 smx_activity_t simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
97                                   size_t src_buff_size,
98                                   bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
99                                   void (*clean_fun)(void*),
100                                   void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
101                                   void* data, bool detached)
102 {
103   /* checking for infinite values */
104   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
105   xbt_assert(std::isfinite(rate), "rate is not finite!");
106
107   xbt_assert(mbox, "No rendez-vous point defined for isend");
108
109   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, static_cast<unsigned char*>(src_buff), src_buff_size,
110                                  match_fun, clean_fun, copy_data_fun, data, detached);
111 }
112
113 /**
114  * @ingroup simix_comm_management
115  */
116 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
117                        bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
118                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
119                        double timeout, double rate)
120 {
121   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
122   xbt_assert(mbox, "No rendez-vous point defined for recv");
123
124   if (MC_is_active() || MC_record_replay_is_active()) {
125     /* the model-checker wants two separate simcalls */
126     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simcall */
127     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
128                               match_fun, copy_data_fun, data, rate);
129     simcall_comm_wait(comm, timeout);
130     comm = nullptr;
131   }
132   else {
133     simcall_BODY_comm_recv(receiver, mbox, static_cast<unsigned char*>(dst_buff), dst_buff_size, match_fun,
134                            copy_data_fun, data, timeout, rate);
135   }
136 }
137 /**
138  * @ingroup simix_comm_management
139  */
140 smx_activity_t simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
141                                   bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
142                                   void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
143                                   void* data, double rate)
144 {
145   xbt_assert(mbox, "No rendez-vous point defined for irecv");
146
147   return simcall_BODY_comm_irecv(receiver, mbox, static_cast<unsigned char*>(dst_buff), dst_buff_size, match_fun,
148                                  copy_data_fun, data, rate);
149 }
150
151 /**
152  * @ingroup simix_comm_management
153  */
154 smx_activity_t simcall_comm_iprobe(smx_mailbox_t mbox, int type,
155                                    bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*), void* data)
156 {
157   xbt_assert(mbox, "No rendez-vous point defined for iprobe");
158
159   return simgrid::kernel::actor::simcall([mbox, type, match_fun, data] { return mbox->iprobe(type, match_fun, data); });
160 }
161
162 /**
163  * @ingroup simix_comm_management
164  */
165 unsigned int simcall_comm_waitany(smx_activity_t comms[], size_t count, double timeout)
166 {
167   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
168   std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
169     return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
170   });
171   return simcall_BODY_comm_waitany(rcomms.get(), count, timeout);
172 }
173
174 unsigned int simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count, double timeout)
175 {
176   return simcall_BODY_comm_waitany(comms, count, timeout);
177 }
178
179 /**
180  * @ingroup simix_comm_management
181  */
182 int simcall_comm_testany(smx_activity_t comms[], size_t count)
183 {
184   if (count == 0)
185     return -1;
186   std::unique_ptr<simgrid::kernel::activity::CommImpl* []> rcomms(new simgrid::kernel::activity::CommImpl*[count]);
187   std::transform(comms, comms + count, rcomms.get(), [](const smx_activity_t& comm) {
188     return static_cast<simgrid::kernel::activity::CommImpl*>(comm.get());
189   });
190   return simcall_BODY_comm_testany(rcomms.get(), count);
191 }
192
193 int simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count)
194 {
195   if (count == 0)
196     return -1;
197   return simcall_BODY_comm_testany(comms, count);
198 }
199
200 /**
201  * @ingroup simix_comm_management
202  */
203 void simcall_comm_wait(const smx_activity_t& comm, double timeout)
204 {
205   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
206   simcall_BODY_comm_wait(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()), timeout);
207 }
208
209 /**
210  * @ingroup simix_comm_management
211  *
212  */
213 bool simcall_comm_test(const smx_activity_t& comm)
214 {
215   return simcall_BODY_comm_test(static_cast<simgrid::kernel::activity::CommImpl*>(comm.get()));
216 }
217
218 /**
219  * @ingroup simix_synchro_management
220  *
221  */
222 smx_mutex_t simcall_mutex_init()
223 {
224   if (simix_global == nullptr) {
225     fprintf(stderr, "You must initialize the SimGrid engine before using it\n"); // We can't use xbt_die since we may
226                                                                                  // get there before the initialization
227     xbt_abort();
228   }
229   return simgrid::kernel::actor::simcall([] { return new simgrid::kernel::activity::MutexImpl(); });
230 }
231
232 /**
233  * @ingroup simix_synchro_management
234  *
235  */
236 void simcall_mutex_lock(smx_mutex_t mutex)
237 {
238   simcall_BODY_mutex_lock(mutex);
239 }
240
241 /**
242  * @ingroup simix_synchro_management
243  *
244  */
245 int simcall_mutex_trylock(smx_mutex_t mutex)
246 {
247   return simcall_BODY_mutex_trylock(mutex);
248 }
249
250 /**
251  * @ingroup simix_synchro_management
252  *
253  */
254 void simcall_mutex_unlock(smx_mutex_t mutex)
255 {
256   simcall_BODY_mutex_unlock(mutex);
257 }
258
259 /**
260  * @ingroup simix_synchro_management
261  *
262  */
263 smx_cond_t simcall_cond_init()
264 {
265   return simgrid::kernel::actor::simcall([] { return new simgrid::kernel::activity::ConditionVariableImpl(); });
266 }
267
268 /**
269  * @ingroup simix_synchro_management
270  *
271  */
272 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex)
273 {
274   simcall_BODY_cond_wait(cond, mutex);
275 }
276
277 /**
278  * @ingroup simix_synchro_management
279  *
280  */
281 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout)
282 {
283   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
284   return simcall_BODY_cond_wait_timeout(cond, mutex, timeout);
285 }
286
287 /**
288  * @ingroup simix_synchro_management
289  *
290  */
291 void simcall_sem_acquire(smx_sem_t sem)
292 {
293   simcall_BODY_sem_acquire(sem);
294 }
295
296 /**
297  * @ingroup simix_synchro_management
298  *
299  */
300 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout)
301 {
302   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
303   return simcall_BODY_sem_acquire_timeout(sem, timeout);
304 }
305
306 e_smx_state_t simcall_io_wait(const smx_activity_t& io, double timeout)
307 {
308   return (e_smx_state_t)simcall_BODY_io_wait(static_cast<simgrid::kernel::activity::IoImpl*>(io.get()), timeout);
309 }
310
311 bool simcall_io_test(const smx_activity_t& io)
312 {
313   return simcall_BODY_io_test(static_cast<simgrid::kernel::activity::IoImpl*>(io.get()));
314 }
315
316 void simcall_run_kernel(std::function<void()> const& code, simgrid::mc::SimcallInspector* t)
317 {
318   simgrid::kernel::actor::ActorImpl::self()->simcall.inspector_ = t;
319   simcall_BODY_run_kernel(&code);
320 }
321
322 void simcall_run_blocking(std::function<void()> const& code, simgrid::mc::SimcallInspector* t = nullptr)
323 {
324   simgrid::kernel::actor::ActorImpl::self()->simcall.inspector_ = t;
325   simcall_BODY_run_blocking(&code);
326 }
327
328 int simcall_mc_random(int min, int max) {
329   return simcall_BODY_mc_random(min, max);
330 }
331
332 /* ************************************************************************** */
333
334 /** @brief returns a printable string representing a simcall */
335 const char *SIMIX_simcall_name(e_smx_simcall_t kind) {
336   return simcall_names[kind];
337 }
338
339 namespace simgrid {
340 namespace simix {
341
342 void unblock(smx_actor_t actor)
343 {
344   xbt_assert(SIMIX_is_maestro());
345   actor->simcall_answer();
346 }
347 } // namespace simix
348 } // namespace simgrid