1 /* Copyright (c) 2006-2019. 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 #include "src/msg/msg_private.hpp"
9 #include "simgrid/Exception.hpp"
10 #include "simgrid/s4u/Comm.hpp"
11 #include "simgrid/s4u/Mailbox.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
17 xbt::signal<void(Actor const&)> Comm::on_sender_start;
18 xbt::signal<void(Actor const&)> Comm::on_receiver_start;
19 xbt::signal<void(Actor const&)> Comm::on_completion;
23 if (state_ == State::STARTED && not detached_ && (pimpl_ == nullptr || pimpl_->state_ == SIMIX_RUNNING)) {
24 XBT_INFO("Comm %p freed before its completion. Detached: %d, State: %d", this, detached_, (int)state_);
25 if (pimpl_ != nullptr)
26 XBT_INFO("pimpl_->state: %d", pimpl_->state_);
28 XBT_INFO("pimpl_ is null");
29 xbt_backtrace_display_current();
33 int Comm::wait_any_for(std::vector<CommPtr>* comms, double timeout)
35 std::unique_ptr<kernel::activity::CommImpl* []> rcomms(new kernel::activity::CommImpl*[comms->size()]);
36 std::transform(begin(*comms), end(*comms), rcomms.get(),
37 [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
38 return simcall_comm_waitany(rcomms.get(), comms->size(), timeout);
41 void Comm::wait_all(std::vector<CommPtr>* comms)
43 // TODO: this should be a simcall or something
44 // TODO: we are missing a version with timeout
45 for (CommPtr comm : *comms)
49 CommPtr Comm::set_rate(double rate)
51 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
57 CommPtr Comm::set_src_data(void* buff)
59 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
61 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
66 CommPtr Comm::set_src_data_size(size_t size)
68 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
70 src_buff_size_ = size;
74 CommPtr Comm::set_src_data(void* buff, size_t size)
76 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
79 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
81 src_buff_size_ = size;
84 CommPtr Comm::set_dst_data(void** buff)
86 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
88 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
93 size_t Comm::get_dst_data_size()
95 xbt_assert(state_ == State::FINISHED, "You cannot use %s before your communication terminated", __FUNCTION__);
96 return dst_buff_size_;
98 CommPtr Comm::set_dst_data(void** buff, size_t size)
100 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
103 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
105 dst_buff_size_ = size;
109 CommPtr Comm::set_tracing_category(const std::string& category)
111 xbt_assert(state_ == State::INITED, "Cannot change the tracing category of an exec after its start");
112 tracing_category_ = category;
118 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
121 if (src_buff_ != nullptr) { // Sender side
122 on_sender_start(*Actor::self());
123 pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
124 clean_fun_, copy_data_function_, user_data_, detached_);
125 } else if (dst_buff_ != nullptr) { // Receiver side
126 xbt_assert(not detached_, "Receive cannot be detached");
127 on_receiver_start(*Actor::self());
128 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
129 copy_data_function_, user_data_, rate_);
132 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
134 state_ = State::STARTED;
138 /** @brief Block the calling actor until the communication is finished */
141 return this->wait_for(-1);
144 /** @brief Block the calling actor until the communication is finished, or until timeout
146 * On timeout, an exception is thrown and the communication is invalidated.
148 * @param timeout the amount of seconds to wait for the comm termination.
149 * Negative values denote infinite wait times. 0 as a timeout returns immediately. */
150 Comm* Comm::wait_for(double timeout)
153 case State::FINISHED:
156 case State::INITED: // It's not started yet. Do it in one simcall
157 if (src_buff_ != nullptr) {
158 on_sender_start(*Actor::self());
159 simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
160 copy_data_function_, user_data_, timeout);
163 on_receiver_start(*Actor::self());
164 simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
165 user_data_, timeout, rate_);
167 state_ = State::FINISHED;
171 simcall_comm_wait(pimpl_, timeout);
172 on_completion(*Actor::self());
173 state_ = State::FINISHED;
176 case State::CANCELED:
177 throw CancelException(XBT_THROW_POINT, "Communication canceled");
184 int Comm::test_any(std::vector<CommPtr>* comms)
186 std::unique_ptr<kernel::activity::CommImpl* []> rcomms(new kernel::activity::CommImpl*[comms->size()]);
187 std::transform(begin(*comms), end(*comms), rcomms.get(),
188 [](const CommPtr& comm) { return static_cast<kernel::activity::CommImpl*>(comm->pimpl_.get()); });
189 return simcall_comm_testany(rcomms.get(), comms->size());
194 xbt_assert(state_ == State::INITED, "You cannot use %s() once your communication started (not implemented)",
196 xbt_assert(src_buff_ != nullptr && src_buff_size_ != 0, "You can only detach sends, not recvs");
203 simix::simcall([this] {
205 boost::static_pointer_cast<kernel::activity::CommImpl>(pimpl_)->cancel();
207 state_ = State::CANCELED;
213 xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
215 if (state_ == State::FINISHED)
218 if (state_ == State::INITED)
221 if (simcall_comm_test(pimpl_)) {
222 state_ = State::FINISHED;
228 Mailbox* Comm::get_mailbox()
233 Actor* Comm::get_sender()
235 return sender_ ? sender_->ciface() : nullptr;
238 void intrusive_ptr_release(simgrid::s4u::Comm* c)
240 if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
241 std::atomic_thread_fence(std::memory_order_acquire);
245 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
247 c->refcount_.fetch_add(1, std::memory_order_relaxed);
250 } // namespace simgrid