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 s4u::CommPtr Comm::send_init(s4u::MailboxPtr chan)
30 CommPtr res = CommPtr(new s4u::Comm());
31 res->sender_ = SIMIX_process_self();
36 s4u::CommPtr Comm::recv_init(s4u::MailboxPtr chan)
38 CommPtr res = CommPtr(new s4u::Comm());
39 res->receiver_ = SIMIX_process_self();
44 void Comm::setRate(double rate) {
45 xbt_assert(state_==inited);
49 void Comm::setSrcData(void * buff) {
50 xbt_assert(state_==inited);
51 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
54 void Comm::setSrcDataSize(size_t size){
55 xbt_assert(state_==inited);
58 void Comm::setSrcData(void * buff, size_t size) {
59 xbt_assert(state_==inited);
61 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
65 void Comm::setDstData(void ** buff) {
66 xbt_assert(state_==inited);
67 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
70 size_t Comm::getDstDataSize(){
71 xbt_assert(state_==finished);
74 void Comm::setDstData(void ** buff, size_t size) {
75 xbt_assert(state_==inited);
77 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
83 xbt_assert(state_ == inited);
85 if (srcBuff_ != nullptr) { // Sender side
86 pimpl_ = simcall_comm_isend(sender_, mailbox_->getImpl(), remains_, rate_,
87 srcBuff_, srcBuffSize_,
88 matchFunction_, cleanFunction_, copyDataFunction_,
89 userData_, detached_);
90 } else if (dstBuff_ != nullptr) { // Receiver side
91 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
92 matchFunction_, copyDataFunction_,
96 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
101 xbt_assert(state_ == started || state_ == inited);
103 if (state_ == started)
104 simcall_comm_wait(pimpl_, -1/*timeout*/);
105 else { // state_ == inited. Save a simcall and do directly a blocking send/recv
106 if (srcBuff_ != nullptr) {
107 simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
108 srcBuff_, srcBuffSize_,
109 matchFunction_, copyDataFunction_,
110 userData_, -1 /*timeout*/);
112 simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
113 matchFunction_, copyDataFunction_,
114 userData_, -1/*timeout*/, rate_);
120 void Comm::wait(double timeout) {
121 xbt_assert(state_ == started || state_ == inited);
123 if (state_ == started) {
124 simcall_comm_wait(pimpl_, timeout);
129 // It's not started yet. Do it in one simcall
130 if (srcBuff_ != nullptr) {
131 simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
132 srcBuff_, srcBuffSize_,
133 matchFunction_, copyDataFunction_,
136 simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
137 matchFunction_, copyDataFunction_,
138 userData_, timeout, rate_);
143 void Comm::send_detached(MailboxPtr dest, void* data, int simulatedSize)
145 s4u::CommPtr res = CommPtr(s4u::Comm::send_init(dest));
146 res->setRemains(simulatedSize);
147 res->srcBuff_ = data;
148 res->srcBuffSize_ = sizeof(void*);
149 res->detached_ = true;
153 s4u::CommPtr Comm::send_async(MailboxPtr dest, void* data, int simulatedSize)
155 s4u::CommPtr res = CommPtr(s4u::Comm::send_init(dest));
156 res->setRemains(simulatedSize);
157 res->srcBuff_ = data;
158 res->srcBuffSize_ = sizeof(void*);
163 s4u::CommPtr Comm::recv_async(MailboxPtr dest, void** data)
165 s4u::CommPtr res = CommPtr(s4u::Comm::recv_init(dest));
166 res->setDstData(data, sizeof(*data));
173 simgrid::kernel::activity::CommImplPtr commPimpl =
174 boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(pimpl_);
179 xbt_assert(state_ == inited || state_ == started || state_ == finished);
181 if (state_ == finished)
182 xbt_die("Don't call test on a finished comm.");
184 if (state_ == inited) {
188 if(simcall_comm_test(pimpl_)){
195 void intrusive_ptr_release(simgrid::s4u::Comm* c)
197 if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
198 std::atomic_thread_fence(std::memory_order_acquire);
202 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
204 c->refcount_.fetch_add(1, std::memory_order_relaxed);