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->p_sender = sender;
23 res->p_mailbox = &chan;
27 s4u::Comm &s4u::Comm::recv_init(s4u::Actor *receiver, s4u::Mailbox &chan) {
28 s4u::Comm *res = new s4u::Comm();
29 res->p_receiver = receiver;
30 res->p_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(p_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(p_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(p_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(p_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 (p_srcBuff != NULL) { // Sender side
77 p_inferior = simcall_comm_isend(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
78 p_srcBuff, p_srcBuffSize,
79 p_matchFunction, p_cleanFunction, p_copyDataFunction,
80 p_userData, p_detached);
81 } else if (p_dstBuff != NULL) { // Receiver side
82 p_inferior = simcall_comm_irecv(p_receiver->getInferior(), p_mailbox->getInferior(), p_dstBuff, &p_dstBuffSize,
83 p_matchFunction, p_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 (p_srcBuff != NULL) {
98 simcall_comm_send(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
99 p_srcBuff, p_srcBuffSize,
100 p_matchFunction, p_copyDataFunction,
101 p_userData, -1 /*timeout*/);
103 simcall_comm_recv(p_receiver->getInferior(), p_mailbox->getInferior(), p_dstBuff, &p_dstBuffSize,
104 p_matchFunction, p_copyDataFunction,
105 p_userData, -1/*timeout*/, p_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 (p_srcBuff != NULL) {
121 simcall_comm_send(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
122 p_srcBuff, p_srcBuffSize,
123 p_matchFunction, p_copyDataFunction,
124 p_userData, timeout);
126 simcall_comm_recv(p_receiver->getInferior(), p_mailbox->getInferior(), p_dstBuff, &p_dstBuffSize,
127 p_matchFunction, p_copyDataFunction,
128 p_userData, timeout, p_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);
137 res.p_srcBuff = data;
138 res.p_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);