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");
18 if (state_ == State::started && not detached_ && (pimpl_ == nullptr || pimpl_->state == SIMIX_RUNNING)) {
19 XBT_INFO("Comm %p freed before its completion. Detached: %d, State: %d", this, detached_, (int)state_);
20 if (pimpl_ != nullptr)
21 XBT_INFO("pimpl_->state: %d", pimpl_->state);
23 XBT_INFO("pimpl_ is null");
24 xbt_backtrace_display_current();
28 Activity* Comm::set_rate(double rate)
30 xbt_assert(state_ == State::inited);
35 Activity* Comm::set_src_data(void* buff)
37 xbt_assert(state_ == State::inited);
38 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
42 Activity* Comm::set_src_data_size(size_t size)
44 xbt_assert(state_ == State::inited);
48 Activity* Comm::set_src_data(void* buff, size_t size)
50 xbt_assert(state_ == State::inited);
52 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
57 Activity* Comm::set_dst_data(void** buff)
59 xbt_assert(state_ == State::inited);
60 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
64 size_t Comm::get_dst_data_size()
66 xbt_assert(state_ == State::finished);
69 Activity* Comm::set_dst_data(void** buff, size_t size)
71 xbt_assert(state_ == State::inited);
73 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
79 Activity* Comm::start()
81 xbt_assert(state_ == State::inited);
83 if (srcBuff_ != nullptr) { // Sender side
84 pimpl_ = simcall_comm_isend(sender_, mailbox_->get_impl(), remains_, rate_, srcBuff_, srcBuffSize_, matchFunction_,
85 cleanFunction_, copyDataFunction_, user_data_, detached_);
86 } else if (dstBuff_ != nullptr) { // Receiver side
87 xbt_assert(not detached_, "Receive cannot be detached");
88 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->get_impl(), dstBuff_, &dstBuffSize_, matchFunction_,
89 copyDataFunction_, user_data_, rate_);
92 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
94 state_ = State::started;
98 /** @brief Block the calling actor until the communication is finished */
99 Activity* Comm::wait()
101 return this->wait(-1);
104 /** @brief Block the calling actor until the communication is finished, or until timeout
106 * On timeout, an exception is thrown.
108 * @param timeout the amount of seconds to wait for the comm termination.
109 * Negative values denote infinite wait times. 0 as a timeout returns immediately. */
110 Activity* Comm::wait(double timeout)
113 case State::finished:
116 case State::inited: // It's not started yet. Do it in one simcall
117 if (srcBuff_ != nullptr) {
118 simcall_comm_send(sender_, mailbox_->get_impl(), remains_, rate_, srcBuff_, srcBuffSize_, matchFunction_,
119 copyDataFunction_, user_data_, timeout);
121 simcall_comm_recv(receiver_, mailbox_->get_impl(), dstBuff_, &dstBuffSize_, matchFunction_, copyDataFunction_,
122 user_data_, timeout, rate_);
124 state_ = State::finished;
128 simcall_comm_wait(pimpl_, timeout);
129 state_ = State::finished;
137 int Comm::test_any(std::vector<CommPtr>* comms)
139 smx_activity_t* array = new smx_activity_t[comms->size()];
140 for (unsigned int i = 0; i < comms->size(); i++) {
141 array[i] = comms->at(i)->pimpl_;
143 int res = simcall_comm_testany(array, comms->size());
148 Activity* Comm::detach()
150 xbt_assert(state_ == State::inited, "You cannot detach communications once they are started (not implemented).");
151 xbt_assert(srcBuff_ != nullptr && srcBuffSize_ != 0, "You can only detach sends, not recvs");
156 Activity* Comm::cancel()
158 simgrid::kernel::activity::CommImplPtr commPimpl =
159 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(pimpl_);
166 xbt_assert(state_ == State::inited || state_ == State::started || state_ == State::finished);
168 if (state_ == State::finished)
171 if (state_ == State::inited)
174 if(simcall_comm_test(pimpl_)){
175 state_ = State::finished;
181 MailboxPtr Comm::get_mailbox()
186 void intrusive_ptr_release(simgrid::s4u::Comm* c)
188 if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
189 std::atomic_thread_fence(std::memory_order_acquire);
193 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
195 c->refcount_.fetch_add(1, std::memory_order_relaxed);