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"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm,s4u_activity,"S4U asynchronous communications");
21 s4u::Comm &Comm::send_init(s4u::Mailbox &chan) {
22 s4u::Comm *res = new s4u::Comm();
23 res->sender_ = SIMIX_process_self();
24 res->mailbox_ = &chan;
28 s4u::Comm &Comm::recv_init(s4u::Mailbox &chan) {
29 s4u::Comm *res = new s4u::Comm();
30 res->receiver_ = SIMIX_process_self();
31 res->mailbox_ = &chan;
35 void Comm::setRate(double rate) {
36 xbt_assert(state_==inited);
40 void Comm::setSrcData(void * buff) {
41 xbt_assert(state_==inited);
42 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
45 void Comm::setSrcDataSize(size_t size){
46 xbt_assert(state_==inited);
49 void Comm::setSrcData(void * buff, size_t size) {
50 xbt_assert(state_==inited);
52 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
56 void Comm::setDstData(void ** buff) {
57 xbt_assert(state_==inited);
58 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
61 size_t Comm::getDstDataSize(){
62 xbt_assert(state_==finished);
65 void Comm::setDstData(void ** buff, size_t size) {
66 xbt_assert(state_==inited);
68 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
74 xbt_assert(state_ == inited);
76 if (srcBuff_ != nullptr) { // Sender side
77 pimpl_ = simcall_comm_isend(sender_, mailbox_->getInferior(), remains_, rate_,
78 srcBuff_, srcBuffSize_,
79 matchFunction_, cleanFunction_, copyDataFunction_,
80 userData_, detached_);
81 } else if (dstBuff_ != nullptr) { // Receiver side
82 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
83 matchFunction_, copyDataFunction_,
87 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
92 xbt_assert(state_ == started || state_ == inited);
94 if (state_ == started)
95 simcall_comm_wait(pimpl_, -1/*timeout*/);
96 else {// p_state == inited. Save a simcall and do directly a blocking send/recv
97 if (srcBuff_ != nullptr) {
98 simcall_comm_send(sender_, mailbox_->getInferior(), remains_, rate_,
99 srcBuff_, srcBuffSize_,
100 matchFunction_, copyDataFunction_,
101 userData_, -1 /*timeout*/);
103 simcall_comm_recv(receiver_, mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
104 matchFunction_, copyDataFunction_,
105 userData_, -1/*timeout*/, rate_);
110 void Comm::wait(double timeout) {
111 xbt_assert(state_ == started || state_ == inited);
113 if (state_ == started) {
114 simcall_comm_wait(pimpl_, timeout);
119 // It's not started yet. Do it in one simcall
120 if (srcBuff_ != nullptr) {
121 simcall_comm_send(sender_, mailbox_->getInferior(), remains_, rate_,
122 srcBuff_, srcBuffSize_,
123 matchFunction_, copyDataFunction_,
126 simcall_comm_recv(receiver_, mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
127 matchFunction_, copyDataFunction_,
128 userData_, timeout, rate_);
133 s4u::Comm &Comm::send_async(Mailbox &dest, void *data, int simulatedSize) {
134 s4u::Comm &res = s4u::Comm::send_init(dest);
135 res.setRemains(simulatedSize);
137 res.srcBuffSize_ = sizeof(void*);
142 s4u::Comm &Comm::recv_async(Mailbox &dest, void **data) {
143 s4u::Comm &res = s4u::Comm::recv_init(dest);
144 res.setDstData(data);