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");
17 s4u::Comm &Comm::send_init(s4u::MailboxPtr chan) {
18 s4u::Comm *res = new s4u::Comm();
19 res->sender_ = SIMIX_process_self();
24 s4u::Comm &Comm::recv_init(s4u::MailboxPtr chan) {
25 s4u::Comm *res = new s4u::Comm();
26 res->receiver_ = SIMIX_process_self();
31 void Comm::setRate(double rate) {
32 xbt_assert(state_==inited);
36 void Comm::setSrcData(void * buff) {
37 xbt_assert(state_==inited);
38 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
41 void Comm::setSrcDataSize(size_t size){
42 xbt_assert(state_==inited);
45 void Comm::setSrcData(void * buff, size_t size) {
46 xbt_assert(state_==inited);
48 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
52 void Comm::setDstData(void ** buff) {
53 xbt_assert(state_==inited);
54 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
57 size_t Comm::getDstDataSize(){
58 xbt_assert(state_==finished);
61 void Comm::setDstData(void ** buff, size_t size) {
62 xbt_assert(state_==inited);
64 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
70 xbt_assert(state_ == inited);
72 if (srcBuff_ != nullptr) { // Sender side
73 pimpl_ = simcall_comm_isend(sender_, mailbox_->getImpl(), remains_, rate_,
74 srcBuff_, srcBuffSize_,
75 matchFunction_, cleanFunction_, copyDataFunction_,
76 userData_, detached_);
77 } else if (dstBuff_ != nullptr) { // Receiver side
78 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
79 matchFunction_, copyDataFunction_,
83 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
88 xbt_assert(state_ == started || state_ == inited);
90 if (state_ == started)
91 simcall_comm_wait(pimpl_, -1/*timeout*/);
92 else {// p_state == inited. Save a simcall and do directly a blocking send/recv
93 if (srcBuff_ != nullptr) {
94 simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
95 srcBuff_, srcBuffSize_,
96 matchFunction_, copyDataFunction_,
97 userData_, -1 /*timeout*/);
99 simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
100 matchFunction_, copyDataFunction_,
101 userData_, -1/*timeout*/, rate_);
107 void Comm::wait(double timeout) {
108 xbt_assert(state_ == started || state_ == inited);
110 if (state_ == started) {
111 simcall_comm_wait(pimpl_, timeout);
116 // It's not started yet. Do it in one simcall
117 if (srcBuff_ != nullptr) {
118 simcall_comm_send(sender_, mailbox_->getImpl(), remains_, rate_,
119 srcBuff_, srcBuffSize_,
120 matchFunction_, copyDataFunction_,
123 simcall_comm_recv(receiver_, mailbox_->getImpl(), dstBuff_, &dstBuffSize_,
124 matchFunction_, copyDataFunction_,
125 userData_, timeout, rate_);
131 s4u::Comm &Comm::send_async(MailboxPtr dest, void *data, int simulatedSize) {
132 s4u::Comm &res = s4u::Comm::send_init(dest);
133 res.setRemains(simulatedSize);
135 res.srcBuffSize_ = sizeof(void*);
140 s4u::Comm &Comm::recv_async(MailboxPtr dest, void **data) {
141 s4u::Comm &res = s4u::Comm::recv_init(dest);
142 res.setDstData(data, sizeof(*data));
148 xbt_assert(state_ == inited || state_ == started || state_ == finished);
150 if (state_ == finished)
151 xbt_die("Don't call test on a finished comm.");
153 if (state_ == inited) {
157 if(simcall_comm_test(pimpl_)){