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/forward.h>
12 #include <simgrid/s4u/Activity.hpp>
13 #include <simgrid/s4u/forward.hpp>
17 /** @brief Communication async
19 * Represents all asynchronous communications, that you can test or wait onto.
21 XBT_PUBLIC_CLASS Comm : public Activity
23 Comm() : Activity() {}
25 friend void intrusive_ptr_release(simgrid::s4u::Comm * c);
26 friend void intrusive_ptr_add_ref(simgrid::s4u::Comm * c);
30 /*! take a range of s4u::Comm* (last excluded) and return when one of them is finished. The return value is an
31 * iterator on the finished Comms. */
32 template <class I> static I wait_any(I first, I last)
34 // Map to dynar<Synchro*>:
35 xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), NULL);
36 for (I iter = first; iter != last; iter++) {
38 if (comm.state_ == inited)
40 xbt_assert(comm.state_ == started);
41 xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, comm.pimpl_);
43 // Call the underlying simcall:
44 int idx = simcall_comm_waitany(comms, -1);
45 xbt_dynar_free(&comms);
49 // Lift the index to the corresponding iterator:
50 auto res = std::next(first, idx);
51 (*res)->state_ = finished;
54 /*! Same as wait_any, but with a timeout. If wait_any_for return because of the timeout last is returned.*/
55 template <class I> static I wait_any_for(I first, I last, double timeout)
57 // Map to dynar<Synchro*>:
58 xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), NULL);
59 for (I iter = first; iter != last; iter++) {
61 if (comm.state_ == inited)
63 xbt_assert(comm.state_ == started);
64 xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, comm.pimpl_);
66 // Call the underlying simcall:
67 int idx = simcall_comm_waitany(comms, timeout);
68 xbt_dynar_free(&comms);
72 // Lift the index to the corresponding iterator:
73 auto res = std::next(first, idx);
74 (*res)->state_ = finished;
77 /** Creates (but don't start) an async send to the mailbox @p dest */
78 static CommPtr send_init(MailboxPtr dest);
79 /** Creates and start an async send to the mailbox @p dest */
80 static CommPtr send_async(MailboxPtr dest, void* data, int simulatedByteAmount);
81 /** Creates (but don't start) an async recv onto the mailbox @p from */
82 static CommPtr recv_init(MailboxPtr from);
83 /** Creates and start an async recv to the mailbox @p from */
84 static CommPtr recv_async(MailboxPtr from, void** data);
85 /** Creates and start a detached send to the mailbox @p dest
86 * TODO: make it possible to detach an already created comm */
87 static void send_detached(MailboxPtr dest, void* data, int simulatedSize);
89 void start() override;
91 void wait(double timeout) override;
93 /** Sets the maximal communication rate (in byte/sec). Must be done before start */
94 void setRate(double rate);
96 /** Specify the data to send */
97 void setSrcData(void* buff);
98 /** Specify the size of the data to send */
99 void setSrcDataSize(size_t size);
100 /** Specify the data to send and its size */
101 void setSrcData(void* buff, size_t size);
103 /** Specify where to receive the data */
104 void setDstData(void** buff);
105 /** Specify the buffer in which the data should be received */
106 void setDstData(void** buff, size_t size);
107 /** Retrieve the size of the received data */
108 size_t getDstDataSize();
115 void* dstBuff_ = nullptr;
116 size_t dstBuffSize_ = 0;
117 void* srcBuff_ = nullptr;
118 size_t srcBuffSize_ = sizeof(void*);
120 /* FIXME: expose these elements in the API */
122 int (*matchFunction_)(void*, void*, smx_activity_t) = nullptr;
123 void (*cleanFunction_)(void*) = nullptr;
124 void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
126 smx_actor_t sender_ = nullptr;
127 smx_actor_t receiver_ = nullptr;
128 MailboxPtr mailbox_ = nullptr;
130 std::atomic_int_fast32_t refcount_{0};
133 } // namespace simgrid::s4u
135 #endif /* SIMGRID_S4U_COMM_HPP */