Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove deprecated features for next release (3.30).
[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-2021. 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 "src/kernel/activity/CommImpl.hpp"
15 #include "src/kernel/activity/ConditionVariableImpl.hpp"
16 #include "src/kernel/activity/ExecImpl.hpp"
17 #include "src/kernel/activity/IoImpl.hpp"
18 #include "src/kernel/activity/MailboxImpl.hpp"
19 #include "src/kernel/activity/MutexImpl.hpp"
20 #include "src/kernel/activity/SemaphoreImpl.hpp"
21 #include "src/kernel/actor/SimcallObserver.hpp"
22 #include "src/mc/mc_replay.hpp"
23 #include "src/plugins/vm/VirtualMachineImpl.hpp"
24 #include "xbt/random.hpp"
25
26 #include "popping_bodies.cpp"
27
28 #include <boost/core/demangle.hpp>
29 #include <string>
30 #include <typeinfo>
31 XBT_LOG_NEW_CATEGORY(simix, "All SIMIX categories");
32
33 unsigned int simcall_execution_waitany_for(simgrid::kernel::activity::ExecImpl* execs[], size_t count,
34                                            double timeout) // XBT_ATTRIB_DEPRECATED_v331
35 {
36   std::vector<simgrid::kernel::activity::ExecImpl*> execs_vec(execs, execs + count);
37   simgrid::kernel::actor::ActorImpl* issuer = simgrid::kernel::actor::ActorImpl::self();
38   simgrid::kernel::actor::ExecutionWaitanySimcall observer{issuer, execs_vec, timeout};
39   return simgrid::kernel::actor::simcall_blocking(
40       [&observer] {
41         simgrid::kernel::activity::ExecImpl::wait_any_for(observer.get_issuer(), observer.get_execs(),
42                                                           observer.get_timeout());
43       },
44       &observer);
45 }
46
47 /**
48  * @ingroup simix_comm_management
49  */
50 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
51                        size_t src_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
52                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
53                        double timeout)
54 {
55   /* checking for infinite values */
56   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
57   xbt_assert(std::isfinite(rate), "rate is not finite!");
58   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
59
60   xbt_assert(mbox, "No rendez-vous point defined for send");
61
62   if (MC_is_active() || MC_record_replay_is_active()) {
63     /* the model-checker wants two separate simcalls */
64     simgrid::kernel::activity::ActivityImplPtr comm =
65         nullptr; /* MC needs the comm to be set to nullptr during the simcall */
66     comm = simcall_comm_isend(sender, mbox, task_size, rate, src_buff, src_buff_size, match_fun, nullptr, copy_data_fun,
67                               data, false);
68     simcall_comm_wait(comm.get(), timeout);
69     comm = nullptr;
70   }
71   else {
72     simcall_BODY_comm_send(sender, mbox, task_size, rate, static_cast<unsigned char*>(src_buff), src_buff_size,
73                            match_fun, copy_data_fun, data, timeout);
74   }
75 }
76
77 /**
78  * @ingroup simix_comm_management
79  */
80 simgrid::kernel::activity::ActivityImplPtr
81 simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
82                    size_t src_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
83                    void (*clean_fun)(void*), void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
84                    void* data, bool detached)
85 {
86   /* checking for infinite values */
87   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
88   xbt_assert(std::isfinite(rate), "rate is not finite!");
89
90   xbt_assert(mbox, "No rendez-vous point defined for isend");
91
92   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, static_cast<unsigned char*>(src_buff), src_buff_size,
93                                  match_fun, clean_fun, copy_data_fun, data, detached);
94 }
95
96 /**
97  * @ingroup simix_comm_management
98  */
99 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
100                        bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
101                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
102                        double timeout, double rate)
103 {
104   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
105   xbt_assert(mbox, "No rendez-vous point defined for recv");
106
107   if (MC_is_active() || MC_record_replay_is_active()) {
108     /* the model-checker wants two separate simcalls */
109     simgrid::kernel::activity::ActivityImplPtr comm =
110         nullptr; /* MC needs the comm to be set to nullptr during the simcall */
111     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
112                               match_fun, copy_data_fun, data, rate);
113     simcall_comm_wait(comm.get(), timeout);
114     comm = nullptr;
115   }
116   else {
117     simcall_BODY_comm_recv(receiver, mbox, static_cast<unsigned char*>(dst_buff), dst_buff_size, match_fun,
118                            copy_data_fun, data, timeout, rate);
119   }
120 }
121 /**
122  * @ingroup simix_comm_management
123  */
124 simgrid::kernel::activity::ActivityImplPtr
125 simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
126                    bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
127                    void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data, double rate)
128 {
129   xbt_assert(mbox, "No rendez-vous point defined for irecv");
130
131   return simcall_BODY_comm_irecv(receiver, mbox, static_cast<unsigned char*>(dst_buff), dst_buff_size, match_fun,
132                                  copy_data_fun, data, rate);
133 }
134
135 /**
136  * @ingroup simix_comm_management
137  */
138 ssize_t simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count, double timeout)
139 {
140   return simcall_BODY_comm_waitany(comms, count, timeout);
141 }
142
143 /**
144  * @ingroup simix_comm_management
145  */
146 ssize_t simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count)
147 {
148   if (count == 0)
149     return -1;
150   return simcall_BODY_comm_testany(comms, count);
151 }
152
153 /**
154  * @ingroup simix_comm_management
155  */
156 void simcall_comm_wait(simgrid::kernel::activity::ActivityImpl* comm, double timeout)
157 {
158   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
159   simcall_BODY_comm_wait(static_cast<simgrid::kernel::activity::CommImpl*>(comm), timeout);
160 }
161
162 /**
163  * @ingroup simix_comm_management
164  *
165  */
166 bool simcall_comm_test(simgrid::kernel::activity::ActivityImpl* comm)
167 {
168   return simcall_BODY_comm_test(static_cast<simgrid::kernel::activity::CommImpl*>(comm));
169 }
170
171 /**
172  * @ingroup simix_synchro_management
173  *
174  */
175 void simcall_mutex_lock(smx_mutex_t mutex) // XBT_ATTRIB_DEPRECATD_v331
176 {
177   mutex->mutex().lock();
178 }
179
180 /**
181  * @ingroup simix_synchro_management
182  *
183  */
184 int simcall_mutex_trylock(smx_mutex_t mutex) // XBT_ATTRIB_DEPRECATD_v331
185 {
186   return mutex->mutex().try_lock();
187 }
188
189 /**
190  * @ingroup simix_synchro_management
191  *
192  */
193 void simcall_mutex_unlock(smx_mutex_t mutex) // XBT_ATTRIB_DEPRECATD_v331
194 {
195   mutex->mutex().unlock();
196 }
197
198 /**
199  * @ingroup simix_synchro_management
200  *
201  */
202 void simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex) // XBT_ATTRIB_DEPRECATED_v331
203 {
204   cond->get_iface()->wait(std::unique_lock<simgrid::s4u::Mutex>(mutex->mutex()));
205 }
206
207 /**
208  * @ingroup simix_synchro_management
209  *
210  */
211 int simcall_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, double timeout) // XBT_ATTRIB_DEPRECATD_v331
212 {
213   return cond->get_iface()->wait_for(std::unique_lock<simgrid::s4u::Mutex>(mutex->mutex()), timeout) ==
214          std::cv_status::timeout;
215 }
216
217 /**
218  * @ingroup simix_synchro_management
219  *
220  */
221 void simcall_sem_acquire(smx_sem_t sem) // XBT_ATTRIB_DEPRECATD_v331
222 {
223   return sem->sem().acquire();
224 }
225
226 /**
227  * @ingroup simix_synchro_management
228  *
229  */
230 int simcall_sem_acquire_timeout(smx_sem_t sem, double timeout) // XBT_ATTRIB_DEPRECATD_v331
231 {
232   return sem->sem().acquire_timeout(timeout);
233 }
234
235 void simcall_run_kernel(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
236 {
237   simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = observer;
238   simcall_BODY_run_kernel(&code);
239   simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = nullptr;
240 }
241
242 void simcall_run_blocking(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
243 {
244   simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = observer;
245   simcall_BODY_run_blocking(&code);
246   simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = nullptr;
247 }
248
249 int simcall_mc_random(int min, int max) // XBT_ATTRIB_DEPRECATD_v331
250 {
251   return MC_random(min, max);
252 }
253
254 /* ************************************************************************** */
255
256 /** @brief returns a printable string representing a simcall */
257 const char* SIMIX_simcall_name(const s_smx_simcall& simcall)
258 {
259   if (simcall.observer_ != nullptr) {
260     static std::string name;
261     name              = boost::core::demangle(typeid(*simcall.observer_).name());
262     const char* cname = name.c_str();
263     if (name.rfind("simgrid::kernel::", 0) == 0)
264       cname += 17; // strip prefix "simgrid::kernel::"
265     return cname;
266   } else {
267     return simcall_names[static_cast<int>(simcall.call_)];
268   }
269 }
270
271 namespace simgrid {
272 namespace simix {
273
274 void unblock(smx_actor_t actor)
275 {
276   xbt_assert(s4u::Actor::is_maestro());
277   actor->simcall_answer();
278 }
279 } // namespace simix
280 } // namespace simgrid