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_);
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_);
137 s4u::Comm &Comm::send_async(MailboxPtr dest, void *data, int simulatedSize) {
138 s4u::Comm &res = s4u::Comm::send_init(dest);
139 res.setRemains(simulatedSize);
141 res.srcBuffSize_ = sizeof(void*);
146 s4u::Comm &Comm::recv_async(MailboxPtr dest, void **data) {
147 s4u::Comm &res = s4u::Comm::recv_init(dest);
148 res.setDstData(data);
154 xbt_assert(state_ == inited || state_ == started || state_ == finished);
156 if (state_ == finished)
157 xbt_die("Don't call test on a finished comm.");
159 if (state_ == inited) {
163 if(simcall_comm_test(pimpl_)){