Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
every setter in s4u::Activity return the activity
[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 "src/msg/msg_private.hpp"
7 #include "xbt/log.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 Activity* Comm::setRate(double rate)
29 {
30   xbt_assert(state_==inited);
31   rate_ = rate;
32   return this;
33 }
34
35 Activity* Comm::setSrcData(void* buff)
36 {
37   xbt_assert(state_==inited);
38   xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
39   srcBuff_ = buff;
40   return this;
41 }
42 Activity* Comm::setSrcDataSize(size_t size)
43 {
44   xbt_assert(state_==inited);
45   srcBuffSize_ = size;
46   return this;
47 }
48 Activity* Comm::setSrcData(void* buff, size_t size)
49 {
50   xbt_assert(state_==inited);
51
52   xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
53   srcBuff_ = buff;
54   srcBuffSize_ = size;
55   return this;
56 }
57 Activity* Comm::setDstData(void** buff)
58 {
59   xbt_assert(state_==inited);
60   xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
61   dstBuff_ = buff;
62   return this;
63 }
64 size_t Comm::getDstDataSize(){
65   xbt_assert(state_==finished);
66   return dstBuffSize_;
67 }
68 Activity* Comm::setDstData(void** buff, size_t size)
69 {
70   xbt_assert(state_==inited);
71
72   xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
73   dstBuff_ = buff;
74   dstBuffSize_ = size;
75   return this;
76 }
77
78 Activity* Comm::start()
79 {
80   xbt_assert(state_ == inited);
81
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_,
91         userData_, rate_);
92
93   } else {
94     xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
95   }
96   state_ = started;
97   return this;
98 }
99
100 /** @brief Block the calling actor until the communication is finished */
101 Activity* Comm::wait()
102 {
103   return this->wait(-1);
104 }
105
106 /** @brief Block the calling actor until the communication is finished, or until timeout
107  *
108  * On timeout, an exception is thrown.
109  *
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)
113 {
114   switch (state_) {
115     case finished:
116       return this;
117
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);
122       } else { // Receiver
123         simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_, matchFunction_, copyDataFunction_,
124                           userData_, timeout, rate_);
125       }
126       state_ = finished;
127       return this;
128
129     case started:
130       simcall_comm_wait(pimpl_, timeout);
131       state_ = finished;
132       return this;
133
134     default:
135       THROW_IMPOSSIBLE;
136   }
137   return this;
138 }
139
140 Activity* Comm::detach()
141 {
142   xbt_assert(state_ == inited, "You cannot detach communications once they are started.");
143   xbt_assert(srcBuff_ != nullptr && srcBuffSize_ != 0, "You can only detach sends, not recvs");
144   detached_ = true;
145   return start();
146 }
147
148 Activity* Comm::cancel()
149 {
150   simgrid::kernel::activity::CommImplPtr commPimpl =
151       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(pimpl_);
152   commPimpl->cancel();
153   return this;
154 }
155
156 bool Comm::test()
157 {
158   xbt_assert(state_ == inited || state_ == started || state_ == finished);
159
160   if (state_ == finished) {
161     return true;
162   }
163
164   if (state_ == inited) {
165     this->start();
166   }
167
168   if(simcall_comm_test(pimpl_)){
169     state_ = finished;
170     return true;
171   }
172   return false;
173 }
174
175 MailboxPtr Comm::getMailbox()
176 {
177   return mailbox_;
178 }
179
180 void intrusive_ptr_release(simgrid::s4u::Comm* c)
181 {
182   if (c->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
183     std::atomic_thread_fence(std::memory_order_acquire);
184     delete c;
185   }
186 }
187 void intrusive_ptr_add_ref(simgrid::s4u::Comm* c)
188 {
189   c->refcount_.fetch_add(1, std::memory_order_relaxed);
190 }
191 }
192 } // namespaces