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_async,"S4U asynchronous communications");
13 using namespace simgrid;
19 s4u::Comm &s4u::Comm::send_init(s4u::Actor *sender, s4u::Mailbox &chan) {
20 s4u::Comm *res = new s4u::Comm();
21 res->sender_ = sender;
22 res->mailbox_ = &chan;
26 s4u::Comm &s4u::Comm::recv_init(s4u::Actor *receiver, s4u::Mailbox &chan) {
27 s4u::Comm *res = new s4u::Comm();
28 res->receiver_ = receiver;
29 res->mailbox_ = &chan;
34 void s4u::Comm::setRate(double rate) {
35 xbt_assert(p_state==inited);
39 void s4u::Comm::setSrcData(void * buff) {
40 xbt_assert(p_state==inited);
41 xbt_assert(dstBuff_ == NULL, "Cannot set the src and dst buffers at the same time");
44 void s4u::Comm::setSrcDataSize(size_t size){
45 xbt_assert(p_state==inited);
48 void s4u::Comm::setSrcData(void * buff, size_t size) {
49 xbt_assert(p_state==inited);
51 xbt_assert(dstBuff_ == NULL, "Cannot set the src and dst buffers at the same time");
55 void s4u::Comm::setDstData(void ** buff) {
56 xbt_assert(p_state==inited);
57 xbt_assert(srcBuff_ == NULL, "Cannot set the src and dst buffers at the same time");
60 size_t s4u::Comm::getDstDataSize(){
61 xbt_assert(p_state==finished);
64 void s4u::Comm::setDstData(void ** buff, size_t size) {
65 xbt_assert(p_state==inited);
67 xbt_assert(srcBuff_ == NULL, "Cannot set the src and dst buffers at the same time");
72 void s4u::Comm::start() {
73 xbt_assert(p_state == inited);
75 if (srcBuff_ != NULL) { // Sender side
76 p_inferior = simcall_comm_isend(sender_->getInferior(), mailbox_->getInferior(), p_remains, rate_,
77 srcBuff_, srcBuffSize_,
78 matchFunction_, cleanFunction_, copyDataFunction_,
79 p_userData, detached_);
80 } else if (dstBuff_ != NULL) { // Receiver side
81 p_inferior = simcall_comm_irecv(receiver_->getInferior(), mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
82 matchFunction_, copyDataFunction_,
86 xbt_die("Cannot start a communication before specifying whether we are the sender or the receiver");
90 void s4u::Comm::wait() {
91 xbt_assert(p_state == started || p_state == inited);
93 if (p_state == started)
94 simcall_comm_wait(p_inferior, -1/*timeout*/);
95 else {// p_state == inited. Save a simcall and do directly a blocking send/recv
96 if (srcBuff_ != NULL) {
97 simcall_comm_send(sender_->getInferior(), mailbox_->getInferior(), p_remains, rate_,
98 srcBuff_, srcBuffSize_,
99 matchFunction_, copyDataFunction_,
100 p_userData, -1 /*timeout*/);
102 simcall_comm_recv(receiver_->getInferior(), mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
103 matchFunction_, copyDataFunction_,
104 p_userData, -1/*timeout*/, rate_);
109 void s4u::Comm::wait(double timeout) {
110 xbt_assert(p_state == started || p_state == inited);
112 if (p_state == started) {
113 simcall_comm_wait(p_inferior, timeout);
118 // It's not started yet. Do it in one simcall
119 if (srcBuff_ != NULL) {
120 simcall_comm_send(sender_->getInferior(), mailbox_->getInferior(), p_remains, rate_,
121 srcBuff_, srcBuffSize_,
122 matchFunction_, copyDataFunction_,
123 p_userData, timeout);
125 simcall_comm_recv(receiver_->getInferior(), mailbox_->getInferior(), dstBuff_, &dstBuffSize_,
126 matchFunction_, copyDataFunction_,
127 p_userData, timeout, rate_);
132 s4u::Comm &s4u::Comm::send_async(s4u::Actor *sender, Mailbox &dest, void *data, int simulatedSize) {
133 s4u::Comm &res = s4u::Comm::send_init(sender, dest);
135 res.setRemains(simulatedSize);
137 res.srcBuffSize_ = sizeof(void*);
143 s4u::Comm &s4u::Comm::recv_async(s4u::Actor *receiver, Mailbox &dest, void **data) {
144 s4u::Comm &res = s4u::Comm::recv_init(receiver, dest);
146 res.setDstData(data);