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 "src/msg/msg_private.h"
10 #include "simgrid/s4u/comm.hpp"
11 #include <simgrid/s4u/Mailbox.hpp>
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm,s4u_activity,"S4U asynchronous communications");
25 s4u::Comm &Comm::send_init(s4u::MailboxPtr chan) {
26 s4u::Comm *res = new s4u::Comm();
27 res->sender_ = SIMIX_process_self();
32 s4u::Comm &Comm::recv_init(s4u::MailboxPtr chan) {
33 s4u::Comm *res = new s4u::Comm();
34 res->receiver_ = SIMIX_process_self();
39 void Comm::setRate(double rate) {
40 xbt_assert(state_==inited);
44 void Comm::setSrcData(void * buff) {
45 xbt_assert(state_==inited);
46 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
49 void Comm::setSrcDataSize(size_t size){
50 xbt_assert(state_==inited);
53 void Comm::setSrcData(void * buff, size_t size) {
54 xbt_assert(state_==inited);
56 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
60 void Comm::setDstData(void ** buff) {
61 xbt_assert(state_==inited);
62 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
65 size_t Comm::getDstDataSize(){
66 xbt_assert(state_==finished);
69 void Comm::setDstData(void ** buff, size_t size) {
70 xbt_assert(state_==inited);
72 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
78 xbt_assert(state_ == inited);
80 if (srcBuff_ != nullptr) { // Sender side
81 pimpl_ = simcall_comm_isend(sender_, mailbox_->getImpl(), remains_, rate_,
82 srcBuff_, srcBuffSize_,
83 matchFunction_, cleanFunction_, copyDataFunction_,
84 userData_, detached_);
85 } else if (dstBuff_ != nullptr) { // Receiver side
86 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
87 matchFunction_, copyDataFunction_,
91 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
96 xbt_assert(state_ == started || state_ == inited);
98 if (state_ == started)
99 simcall_comm_wait(pimpl_, -1/*timeout*/);
100 else {// p_state == inited. Save a simcall and do directly a blocking send/recv
101 if (srcBuff_ != nullptr) {
102 simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
103 srcBuff_, srcBuffSize_,
104 matchFunction_, copyDataFunction_,
105 userData_, -1 /*timeout*/);
107 simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
108 matchFunction_, copyDataFunction_,
109 userData_, -1/*timeout*/, rate_);
115 void Comm::wait(double timeout) {
116 xbt_assert(state_ == started || state_ == inited);
118 if (state_ == started) {
119 simcall_comm_wait(pimpl_, timeout);
124 // It's not started yet. Do it in one simcall
125 if (srcBuff_ != nullptr) {
126 simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
127 srcBuff_, srcBuffSize_,
128 matchFunction_, copyDataFunction_,
131 simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
132 matchFunction_, copyDataFunction_,
133 userData_, timeout, rate_);
139 s4u::Comm &Comm::send_async(MailboxPtr dest, void *data, int simulatedSize) {
140 s4u::Comm &res = s4u::Comm::send_init(dest);
141 res.setRemains(simulatedSize);
143 res.srcBuffSize_ = sizeof(void*);
148 s4u::Comm &Comm::recv_async(MailboxPtr dest, void **data) {
149 s4u::Comm &res = s4u::Comm::recv_init(dest);
150 res.setDstData(data);
156 xbt_assert(state_ == inited || state_ == started || state_ == finished);
158 if (state_ == finished)
159 xbt_die("Don't call test on a finished comm.");
161 if (state_ == inited) {
165 if(simcall_comm_test(pimpl_)){