Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mark some old simcalls as deprecated (test, test_any, and wait_any)
[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-2022. 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/MutexImpl.hpp"
17 #include "src/kernel/activity/SemaphoreImpl.hpp"
18 #include "src/kernel/actor/SimcallObserver.hpp"
19 #include "src/mc/mc_replay.hpp"
20 #include "xbt/random.hpp"
21
22 #include "popping_bodies.cpp"
23
24 #include <boost/core/demangle.hpp>
25 #include <string>
26 #include <typeinfo>
27
28 /**
29  * @ingroup simix_comm_management
30  */
31 void simcall_comm_send(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
32                        size_t src_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
33                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
34                        double timeout)
35 {
36   /* checking for infinite values */
37   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
38   xbt_assert(std::isfinite(rate), "rate is not finite!");
39   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
40
41   xbt_assert(mbox, "No rendez-vous point defined for send");
42
43   if (MC_is_active() || MC_record_replay_is_active()) {
44     /* the model-checker wants two separate simcalls */
45     simgrid::kernel::activity::ActivityImplPtr comm =
46         nullptr; /* MC needs the comm to be set to nullptr during the simcall */
47     comm = simcall_comm_isend(sender, mbox, task_size, rate, src_buff, src_buff_size, match_fun, nullptr, copy_data_fun,
48                               data, false);
49     simcall_comm_wait(comm.get(), timeout);
50     comm = nullptr;
51   }
52   else {
53     simcall_BODY_comm_send(sender, mbox, task_size, rate, static_cast<unsigned char*>(src_buff), src_buff_size,
54                            match_fun, copy_data_fun, data, timeout);
55   }
56 }
57
58 /**
59  * @ingroup simix_comm_management
60  */
61 simgrid::kernel::activity::ActivityImplPtr
62 simcall_comm_isend(smx_actor_t sender, smx_mailbox_t mbox, double task_size, double rate, void* src_buff,
63                    size_t src_buff_size, bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
64                    void (*clean_fun)(void*), void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t),
65                    void* data, bool detached)
66 {
67   /* checking for infinite values */
68   xbt_assert(std::isfinite(task_size), "task_size is not finite!");
69   xbt_assert(std::isfinite(rate), "rate is not finite!");
70
71   xbt_assert(mbox, "No rendez-vous point defined for isend");
72
73   return simcall_BODY_comm_isend(sender, mbox, task_size, rate, static_cast<unsigned char*>(src_buff), src_buff_size,
74                                  match_fun, clean_fun, copy_data_fun, data, detached);
75 }
76
77 /**
78  * @ingroup simix_comm_management
79  */
80 void simcall_comm_recv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
81                        bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
82                        void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data,
83                        double timeout, double rate)
84 {
85   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
86   xbt_assert(mbox, "No rendez-vous point defined for recv");
87
88   if (MC_is_active() || MC_record_replay_is_active()) {
89     /* the model-checker wants two separate simcalls */
90     simgrid::kernel::activity::ActivityImplPtr comm =
91         nullptr; /* MC needs the comm to be set to nullptr during the simcall */
92     comm = simcall_comm_irecv(receiver, mbox, dst_buff, dst_buff_size,
93                               match_fun, copy_data_fun, data, rate);
94     simcall_comm_wait(comm.get(), timeout);
95     comm = nullptr;
96   }
97   else {
98     simcall_BODY_comm_recv(receiver, mbox, static_cast<unsigned char*>(dst_buff), dst_buff_size, match_fun,
99                            copy_data_fun, data, timeout, rate);
100   }
101 }
102 /**
103  * @ingroup simix_comm_management
104  */
105 simgrid::kernel::activity::ActivityImplPtr
106 simcall_comm_irecv(smx_actor_t receiver, smx_mailbox_t mbox, void* dst_buff, size_t* dst_buff_size,
107                    bool (*match_fun)(void*, void*, simgrid::kernel::activity::CommImpl*),
108                    void (*copy_data_fun)(simgrid::kernel::activity::CommImpl*, void*, size_t), void* data, double rate)
109 {
110   xbt_assert(mbox, "No rendez-vous point defined for irecv");
111
112   return simcall_BODY_comm_irecv(receiver, mbox, static_cast<unsigned char*>(dst_buff), dst_buff_size, match_fun,
113                                  copy_data_fun, data, rate);
114 }
115
116 /**
117  * @ingroup simix_comm_management
118  */
119 ssize_t simcall_comm_waitany(simgrid::kernel::activity::CommImpl* comms[], size_t count,
120                              double timeout) // XBT_ATTRIB_DEPRECATED_v335
121 {
122   return simcall_BODY_comm_waitany(comms, count, timeout);
123 }
124
125 /**
126  * @ingroup simix_comm_management
127  */
128 ssize_t simcall_comm_testany(simgrid::kernel::activity::CommImpl* comms[], size_t count) // XBT_ATTRIB_DEPRECATED_v335
129 {
130   if (count == 0)
131     return -1;
132   return simcall_BODY_comm_testany(comms, count);
133 }
134
135 /**
136  * @ingroup simix_comm_management
137  */
138 void simcall_comm_wait(simgrid::kernel::activity::ActivityImpl* comm, double timeout)
139 {
140   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
141   simcall_BODY_comm_wait(static_cast<simgrid::kernel::activity::CommImpl*>(comm), timeout);
142 }
143
144 /**
145  * @ingroup simix_comm_management
146  *
147  */
148 bool simcall_comm_test(simgrid::kernel::activity::ActivityImpl* comm) // XBT_ATTRIB_DEPRECATED_v335
149 {
150   return simcall_BODY_comm_test(static_cast<simgrid::kernel::activity::CommImpl*>(comm));
151 }
152
153 void simcall_run_kernel(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
154 {
155   simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = observer;
156   simcall_BODY_run_kernel(&code);
157   simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = nullptr;
158 }
159
160 void simcall_run_blocking(std::function<void()> const& code, simgrid::kernel::actor::SimcallObserver* observer)
161 {
162   simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = observer;
163   simcall_BODY_run_blocking(&code);
164   simgrid::kernel::actor::ActorImpl::self()->simcall_.observer_ = nullptr;
165 }
166
167 /* ************************************************************************** */
168
169 /** @brief returns a printable string representing a simcall */
170 const char* SIMIX_simcall_name(const s_smx_simcall& simcall)
171 {
172   if (simcall.observer_ != nullptr) {
173     static std::string name;
174     name              = boost::core::demangle(typeid(*simcall.observer_).name());
175     const char* cname = name.c_str();
176     if (name.rfind("simgrid::kernel::", 0) == 0)
177       cname += 17; // strip prefix "simgrid::kernel::"
178     return cname;
179   } else {
180     return simcall_names[static_cast<int>(simcall.call_)];
181   }
182 }
183
184 namespace simgrid {
185 namespace simix {
186
187 void unblock(smx_actor_t actor)
188 {
189   xbt_assert(s4u::Actor::is_maestro());
190   actor->simcall_answer();
191 }
192 } // namespace simix
193 } // namespace simgrid