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. */
7 #include "src/msg/msg_private.h"
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 void Comm::setRate(double rate) {
29 xbt_assert(state_==inited);
33 void Comm::setSrcData(void * buff) {
34 xbt_assert(state_==inited);
35 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
38 void Comm::setSrcDataSize(size_t size){
39 xbt_assert(state_==inited);
42 void Comm::setSrcData(void * buff, size_t size) {
43 xbt_assert(state_==inited);
45 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
49 void Comm::setDstData(void ** buff) {
50 xbt_assert(state_==inited);
51 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
54 size_t Comm::getDstDataSize(){
55 xbt_assert(state_==finished);
58 void Comm::setDstData(void ** buff, size_t size) {
59 xbt_assert(state_==inited);
61 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
67 xbt_assert(state_ == inited);
69 if (srcBuff_ != nullptr) { // Sender side
70 pimpl_ = simcall_comm_isend(sender_, mailbox_->getImpl(), remains_, rate_,
71 srcBuff_, srcBuffSize_,
72 matchFunction_, cleanFunction_, copyDataFunction_,
73 userData_, detached_);
74 } else if (dstBuff_ != nullptr) { // Receiver side
75 xbt_assert(not detached_, "Receive cannot be detached");
76 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
77 matchFunction_, copyDataFunction_,
81 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
86 xbt_assert(state_ == started || state_ == inited);
88 if (state_ == started)
89 simcall_comm_wait(pimpl_, -1/*timeout*/);
90 else { // state_ == inited. Save a simcall and do directly a blocking send/recv
91 if (srcBuff_ != nullptr) {
92 simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
93 srcBuff_, srcBuffSize_,
94 matchFunction_, copyDataFunction_,
95 userData_, -1 /*timeout*/);
97 simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
98 matchFunction_, copyDataFunction_,
99 userData_, -1/*timeout*/, rate_);
105 void Comm::wait(double timeout) {
106 xbt_assert(state_ == started || state_ == inited);
108 if (state_ == started) {
109 simcall_comm_wait(pimpl_, timeout);
114 // It's not started yet. Do it in one simcall
115 if (srcBuff_ != nullptr) {
116 simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
117 srcBuff_, srcBuffSize_,
118 matchFunction_, copyDataFunction_,
121 simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
122 matchFunction_, copyDataFunction_,
123 userData_, timeout, rate_);
130 xbt_assert(state_ == inited, "You cannot detach communications once they are started.");
131 xbt_assert(srcBuff_ != nullptr && srcBuffSize_ != 0, "You can only detach sends, not recvs");
138 simgrid::kernel::activity::CommImplPtr commPimpl =
139 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(pimpl_);
144 xbt_assert(state_ == inited || state_ == started || state_ == finished);
146 if (state_ == finished) {
150 if (state_ == inited) {
154 if(simcall_comm_test(pimpl_)){
161 void intrusive_ptr_release(simgrid::s4u::Comm* c)
163 if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
164 std::atomic_thread_fence(std::memory_order_acquire);
168 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
170 c->refcount_.fetch_add(1, std::memory_order_relaxed);