1 /* Copyright (c) 2006-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "simgrid/s4u/comm.hpp"
9 #include <simgrid/s4u/Mailbox.hpp>
10 #include "../msg/msg_private.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm,s4u_activity,"S4U asynchronous communications");
24 s4u::Comm &Comm::send_init(s4u::MailboxPtr chan) {
25 s4u::Comm *res = new s4u::Comm();
26 res->sender_ = SIMIX_process_self();
31 s4u::Comm &Comm::recv_init(s4u::MailboxPtr chan) {
32 s4u::Comm *res = new s4u::Comm();
33 res->receiver_ = SIMIX_process_self();
38 void Comm::setRate(double rate) {
39 xbt_assert(state_==inited);
43 void Comm::setSrcData(void * buff) {
44 xbt_assert(state_==inited);
45 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
48 void Comm::setSrcDataSize(size_t size){
49 xbt_assert(state_==inited);
52 void Comm::setSrcData(void * buff, size_t size) {
53 xbt_assert(state_==inited);
55 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
59 void Comm::setDstData(void ** buff) {
60 xbt_assert(state_==inited);
61 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 void Comm::setDstData(void ** buff, size_t size) {
69 xbt_assert(state_==inited);
71 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
77 xbt_assert(state_ == inited);
79 if (srcBuff_ != nullptr) { // Sender side
80 pimpl_ = simcall_comm_isend(sender_, mailbox_->getImpl(), remains_, rate_,
81 srcBuff_, srcBuffSize_,
82 matchFunction_, cleanFunction_, copyDataFunction_,
83 userData_, detached_);
84 } else if (dstBuff_ != nullptr) { // Receiver side
85 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
86 matchFunction_, copyDataFunction_,
90 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
95 xbt_assert(state_ == started || state_ == inited);
97 if (state_ == started)
98 simcall_comm_wait(pimpl_, -1/*timeout*/);
99 else {// p_state == inited. Save a simcall and do directly a blocking send/recv
100 if (srcBuff_ != nullptr) {
101 simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
102 srcBuff_, srcBuffSize_,
103 matchFunction_, copyDataFunction_,
104 userData_, -1 /*timeout*/);
106 simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
107 matchFunction_, copyDataFunction_,
108 userData_, -1/*timeout*/, rate_);
114 void Comm::wait(double timeout) {
115 xbt_assert(state_ == started || state_ == inited);
117 if (state_ == started) {
118 simcall_comm_wait(pimpl_, timeout);
123 // It's not started yet. Do it in one simcall
124 if (srcBuff_ != nullptr) {
125 simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
126 srcBuff_, srcBuffSize_,
127 matchFunction_, copyDataFunction_,
130 simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
131 matchFunction_, copyDataFunction_,
132 userData_, timeout, rate_);
138 s4u::Comm &Comm::send_async(MailboxPtr dest, void *data, int simulatedSize) {
139 s4u::Comm &res = s4u::Comm::send_init(dest);
140 res.setRemains(simulatedSize);
142 res.srcBuffSize_ = sizeof(void*);
147 s4u::Comm &Comm::recv_async(MailboxPtr dest, void **data) {
148 s4u::Comm &res = s4u::Comm::recv_init(dest);
149 res.setDstData(data);
155 xbt_assert(state_ == inited || state_ == started || state_ == finished);
157 if (state_ == finished)
158 xbt_die("Don't call test on a finished comm.");
160 if (state_ == inited) {
164 if(simcall_comm_test(pimpl_)){