Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d37e756f1f6f07baec547041ecabaae0e969bff8
[simgrid.git] / src / kernel / activity / MailboxImpl.hpp
1 /* Copyright (c) 2007-2023. 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_MAILBOX_HPP
7 #define SIMGRID_KERNEL_ACTIVITY_MAILBOX_HPP
8
9 #include "simgrid/s4u/Engine.hpp"
10 #include "simgrid/s4u/Mailbox.hpp"
11 #include "src/kernel/activity/CommImpl.hpp"
12 #include "src/kernel/actor/ActorImpl.hpp"
13
14 namespace simgrid::kernel::activity {
15
16 /** @brief Implementation of the s4u::Mailbox */
17
18 class MailboxImpl {
19   s4u::Mailbox piface_;
20   std::string name_;
21   actor::ActorImplPtr permanent_receiver_; // actor to which the mailbox is attached
22   std::deque<CommImplPtr> comm_queue_;
23   // messages already received in the permanent receive mode
24   std::deque<CommImplPtr> done_comm_queue_;
25
26   friend s4u::Engine;
27   friend s4u::Mailbox;
28   friend s4u::Mailbox* s4u::Engine::mailbox_by_name_or_create(const std::string& name) const;
29   friend s4u::Mailbox* s4u::Mailbox::by_name(const std::string& name);
30
31   static unsigned next_id_; // Next ID to be given
32   const unsigned id_ = next_id_++;
33   explicit MailboxImpl(const std::string& name) : piface_(this), name_(name) {}
34   MailboxImpl(const MailboxImpl&) = delete;
35   MailboxImpl& operator=(const MailboxImpl&) = delete;
36
37 public:
38   /** @brief Public interface */
39   unsigned get_id() const { return id_; }
40
41   ~MailboxImpl();
42
43   const s4u::Mailbox* get_iface() const { return &piface_; }
44   s4u::Mailbox* get_iface() { return &piface_; }
45
46   const std::string& get_name() const { return name_; }
47   const char* get_cname() const { return name_.c_str(); }
48   void set_receiver(s4u::ActorPtr actor);
49   void push(const CommImplPtr& comm);
50   void push_done(const CommImplPtr& done_comm) { done_comm_queue_.push_back(done_comm); }
51   void remove(const CommImplPtr& comm);
52   void clear(bool do_finish);
53   CommImplPtr iprobe(int type, const std::function<bool(void*, void*, CommImpl*)>& match_fun, void* data);
54   CommImplPtr find_matching_comm(CommImplType type, const std::function<bool(void*, void*, CommImpl*)>& match_fun,
55                                  void* this_user_data, const CommImplPtr& my_synchro, bool done, bool remove_matching);
56   bool is_permanent() const { return permanent_receiver_ != nullptr; }
57   actor::ActorImplPtr get_permanent_receiver() const { return permanent_receiver_; }
58   bool empty() const { return comm_queue_.empty(); }
59   size_t size() const { return comm_queue_.size(); }
60   const CommImplPtr& front() const { return comm_queue_.front(); }
61   bool has_some_done_comm() const { return not done_comm_queue_.empty(); }
62   const CommImplPtr& done_front() const { return done_comm_queue_.front(); }
63 };
64 } // namespace simgrid::kernel::activity
65
66 #endif