1 /* Copyright (c) 2006-2017. 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 #include "simgrid/s4u/Comm.hpp"
7 #include "simgrid/s4u/Mailbox.hpp"
8 #include "src/msg/msg_private.h"
9 #include "src/simix/ActorImpl.hpp"
10 #include "src/simix/smx_network_private.h"
13 XBT_LOG_EXTERNAL_CATEGORY(s4u);
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_channel,s4u,"S4U Communication Mailboxes");
19 const char* Mailbox::getName()
24 MailboxPtr Mailbox::byName(const char*name)
26 kernel::activity::MailboxImpl* mbox = kernel::activity::MailboxImpl::byNameOrNull(name);
27 if (mbox == nullptr) {
28 mbox = simix::kernelImmediate([name] {
29 return kernel::activity::MailboxImpl::byNameOrCreate(name);
32 return MailboxPtr(&mbox->piface_, true);
35 MailboxPtr Mailbox::byName(std::string name)
37 return byName(name.c_str());
42 return pimpl_->comm_queue.empty();
45 bool Mailbox::listen()
47 return not this->empty() || (pimpl_->permanent_receiver && not pimpl_->done_comm_queue.empty());
50 smx_activity_t Mailbox::front()
52 return pimpl_->comm_queue.empty() ? nullptr : pimpl_->comm_queue.front();
55 void Mailbox::setReceiver(ActorPtr actor) {
56 simix::kernelImmediate([this, actor]() { this->pimpl_->setReceiver(actor); });
59 /** @brief get the receiver (process associated to the mailbox) */
60 ActorPtr Mailbox::getReceiver()
62 if (pimpl_->permanent_receiver == nullptr)
64 return pimpl_->permanent_receiver->iface();
67 CommPtr Mailbox::put_init()
69 CommPtr res = CommPtr(new s4u::Comm());
70 res->sender_ = SIMIX_process_self();
74 s4u::CommPtr Mailbox::put_init(void* data, uint64_t simulatedSize)
76 s4u::CommPtr res = put_init();
77 res->setRemains(simulatedSize);
79 res->srcBuffSize_ = sizeof(void*);
82 s4u::CommPtr Mailbox::put_async(void* payload, uint64_t simulatedSize)
84 xbt_assert(payload != nullptr, "You cannot send nullptr");
86 s4u::CommPtr res = put_init(payload, simulatedSize);
90 void Mailbox::put(void* payload, uint64_t simulatedSize)
92 xbt_assert(payload != nullptr, "You cannot send nullptr");
94 CommPtr c = put_init();
95 c->setRemains(simulatedSize);
96 c->setSrcData(payload);
99 /** Blocking send with timeout */
100 void Mailbox::put(void* payload, uint64_t simulatedSize, double timeout)
102 xbt_assert(payload != nullptr, "You cannot send nullptr");
104 CommPtr c = put_init();
105 c->setRemains(simulatedSize);
106 c->setSrcData(payload);
107 // c->start() is optional.
111 s4u::CommPtr Mailbox::get_init()
113 CommPtr res = CommPtr(new s4u::Comm());
114 res->receiver_ = SIMIX_process_self();
115 res->mailbox_ = this;
118 s4u::CommPtr Mailbox::get_async(void** data)
120 s4u::CommPtr res = get_init();
121 res->setDstData(data, sizeof(*data));
129 CommPtr c = get_init();
130 c->setDstData(&res, sizeof(res));
134 void* Mailbox::get(double timeout)
137 CommPtr c = get_init();
138 c->setDstData(&res, sizeof(res));