Logo AND Algorithmique Numérique Distribuée

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