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 "msg/msg_private.h"
9 #include "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->p_sender = sender;
23 res->p_mailbox = &chan;
27 s4u::Comm &s4u::Comm::recv_init(s4u::Mailbox &chan) {
28 s4u::Comm *res = new s4u::Comm();
29 res->p_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(p_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(p_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(p_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(p_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 (p_srcBuff != NULL) { // Sender side
76 p_inferior = simcall_comm_isend(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
77 p_srcBuff, p_srcBuffSize,
78 p_matchFunction, p_cleanFunction, p_copyDataFunction,
79 p_userData, p_detached);
80 } else if (p_dstBuff != NULL) { // Receiver side
81 p_inferior = simcall_comm_irecv(p_mailbox->getInferior(), p_dstBuff, &p_dstBuffSize,
82 p_matchFunction, p_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 (p_srcBuff != NULL) {
97 simcall_comm_send(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
98 p_srcBuff, p_srcBuffSize,
99 p_matchFunction, p_copyDataFunction,
100 p_userData, -1 /*timeout*/);
102 simcall_comm_recv(p_mailbox->getInferior(), p_dstBuff, &p_dstBuffSize,
103 p_matchFunction, p_copyDataFunction,
104 p_userData, -1/*timeout*/, p_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 (p_srcBuff != NULL) {
120 simcall_comm_send(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
121 p_srcBuff, p_srcBuffSize,
122 p_matchFunction, p_copyDataFunction,
123 p_userData, timeout);
125 simcall_comm_recv(p_mailbox->getInferior(), p_dstBuff, &p_dstBuffSize,
126 p_matchFunction, p_copyDataFunction,
127 p_userData, timeout, p_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);
136 res.p_srcBuff = data;
137 res.p_srcBuffSize = sizeof(void*);