Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
refactor exception handling for activities
[simgrid.git] / src / kernel / activity / CommImpl.hpp
1 /* Copyright (c) 2007-2021. 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_KERNEL_ACTIVITY_COMM_HPP
7 #define SIMGRID_KERNEL_ACTIVITY_COMM_HPP
8
9 #include "src/kernel/activity/ActivityImpl.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11
12 namespace simgrid {
13 namespace kernel {
14 namespace activity {
15
16 class XBT_PUBLIC CommImpl : public ActivityImpl_T<CommImpl> {
17   ~CommImpl() override;
18   void cleanup_surf();
19
20   static void (*copy_data_callback_)(CommImpl*, void*, size_t);
21
22   double rate_       = 0.0;
23   double size_       = 0.0;
24   bool detached_     = false;   /* If detached or not */
25   bool copied_       = false;   /* whether the data were already copied */
26   MailboxImpl* mbox_ = nullptr; /* Rendez-vous where the comm is queued */
27   s4u::Host* from_   = nullptr; /* Pre-determined only for direct host-to-host communications */
28   s4u::Host* to_     = nullptr; /* Otherwise, computed at start() time from the actors */
29
30 public:
31   enum class Type { SEND, RECEIVE };
32
33   static void set_copy_data_callback(void (*callback)(CommImpl*, void*, size_t));
34
35   explicit CommImpl(Type type) : type_(type) {}
36   CommImpl(s4u::Host* from, s4u::Host* to, double bytes);
37
38   CommImpl& set_size(double size);
39   CommImpl& set_src_buff(unsigned char* buff, size_t size);
40   CommImpl& set_dst_buff(unsigned char* buff, size_t* size);
41   CommImpl& set_rate(double rate);
42   CommImpl& set_mailbox(MailboxImpl* mbox);
43   CommImpl& detach();
44
45   double get_rate() const { return rate_; }
46   MailboxImpl* get_mailbox() const { return mbox_; }
47   bool detached() const { return detached_; }
48
49   std::vector<s4u::Link*> get_traversed_links() const;
50   void copy_data();
51
52   bool test() override;
53   void wait_for(actor::ActorImpl* issuer, double timeout) override;
54   static ssize_t test_any(const actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms);
55   static void wait_any_for(actor::ActorImpl* issuer, const std::vector<CommImpl*>& comms, double timeout);
56
57   CommImpl* start();
58   void suspend() override;
59   void resume() override;
60   void cancel() override;
61   void post() override;
62   void set_exception(actor::ActorImpl* issuer) override;
63   void finish() override;
64
65   const Type type_ = Type::SEND; /* Type of the communication (SEND or RECEIVE) */
66
67 #if SIMGRID_HAVE_MC
68   MailboxImpl* mbox_cpy = nullptr; /* Copy of the rendez-vous where the comm is queued, MC needs it for DPOR
69                                      (comm.mbox set to nullptr when the communication is removed from the mailbox
70                                      (used as garbage collector)) */
71 #endif
72
73   void (*clean_fun)(void*) = nullptr; /* Function to clean the detached src_buf if something goes wrong */
74   bool (*match_fun)(void*, void*, CommImpl*) = nullptr; /* Filter function used by the other side. It is used when
75 looking if a given communication matches my needs. For that, myself must match the
76 expectations of the other side, too. See  */
77   void (*copy_data_fun)(CommImpl*, void*, size_t) = nullptr;
78
79   /* Surf action data */
80   resource::Action* src_timeout_ = nullptr; /* Surf's actions to instrument the timeouts */
81   resource::Action* dst_timeout_ = nullptr; /* Surf's actions to instrument the timeouts */
82   actor::ActorImplPtr src_actor_ = nullptr;
83   actor::ActorImplPtr dst_actor_ = nullptr;
84
85   /* Data to be transferred */
86   unsigned char* src_buff_ = nullptr;
87   unsigned char* dst_buff_ = nullptr;
88   size_t src_buff_size_    = 0;
89   size_t* dst_buff_size_   = nullptr;
90
91   void* src_data_ = nullptr; /* User data associated to the communication */
92   void* dst_data_ = nullptr;
93   static xbt::signal<void(CommImpl const&)> on_start;
94   static xbt::signal<void(CommImpl const&)> on_completion;
95 };
96 } // namespace activity
97 } // namespace kernel
98 } // namespace simgrid
99
100 #endif