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");
23 s4u::Comm &Comm::send_init(s4u::Mailbox &chan) {
24 s4u::Comm *res = new s4u::Comm();
25 res->sender_ = SIMIX_process_self();
26 res->mailbox_ = &chan;
30 s4u::Comm &Comm::recv_init(s4u::Mailbox &chan) {
31 s4u::Comm *res = new s4u::Comm();
32 res->receiver_ = SIMIX_process_self();
33 res->mailbox_ = &chan;
37 void Comm::setRate(double rate) {
38 xbt_assert(state_==inited);
42 void Comm::setSrcData(void * buff) {
43 xbt_assert(state_==inited);
44 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
47 void Comm::setSrcDataSize(size_t size){
48 xbt_assert(state_==inited);
51 void Comm::setSrcData(void * buff, size_t size) {
52 xbt_assert(state_==inited);
54 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
58 void Comm::setDstData(void ** buff) {
59 xbt_assert(state_==inited);
60 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
63 size_t Comm::getDstDataSize(){
64 xbt_assert(state_==finished);
67 void Comm::setDstData(void ** buff, size_t size) {
68 xbt_assert(state_==inited);
70 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
76 xbt_assert(state_ == inited);
78 if (srcBuff_ != nullptr) { // Sender side
79 pimpl_ = simcall_comm_isend(sender_, mailbox_->getInferior(), remains_, rate_,
80 srcBuff_, srcBuffSize_,
81 matchFunction_, cleanFunction_, copyDataFunction_,
82 userData_, detached_);
83 } else if (dstBuff_ != nullptr) { // Receiver side
84 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
85 matchFunction_, copyDataFunction_,
89 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
94 xbt_assert(state_ == started || state_ == inited);
96 if (state_ == started)
97 simcall_comm_wait(pimpl_, -1/*timeout*/);
98 else {// p_state == inited. Save a simcall and do directly a blocking send/recv
99 if (srcBuff_ != nullptr) {
100 simcall_comm_send(sender_, mailbox_->getInferior(), remains_, rate_,
101 srcBuff_, srcBuffSize_,
102 matchFunction_, copyDataFunction_,
103 userData_, -1 /*timeout*/);
105 simcall_comm_recv(receiver_, mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
106 matchFunction_, copyDataFunction_,
107 userData_, -1/*timeout*/, rate_);
112 void Comm::wait(double timeout) {
113 xbt_assert(state_ == started || state_ == inited);
115 if (state_ == started) {
116 simcall_comm_wait(pimpl_, timeout);
121 // It's not started yet. Do it in one simcall
122 if (srcBuff_ != nullptr) {
123 simcall_comm_send(sender_, mailbox_->getInferior(), remains_, rate_,
124 srcBuff_, srcBuffSize_,
125 matchFunction_, copyDataFunction_,
128 simcall_comm_recv(receiver_, mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
129 matchFunction_, copyDataFunction_,
130 userData_, timeout, rate_);
135 s4u::Comm &Comm::send_async(Mailbox &dest, void *data, int simulatedSize) {
136 s4u::Comm &res = s4u::Comm::send_init(dest);
137 res.setRemains(simulatedSize);
139 res.srcBuffSize_ = sizeof(void*);
144 s4u::Comm &Comm::recv_async(Mailbox &dest, void **data) {
145 s4u::Comm &res = s4u::Comm::recv_init(dest);
146 res.setDstData(data);
152 xbt_assert(state_ == inited || state_ == started || state_ == finished);
154 if (state_ == finished)
155 xbt_die("Don't call test on a finished comm.");
157 if (state_ == inited) {
161 if(simcall_comm_test(pimpl_)){