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");
13 using namespace simgrid;
19 s4u::Comm &s4u::Comm::send_init(s4u::Mailbox &chan) {
20 s4u::Comm *res = new s4u::Comm();
21 res->sender_ = SIMIX_process_self();
22 res->mailbox_ = &chan;
26 s4u::Comm &s4u::Comm::recv_init(s4u::Mailbox &chan) {
27 s4u::Comm *res = new s4u::Comm();
28 res->receiver_ = SIMIX_process_self();
29 res->mailbox_ = &chan;
33 void s4u::Comm::setRate(double rate) {
34 xbt_assert(state_==inited);
38 void s4u::Comm::setSrcData(void * buff) {
39 xbt_assert(state_==inited);
40 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
43 void s4u::Comm::setSrcDataSize(size_t size){
44 xbt_assert(state_==inited);
47 void s4u::Comm::setSrcData(void * buff, size_t size) {
48 xbt_assert(state_==inited);
50 xbt_assert(dstBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
54 void s4u::Comm::setDstData(void ** buff) {
55 xbt_assert(state_==inited);
56 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
59 size_t s4u::Comm::getDstDataSize(){
60 xbt_assert(state_==finished);
63 void s4u::Comm::setDstData(void ** buff, size_t size) {
64 xbt_assert(state_==inited);
66 xbt_assert(srcBuff_ == nullptr, "Cannot set the src and dst buffers at the same time");
71 void s4u::Comm::start() {
72 xbt_assert(state_ == inited);
74 if (srcBuff_ != nullptr) { // Sender side
75 pimpl_ = simcall_comm_isend(sender_, mailbox_->getInferior(), remains_, rate_,
76 srcBuff_, srcBuffSize_,
77 matchFunction_, cleanFunction_, copyDataFunction_,
78 userData_, detached_);
79 } else if (dstBuff_ != nullptr) { // Receiver side
80 pimpl_ = simcall_comm_irecv(receiver_, mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
81 matchFunction_, copyDataFunction_,
85 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
89 void s4u::Comm::wait() {
90 xbt_assert(state_ == started || state_ == inited);
92 if (state_ == started)
93 simcall_comm_wait(pimpl_, -1/*timeout*/);
94 else {// p_state == inited. Save a simcall and do directly a blocking send/recv
95 if (srcBuff_ != nullptr) {
96 simcall_comm_send(sender_, mailbox_->getInferior(), remains_, rate_,
97 srcBuff_, srcBuffSize_,
98 matchFunction_, copyDataFunction_,
99 userData_, -1 /*timeout*/);
101 simcall_comm_recv(receiver_, mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
102 matchFunction_, copyDataFunction_,
103 userData_, -1/*timeout*/, rate_);
108 void s4u::Comm::wait(double timeout) {
109 xbt_assert(state_ == started || state_ == inited);
111 if (state_ == started) {
112 simcall_comm_wait(pimpl_, timeout);
117 // It's not started yet. Do it in one simcall
118 if (srcBuff_ != nullptr) {
119 simcall_comm_send(sender_, mailbox_->getInferior(), remains_, rate_,
120 srcBuff_, srcBuffSize_,
121 matchFunction_, copyDataFunction_,
124 simcall_comm_recv(receiver_, mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
125 matchFunction_, copyDataFunction_,
126 userData_, timeout, rate_);
131 s4u::Comm &s4u::Comm::send_async(Mailbox &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 &s4u::Comm::recv_async(Mailbox &dest, void **data) {
141 s4u::Comm &res = s4u::Comm::recv_init(dest);
142 res.setDstData(data);