Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix scan-build warning (link may be null).
[simgrid.git] / src / s4u / s4u_mailbox.cpp
1 /* Copyright (c) 2006-2018. 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 #include "simgrid/s4u/Comm.hpp"
7 #include "simgrid/s4u/Mailbox.hpp"
8 #include "src/msg/msg_private.hpp"
9 #include "src/simix/ActorImpl.hpp"
10 #include "src/simix/smx_network_private.hpp"
11 #include "xbt/log.h"
12
13 XBT_LOG_EXTERNAL_CATEGORY(s4u);
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_channel,s4u,"S4U Communication Mailboxes");
15
16 namespace simgrid {
17 namespace s4u {
18
19 const simgrid::xbt::string& Mailbox::get_name() const
20 {
21   return pimpl_->get_name();
22 }
23
24 const char* Mailbox::get_cname() const
25 {
26   return pimpl_->get_cname();
27 }
28
29 MailboxPtr Mailbox::byName(const char*name)
30 {
31   kernel::activity::MailboxImpl* mbox = kernel::activity::MailboxImpl::byNameOrNull(name);
32   if (mbox == nullptr) {
33     mbox = simix::kernelImmediate([name] {
34       return kernel::activity::MailboxImpl::byNameOrCreate(name);
35     });
36   }
37   return MailboxPtr(&mbox->piface_, true);
38 }
39
40 MailboxPtr Mailbox::byName(std::string name)
41 {
42   return byName(name.c_str());
43 }
44
45 bool Mailbox::empty()
46 {
47   return pimpl_->comm_queue.empty();
48 }
49
50 bool Mailbox::listen()
51 {
52   return not this->empty() || (pimpl_->permanent_receiver && not pimpl_->done_comm_queue.empty());
53 }
54
55 smx_activity_t Mailbox::front()
56 {
57   return pimpl_->comm_queue.empty() ? nullptr : pimpl_->comm_queue.front();
58 }
59
60 void Mailbox::setReceiver(ActorPtr actor) {
61   simix::kernelImmediate([this, actor]() { this->pimpl_->setReceiver(actor); });
62 }
63
64 /** @brief get the receiver (process associated to the mailbox) */
65 ActorPtr Mailbox::getReceiver()
66 {
67   if (pimpl_->permanent_receiver == nullptr)
68     return ActorPtr();
69   return pimpl_->permanent_receiver->iface();
70 }
71
72 CommPtr Mailbox::put_init()
73 {
74   CommPtr res   = CommPtr(new s4u::Comm());
75   res->sender_  = SIMIX_process_self();
76   res->mailbox_ = this;
77   return res;
78 }
79 s4u::CommPtr Mailbox::put_init(void* data, uint64_t simulatedSize)
80 {
81   s4u::CommPtr res = put_init();
82   res->set_remaining(simulatedSize);
83   res->srcBuff_     = data;
84   res->srcBuffSize_ = sizeof(void*);
85   return res;
86 }
87 s4u::CommPtr Mailbox::put_async(void* payload, uint64_t simulatedSize)
88 {
89   xbt_assert(payload != nullptr, "You cannot send nullptr");
90
91   s4u::CommPtr res = put_init(payload, simulatedSize);
92   res->start();
93   return res;
94 }
95 void Mailbox::put(void* payload, uint64_t simulatedSize)
96 {
97   xbt_assert(payload != nullptr, "You cannot send nullptr");
98
99   CommPtr c = put_init();
100   c->set_remaining(simulatedSize);
101   c->setSrcData(payload);
102   c->wait();
103 }
104 /** Blocking send with timeout */
105 void Mailbox::put(void* payload, uint64_t simulatedSize, double timeout)
106 {
107   xbt_assert(payload != nullptr, "You cannot send nullptr");
108
109   CommPtr c = put_init();
110   c->set_remaining(simulatedSize);
111   c->setSrcData(payload);
112   // c->start() is optional.
113   c->wait(timeout);
114 }
115
116 s4u::CommPtr Mailbox::get_init()
117 {
118   CommPtr res    = CommPtr(new s4u::Comm());
119   res->receiver_ = SIMIX_process_self();
120   res->mailbox_  = this;
121   return res;
122 }
123 s4u::CommPtr Mailbox::get_async(void** data)
124 {
125   s4u::CommPtr res = get_init();
126   res->setDstData(data, sizeof(*data));
127   res->start();
128   return res;
129 }
130
131 void* Mailbox::get()
132 {
133   void* res = nullptr;
134   CommPtr c = get_init();
135   c->setDstData(&res, sizeof(res));
136   c->wait();
137   return res;
138 }
139 void* Mailbox::get(double timeout)
140 {
141   void* res = nullptr;
142   CommPtr c = get_init();
143   c->setDstData(&res, sizeof(res));
144   c->wait(timeout);
145   return res;
146 }
147 }
148 }