Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4ef20b7a94dc4ffb0d1cdd2572021d1a1b5a89de
[simgrid.git] / src / s4u / s4u_comm.cpp
1 /* Copyright (c) 2006-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "xbt/log.h"
8 #include "msg/msg_private.h"
9 #include "msg/msg_mailbox.h"
10
11 #include "simgrid/s4u/comm.hpp"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_comm,s4u_async,"S4U asynchronous communications");
14 using namespace simgrid;
15
16 s4u::Comm::~Comm() {
17
18 }
19
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;
24
25         return *res;
26 }
27
28 void s4u::Comm::setRate(double rate) {
29         xbt_assert(p_state==inited);
30         p_rate = rate;
31 }
32
33 void s4u::Comm::setSrcData(void * buff) {
34         xbt_assert(p_state==inited);
35         p_srcBuff = buff;
36 }
37 void s4u::Comm::setSrcDataSize(size_t size){
38         xbt_assert(p_state==inited);
39         p_srcBuffSize = size;
40 }
41 void s4u::Comm::setSrcData(void * buff, size_t size) {
42         xbt_assert(p_state==inited);
43
44         p_srcBuff = buff;
45         p_srcBuffSize = size;
46 }
47
48 void s4u::Comm::start() {
49         xbt_assert(p_state == inited);
50
51         p_inferior = simcall_comm_isend(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
52                         p_srcBuff, p_srcBuffSize,
53                         p_matchFunction, p_cleanFunction, p_copyDataFunction,
54                         p_userData, p_detached);
55         p_state = started;
56 }
57 void s4u::Comm::wait() {
58         xbt_assert(p_state == started || p_state == inited);
59
60         if (p_state == started)
61                 simcall_comm_wait(p_inferior, -1/*timeout*/);
62         else // p_state == inited
63                 /* Save a simcall and do directly a blocking send */
64                 simcall_comm_send(p_sender->getInferior(), p_mailbox->getInferior(), p_remains, p_rate,
65                                 p_srcBuff, p_srcBuffSize,
66                                 p_matchFunction, p_copyDataFunction,
67                                 p_userData, p_detached);
68         p_state = finished;
69 }
70 void s4u::Comm::wait(double timeout) {
71         xbt_assert(p_state == started || p_state == inited);
72
73         if (p_state == inited)
74                 start();
75         simcall_comm_wait(p_inferior, timeout);
76 }
77
78 s4u::Comm &s4u::Comm::send_async(s4u::Actor *sender, Mailbox &dest, void *data, int simulatedSize) {
79         s4u::Comm &res = s4u::Comm::send_init(sender, dest);
80
81         res.setRemains(simulatedSize);
82         res.p_srcBuff = data;
83         res.p_srcBuffSize = sizeof(void*);
84
85         res.start();
86         return res;
87 }
88