Logo AND Algorithmique Numérique Distribuée

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