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>
16 /** @brief Communication async
18 * Represents all asynchronous communications, that you can test or wait onto.
20 XBT_PUBLIC_CLASS Comm : public Activity
22 Comm() : Activity() {}
24 friend void intrusive_ptr_release(simgrid::s4u::Comm * c);
25 friend void intrusive_ptr_add_ref(simgrid::s4u::Comm * c);
29 /*! take a range of s4u::CommPtr (last excluded) and return when one of them is finished. The return value is an
30 * iterator on the finished Comms. */
31 template <class I> static I wait_any(I first, I last)
33 // Map to dynar<Synchro*>:
34 xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), [](void*ptr){
35 intrusive_ptr_release((simgrid::kernel::activity::ActivityImpl*)ptr);
37 for (I iter = first; iter != last; iter++) {
39 if (comm->state_ == inited)
41 xbt_assert(comm->state_ == started);
42 simgrid::kernel::activity::ActivityImpl* ptr = comm->pimpl_.get();
43 intrusive_ptr_add_ref(ptr);
44 xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, ptr);
46 // Call the underlying simcall:
47 int idx = simcall_comm_waitany(comms, -1);
48 xbt_dynar_free(&comms);
52 // Lift the index to the corresponding iterator:
53 auto res = std::next(first, idx);
54 (*res)->state_ = finished;
57 /*! Same as wait_any, but with a timeout. If the timeout occurs, parameter last is returned.*/
58 template <class I> static I wait_any_for(I first, I last, double timeout)
60 // Map to dynar<Synchro*>:
61 xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), [](void*ptr){
62 intrusive_ptr_release((simgrid::kernel::activity::ActivityImpl*)ptr);
64 for (I iter = first; iter != last; iter++) {
66 if (comm->state_ == inited)
68 xbt_assert(comm->state_ == started);
69 simgrid::kernel::activity::ActivityImpl* ptr = comm->pimpl_.get();
70 intrusive_ptr_add_ref(ptr);
71 xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, ptr);
73 // Call the underlying simcall:
74 int idx = simcall_comm_waitany(comms, timeout);
75 xbt_dynar_free(&comms);
79 // Lift the index to the corresponding iterator:
80 auto res = std::next(first, idx);
81 (*res)->state_ = finished;
84 /** Creates (but don't start) an async send to the mailbox @p dest */
85 static CommPtr send_init(MailboxPtr dest);
86 /** Creates and start an async send to the mailbox @p dest */
87 static CommPtr send_async(MailboxPtr dest, void* data, int simulatedByteAmount);
88 /** Creates (but don't start) an async recv onto the mailbox @p from */
89 static CommPtr recv_init(MailboxPtr from);
90 /** Creates and start an async recv to the mailbox @p from */
91 static CommPtr recv_async(MailboxPtr from, void** data);
92 /** Creates and start a detached send to the mailbox @p dest
93 * TODO: make it possible to detach an already created comm */
94 static void send_detached(MailboxPtr dest, void* data, int simulatedSize);
96 void start() override;
98 void wait(double timeout) override;
100 /** Sets the maximal communication rate (in byte/sec). Must be done before start */
101 void setRate(double rate);
103 /** Specify the data to send */
104 void setSrcData(void* buff);
105 /** Specify the size of the data to send */
106 void setSrcDataSize(size_t size);
107 /** Specify the data to send and its size */
108 void setSrcData(void* buff, size_t size);
110 /** Specify where to receive the data */
111 void setDstData(void** buff);
112 /** Specify the buffer in which the data should be received */
113 void setDstData(void** buff, size_t size);
114 /** Retrieve the size of the received data */
115 size_t getDstDataSize();
122 void* dstBuff_ = nullptr;
123 size_t dstBuffSize_ = 0;
124 void* srcBuff_ = nullptr;
125 size_t srcBuffSize_ = sizeof(void*);
127 /* FIXME: expose these elements in the API */
129 int (*matchFunction_)(void*, void*, smx_activity_t) = nullptr;
130 void (*cleanFunction_)(void*) = nullptr;
131 void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
133 smx_actor_t sender_ = nullptr;
134 smx_actor_t receiver_ = nullptr;
135 MailboxPtr mailbox_ = nullptr;
137 std::atomic_int_fast32_t refcount_{0};
140 } // namespace simgrid::s4u
142 #endif /* SIMGRID_S4U_COMM_HPP */