1 /* Copyright (c) 2006-2017. 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_ == started && not detached_ && (pimpl_ == nullptr || pimpl_->state == SIMIX_RUNNING)) {
19 XBT_INFO("Comm %p freed before its completion. Detached: %d, State: %d", this, detached_, 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::setRate(double rate)
30 xbt_assert(state_==inited);
35 Activity* Comm::setSrcData(void* buff)
37 xbt_assert(state_==inited);
38 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
42 Activity* Comm::setSrcDataSize(size_t size)
44 xbt_assert(state_==inited);
48 Activity* Comm::setSrcData(void* buff, size_t size)
50 xbt_assert(state_==inited);
52 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
57 Activity* Comm::setDstData(void** buff)
59 xbt_assert(state_==inited);
60 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
64 size_t Comm::getDstDataSize(){
65 xbt_assert(state_==finished);
68 Activity* Comm::setDstData(void** buff, size_t size)
70 xbt_assert(state_==inited);
72 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
78 Activity* Comm::start()
80 xbt_assert(state_ == inited);
82 if (srcBuff_ != nullptr) { // Sender side
83 pimpl_ = simcall_comm_isend(sender_, mailbox_->getImpl(), remains_, rate_,
84 srcBuff_, srcBuffSize_,
85 matchFunction_, cleanFunction_, copyDataFunction_,
86 userData_, detached_);
87 } else if (dstBuff_ != nullptr) { // Receiver side
88 xbt_assert(not detached_, "Receive cannot be detached");
89 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
90 matchFunction_, copyDataFunction_,
94 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
100 /** @brief Block the calling actor until the communication is finished */
101 Activity* Comm::wait()
103 return this->wait(-1);
106 /** @brief Block the calling actor until the communication is finished, or until timeout
108 * On timeout, an exception is thrown.
110 * @param timeout the amount of seconds to wait for the comm termination.
111 * Negative values denote infinite wait times. 0 as a timeout returns immediately. */
112 Activity* Comm::wait(double timeout)
118 case inited: // It's not started yet. Do it in one simcall
119 if (srcBuff_ != nullptr) {
120 simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_, srcBuff_, srcBuffSize_, matchFunction_,
121 copyDataFunction_, userData_, timeout);
123 simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_, matchFunction_, copyDataFunction_,
124 userData_, timeout, rate_);
130 simcall_comm_wait(pimpl_, timeout);
139 int Comm::test_any(std::vector<CommPtr>* comms)
141 smx_activity_t* array = new smx_activity_t[comms->size()];
142 for (unsigned int i = 0; i < comms->size(); i++) {
143 array[i] = comms->at(i)->pimpl_;
145 int res = simcall_comm_testany(array, comms->size());
150 Activity* Comm::detach()
152 xbt_assert(state_ == inited, "You cannot detach communications once they are started.");
153 xbt_assert(srcBuff_ != nullptr && srcBuffSize_ != 0, "You can only detach sends, not recvs");
158 Activity* Comm::cancel()
160 simgrid::kernel::activity::CommImplPtr commPimpl =
161 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(pimpl_);
168 xbt_assert(state_ == inited || state_ == started || state_ == finished);
170 if (state_ == finished) {
174 if (state_ == inited) {
178 if(simcall_comm_test(pimpl_)){
185 MailboxPtr Comm::getMailbox()
190 void intrusive_ptr_release(simgrid::s4u::Comm* c)
192 if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
193 std::atomic_thread_fence(std::memory_order_acquire);
197 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
199 c->refcount_.fetch_add(1, std::memory_order_relaxed);