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