1 /* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_S4U_COMM_HPP
7 #define SIMGRID_S4U_COMM_HPP
11 #include <simgrid/s4u/Activity.hpp>
12 #include <simgrid/s4u/forward.hpp>
13 #include <simgrid/forward.h>
20 /** @brief Communication async
22 * Represents all asynchronous communications, that you can test or wait onto.
24 XBT_PUBLIC_CLASS Comm : public Activity {
25 Comm() : Activity() {}
31 /*! take a range of s4u::Comm* (last excluded) and return when one of them is finished. The return value is an iterator on the finished Comms. */
32 template<class I> static
33 I wait_any(I first, I last)
35 // Map to dynar<Synchro*>:
36 xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), NULL);
37 for(I iter = first; iter != last; iter++) {
39 if (comm.state_ == inited)
41 xbt_assert(comm.state_ == started);
42 xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, comm.pimpl_);
44 // Call the underlying simcall:
45 int idx = simcall_comm_waitany(comms, -1);
46 xbt_dynar_free(&comms);
50 // Lift the index to the corresponding iterator:
51 auto res = std::next(first, idx);
52 (*res)->state_ = finished;
55 /*! Same as wait_any, but with a timeout. If wait_any_for return because of the timeout last is returned.*/
56 template<class I> static
57 I wait_any_for(I first, I last, double timeout)
59 // Map to dynar<Synchro*>:
60 xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), NULL);
61 for(I iter = first; iter != last; iter++) {
63 if (comm.state_ == inited)
65 xbt_assert(comm.state_ == started);
66 xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, comm.pimpl_);
68 // Call the underlying simcall:
69 int idx = simcall_comm_waitany(comms, timeout);
70 xbt_dynar_free(&comms);
74 // Lift the index to the corresponding iterator:
75 auto res = std::next(first, idx);
76 (*res)->state_ = finished;
79 /** Creates (but don't start) an async send to the mailbox @p dest */
80 static Comm &send_init(MailboxPtr dest);
81 /** Creates and start an async send to the mailbox @p dest */
82 static Comm &send_async(MailboxPtr dest, void *data, int simulatedByteAmount);
83 /** Creates (but don't start) an async recv onto the mailbox @p from */
84 static Comm &recv_init(MailboxPtr from);
85 /** Creates and start an async recv to the mailbox @p from */
86 static Comm &recv_async(MailboxPtr from, void **data);
88 void start() override;
90 void wait(double timeout) override;
92 /** Sets the maximal communication rate (in byte/sec). Must be done before start */
93 void setRate(double rate);
95 /** Specify the data to send */
96 void setSrcData(void * buff);
97 /** Specify the size of the data to send */
98 void setSrcDataSize(size_t size);
99 /** Specify the data to send and its size */
100 void setSrcData(void * buff, size_t size);
102 /** Specify where to receive the data */
103 void setDstData(void ** buff);
104 /** Specify the buffer in which the data should be received */
105 void setDstData(void ** buff, size_t size);
106 /** Retrieve the size of the received data */
107 size_t getDstDataSize();
114 void *dstBuff_ = nullptr;
115 size_t dstBuffSize_ = 0;
116 void *srcBuff_ = nullptr;
117 size_t srcBuffSize_ = sizeof(void*);
119 /* FIXME: expose these elements in the API */
121 int (*matchFunction_)(void *, void *, smx_activity_t) = nullptr;
122 void (*cleanFunction_)(void *) = nullptr;
123 void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
125 smx_actor_t sender_ = nullptr;
126 smx_actor_t receiver_ = nullptr;
127 MailboxPtr mailbox_ = nullptr;
130 }} // namespace simgrid::s4u
132 #endif /* SIMGRID_S4U_COMM_HPP */