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