1 /* Copyright (c) 2006-2018. 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/s4u/Comm.hpp"
10 #include "simgrid/s4u/Mailbox.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm, s4u_activity, "S4U asynchronous communications");
16 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Comm::on_sender_start;
17 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Comm::on_receiver_start;
18 simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> s4u::Comm::on_completion;
22 if (state_ == State::STARTED && not detached_ && (pimpl_ == nullptr || pimpl_->state_ == SIMIX_RUNNING)) {
23 XBT_INFO("Comm %p freed before its completion. Detached: %d, State: %d", this, detached_, (int)state_);
24 if (pimpl_ != nullptr)
25 XBT_INFO("pimpl_->state: %d", pimpl_->state_);
27 XBT_INFO("pimpl_ is null");
28 xbt_backtrace_display_current();
32 int Comm::wait_any_for(std::vector<CommPtr>* comms_in, double timeout)
34 // Map to dynar<Synchro*>:
35 xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), [](void* ptr) {
36 intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
38 for (auto const& comm : *comms_in) {
39 if (comm->state_ == Activity::State::INITED)
41 xbt_assert(comm->state_ == Activity::State::STARTED);
42 simgrid::kernel::activity::ActivityImpl* ptr = comm->pimpl_.get();
43 intrusive_ptr_add_ref(ptr);
44 xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, ptr);
46 // Call the underlying simcall:
47 int idx = simcall_comm_waitany(comms, timeout);
48 xbt_dynar_free(&comms);
52 void Comm::wait_all(std::vector<CommPtr>* comms)
54 // TODO: this should be a simcall or something
55 // TODO: we are missing a version with timeout
56 for (CommPtr comm : *comms)
60 Activity* Comm::set_rate(double rate)
62 xbt_assert(state_ == State::INITED);
67 Activity* Comm::set_src_data(void* buff)
69 xbt_assert(state_ == State::INITED);
70 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
74 Activity* Comm::set_src_data_size(size_t size)
76 xbt_assert(state_ == State::INITED);
77 src_buff_size_ = size;
80 Activity* Comm::set_src_data(void* buff, size_t size)
82 xbt_assert(state_ == State::INITED);
84 xbt_assert(dst_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
86 src_buff_size_ = size;
89 Activity* Comm::set_dst_data(void** buff)
91 xbt_assert(state_ == State::INITED);
92 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
96 size_t Comm::get_dst_data_size()
98 xbt_assert(state_ == State::FINISHED);
99 return dst_buff_size_;
101 Activity* Comm::set_dst_data(void** buff, size_t size)
103 xbt_assert(state_ == State::INITED);
105 xbt_assert(src_buff_ == nullptr, "Cannot set the src and dst buffers at the same time");
107 dst_buff_size_ = size;
111 Activity* Comm::start()
113 xbt_assert(state_ == State::INITED);
115 if (src_buff_ != nullptr) { // Sender side
116 on_sender_start(Actor::self());
117 pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
118 clean_fun_, copy_data_function_, user_data_, detached_);
119 } else if (dst_buff_ != nullptr) { // Receiver side
120 xbt_assert(not detached_, "Receive cannot be detached");
121 on_receiver_start(Actor::self());
122 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_,
123 copy_data_function_, user_data_, rate_);
126 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
128 state_ = State::STARTED;
132 /** @brief Block the calling actor until the communication is finished */
133 Activity* Comm::wait()
135 return this->wait_for(-1);
138 /** @brief Block the calling actor until the communication is finished, or until timeout
140 * On timeout, an exception is thrown.
142 * @param timeout the amount of seconds to wait for the comm termination.
143 * Negative values denote infinite wait times. 0 as a timeout returns immediately. */
144 Activity* Comm::wait_for(double timeout)
147 case State::FINISHED:
150 case State::INITED: // It's not started yet. Do it in one simcall
151 if (src_buff_ != nullptr) {
152 on_sender_start(Actor::self());
153 simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, src_buff_, src_buff_size_, match_fun_,
154 copy_data_function_, user_data_, timeout);
157 on_receiver_start(Actor::self());
158 simcall_comm_recv(receiver_, mailbox_->get_impl(), dst_buff_, &dst_buff_size_, match_fun_, copy_data_function_,
159 user_data_, timeout, rate_);
161 state_ = State::FINISHED;
165 simcall_comm_wait(pimpl_, timeout);
166 on_completion(Actor::self());
167 state_ = State::FINISHED;
175 int Comm::test_any(std::vector<CommPtr>* comms)
177 smx_activity_t* array = new smx_activity_t[comms->size()];
178 for (unsigned int i = 0; i < comms->size(); i++) {
179 array[i] = comms->at(i)->pimpl_;
181 int res = simcall_comm_testany(array, comms->size());
186 Activity* Comm::detach()
188 xbt_assert(state_ == State::INITED, "You cannot detach communications once they are started (not implemented).");
189 xbt_assert(src_buff_ != nullptr && src_buff_size_ != 0, "You can only detach sends, not recvs");
194 Activity* Comm::cancel()
196 simgrid::simix::simcall([this] { dynamic_cast<kernel::activity::CommImpl*>(pimpl_.get())->cancel(); });
197 state_ = State::CANCELED;
203 xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
205 if (state_ == State::FINISHED)
208 if (state_ == State::INITED)
211 if (simcall_comm_test(pimpl_)) {
212 state_ = State::FINISHED;
218 MailboxPtr Comm::get_mailbox()
223 void intrusive_ptr_release(simgrid::s4u::Comm* c)
225 if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
226 std::atomic_thread_fence(std::memory_order_acquire);
230 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
232 c->refcount_.fetch_add(1, std::memory_order_relaxed);
235 } // namespace simgrid