Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
attempt to get rid of all const_cast (take 2)
[simgrid.git] / include / simgrid / s4u / Comm.hpp
1 /* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #ifndef SIMGRID_S4U_COMM_HPP
7 #define SIMGRID_S4U_COMM_HPP
8
9 #include <xbt/base.h>
10
11 #include <simgrid/forward.h>
12 #include <simgrid/s4u/Activity.hpp>
13 #include <simgrid/s4u/forward.hpp>
14
15 namespace simgrid {
16 namespace s4u {
17
18 /** @brief Communication async
19  *
20  * Represents all asynchronous communications, that you can test or wait onto.
21  */
22 XBT_PUBLIC_CLASS Comm : public Activity
23 {
24   Comm() : Activity() {}
25 public:
26   ~Comm() override;
27
28   /*! take a range of s4u::Comm* (last excluded) and return when one of them is finished. The return value is an
29    * iterator on the finished Comms. */
30   template <class I> static I wait_any(I first, I last)
31   {
32     // Map to dynar<Synchro*>:
33     xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), NULL);
34     for (I iter = first; iter != last; iter++) {
35       Comm& comm = **iter;
36       if (comm.state_ == inited)
37         comm.start();
38       xbt_assert(comm.state_ == started);
39       xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, comm.pimpl_);
40     }
41     // Call the underlying simcall:
42     int idx = simcall_comm_waitany(comms, -1);
43     xbt_dynar_free(&comms);
44     // Not found:
45     if (idx == -1)
46       return last;
47     // Lift the index to the corresponding iterator:
48     auto res       = std::next(first, idx);
49     (*res)->state_ = finished;
50     return res;
51   }
52   /*! Same as wait_any, but with a timeout. If wait_any_for return because of the timeout last is returned.*/
53   template <class I> static I wait_any_for(I first, I last, double timeout)
54   {
55     // Map to dynar<Synchro*>:
56     xbt_dynar_t comms = xbt_dynar_new(sizeof(simgrid::kernel::activity::ActivityImpl*), NULL);
57     for (I iter = first; iter != last; iter++) {
58       Comm& comm = **iter;
59       if (comm.state_ == inited)
60         comm.start();
61       xbt_assert(comm.state_ == started);
62       xbt_dynar_push_as(comms, simgrid::kernel::activity::ActivityImpl*, comm.pimpl_);
63     }
64     // Call the underlying simcall:
65     int idx = simcall_comm_waitany(comms, timeout);
66     xbt_dynar_free(&comms);
67     // Not found:
68     if (idx == -1)
69       return last;
70     // Lift the index to the corresponding iterator:
71     auto res       = std::next(first, idx);
72     (*res)->state_ = finished;
73     return res;
74   }
75   /** Creates (but don't start) an async send to the mailbox @p dest */
76   static Comm& send_init(MailboxPtr dest);
77   /** Creates and start an async send to the mailbox @p dest */
78   static Comm& send_async(MailboxPtr dest, void* data, int simulatedByteAmount);
79   /** Creates (but don't start) an async recv onto the mailbox @p from */
80   static Comm& recv_init(MailboxPtr from);
81   /** Creates and start an async recv to the mailbox @p from */
82   static Comm& recv_async(MailboxPtr from, void** data);
83
84   void start() override;
85   void wait() override;
86   void wait(double timeout) override;
87
88   /** Sets the maximal communication rate (in byte/sec). Must be done before start */
89   void setRate(double rate);
90
91   /** Specify the data to send */
92   void setSrcData(void* buff);
93   /** Specify the size of the data to send */
94   void setSrcDataSize(size_t size);
95   /** Specify the data to send and its size */
96   void setSrcData(void* buff, size_t size);
97
98   /** Specify where to receive the data */
99   void setDstData(void** buff);
100   /** Specify the buffer in which the data should be received */
101   void setDstData(void** buff, size_t size);
102   /** Retrieve the size of the received data */
103   size_t getDstDataSize();
104
105   bool test();
106
107 private:
108   double rate_        = -1;
109   void* dstBuff_      = nullptr;
110   size_t dstBuffSize_ = 0;
111   void* srcBuff_      = nullptr;
112   size_t srcBuffSize_ = sizeof(void*);
113
114   /* FIXME: expose these elements in the API */
115   int detached_ = 0;
116   int (*matchFunction_)(void*, void*, smx_activity_t) = nullptr;
117   void (*cleanFunction_)(void*) = nullptr;
118   void (*copyDataFunction_)(smx_activity_t, void*, size_t) = nullptr;
119
120   smx_actor_t sender_   = nullptr;
121   smx_actor_t receiver_ = nullptr;
122   MailboxPtr mailbox_   = nullptr;
123 };
124 }
125 } // namespace simgrid::s4u
126
127 #endif /* SIMGRID_S4U_COMM_HPP */