Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make sure that the user don't try a detached receive
[simgrid.git] / src / s4u / s4u_comm.cpp
1 /* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "xbt/log.h"
7 #include "src/msg/msg_private.h"
8
9 #include "simgrid/s4u/Comm.hpp"
10 #include "simgrid/s4u/Mailbox.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm,s4u_activity,"S4U asynchronous communications");
13
14 namespace simgrid {
15 namespace s4u {
16 Comm::~Comm()
17 {
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);
22     else
23       XBT_INFO("pimpl_ is null");
24     xbt_backtrace_display_current();
25   }
26 }
27
28 s4u::CommPtr Comm::send_init(s4u::MailboxPtr chan)
29 {
30   CommPtr res   = CommPtr(new s4u::Comm());
31   res->sender_ = SIMIX_process_self();
32   res->mailbox_ = chan;
33   return res;
34 }
35
36 s4u::CommPtr Comm::recv_init(s4u::MailboxPtr chan)
37 {
38   CommPtr res    = CommPtr(new s4u::Comm());
39   res->receiver_ = SIMIX_process_self();
40   res->mailbox_ = chan;
41   return res;
42 }
43
44 void Comm::setRate(double rate) {
45   xbt_assert(state_==inited);
46   rate_ = rate;
47 }
48
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");
52   srcBuff_ = buff;
53 }
54 void Comm::setSrcDataSize(size_t size){
55   xbt_assert(state_==inited);
56   srcBuffSize_ = size;
57 }
58 void Comm::setSrcData(void * buff, size_t size) {
59   xbt_assert(state_==inited);
60
61   xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
62   srcBuff_ = buff;
63   srcBuffSize_ = size;
64 }
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");
68   dstBuff_ = buff;
69 }
70 size_t Comm::getDstDataSize(){
71   xbt_assert(state_==finished);
72   return dstBuffSize_;
73 }
74 void Comm::setDstData(void ** buff, size_t size) {
75   xbt_assert(state_==inited);
76
77   xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
78   dstBuff_ = buff;
79   dstBuffSize_ = size;
80 }
81
82 void Comm::start() {
83   xbt_assert(state_ == inited);
84
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     xbt_assert(not detached_, "Receive cannot be detached");
92     pimpl_ = simcall_comm_irecv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
93         matchFunction_, copyDataFunction_,
94         userData_, rate_);
95
96   } else {
97     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
98   }
99   state_ = started;
100 }
101 void Comm::wait() {
102   xbt_assert(state_ == started || state_ == inited);
103
104   if (state_ == started)
105     simcall_comm_wait(pimpl_, -1/*timeout*/);
106   else { // state_ == inited. Save a simcall and do directly a blocking send/recv
107     if (srcBuff_ != nullptr) {
108       simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
109           srcBuff_, srcBuffSize_,
110           matchFunction_, copyDataFunction_,
111           userData_, -1 /*timeout*/);
112     } else {
113       simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
114           matchFunction_, copyDataFunction_,
115           userData_, -1/*timeout*/, rate_);
116     }
117   }
118   state_ = finished;
119 }
120
121 void Comm::wait(double timeout) {
122   xbt_assert(state_ == started || state_ == inited);
123
124   if (state_ == started) {
125     simcall_comm_wait(pimpl_, timeout);
126     state_ = finished;
127     return;
128   }
129
130   // It's not started yet. Do it in one simcall
131   if (srcBuff_ != nullptr) {
132     simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
133         srcBuff_, srcBuffSize_,
134         matchFunction_, copyDataFunction_,
135         userData_, timeout);
136   } else { // Receiver
137     simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
138         matchFunction_, copyDataFunction_,
139         userData_, timeout, rate_);
140   }
141   state_ = finished;
142 }
143
144 void Comm::send_detached(MailboxPtr dest, void* data, int simulatedSize)
145 {
146   s4u::CommPtr res = CommPtr(s4u::Comm::send_init(dest));
147   res->setRemains(simulatedSize);
148   res->srcBuff_     = data;
149   res->srcBuffSize_ = sizeof(void*);
150   res->detached_    = true;
151   res->start();
152 }
153
154 s4u::CommPtr Comm::send_async(MailboxPtr dest, void* data, int simulatedSize)
155 {
156   s4u::CommPtr res = CommPtr(s4u::Comm::send_init(dest));
157   res->setRemains(simulatedSize);
158   res->srcBuff_     = data;
159   res->srcBuffSize_ = sizeof(void*);
160   res->start();
161   return res;
162 }
163
164 s4u::CommPtr Comm::recv_async(MailboxPtr dest, void** data)
165 {
166   s4u::CommPtr res = CommPtr(s4u::Comm::recv_init(dest));
167   res->setDstData(data, sizeof(*data));
168   res->start();
169   return res;
170 }
171
172 void Comm::cancel()
173 {
174   simgrid::kernel::activity::CommImplPtr commPimpl =
175       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(pimpl_);
176   commPimpl->cancel();
177 }
178
179 bool Comm::test() {
180   xbt_assert(state_ == inited || state_ == started || state_ == finished);
181
182   if (state_ == finished)
183     xbt_die("Don't call test on a finished comm.");
184
185   if (state_ == inited) {
186     this->start();
187   }
188
189   if(simcall_comm_test(pimpl_)){
190     state_ = finished;
191     return true;
192   }
193   return false;
194 }
195
196 void intrusive_ptr_release(simgrid::s4u::Comm* c)
197 {
198   if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
199     std::atomic_thread_fence(std::memory_order_acquire);
200     delete c;
201   }
202 }
203 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
204 {
205   c->refcount_.fetch_add(1, std::memory_order_relaxed);
206 }
207 }
208 } // namespaces