Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge s4u wait_any
[simgrid.git] / src / s4u / s4u_mailbox.cpp
1 /* Copyright (c) 2006-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/log.h"
8 #include "src/msg/msg_private.h"
9 #include "src/simix/smx_network_private.h"
10 #include "src/simix/smx_process_private.h"
11
12 #include "simgrid/s4u/mailbox.hpp"
13
14 XBT_LOG_EXTERNAL_CATEGORY(s4u);
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_channel,s4u,"S4U Communication Mailboxes");
16
17 namespace simgrid {
18 namespace s4u {
19
20 const char *Mailbox::getName() {
21   return pimpl_->name;
22 }
23
24 MailboxPtr Mailbox::byName(const char*name) {
25   // FIXME: there is a race condition here where two actors run Mailbox::byName
26   // on a non-existent mailbox during the same scheduling round. Both will be
27   // interrupted in the simcall creating the underlying simix mbox.
28   // Only one simix object will be created, but two S4U objects will be created.
29   // Only one S4U object will be stored in the hashmap and used, and the other
30   // one will be leaked.
31   smx_mailbox_t mbox = simcall_mbox_get_by_name(name);
32   if (mbox == nullptr)
33     mbox = simcall_mbox_create(name);
34   return MailboxPtr(&mbox->mbox_, true);
35 }
36
37 bool Mailbox::empty() {
38   return nullptr == simcall_mbox_front(pimpl_);
39 }
40
41 void Mailbox::setReceiver(Actor* actor) {
42   simcall_mbox_set_receiver(pimpl_, actor == nullptr ? nullptr : actor->pimpl_);
43 }
44
45 /** @brief get the receiver (process associated to the mailbox) */
46 ActorPtr Mailbox::receiver() {
47   if(pimpl_->permanent_receiver == nullptr) return ActorPtr();
48   return ActorPtr(&pimpl_->permanent_receiver->actor());
49 }
50
51 }
52 }
53
54 /*------- C functions -------*/
55
56 sg_mbox_t sg_mbox_by_name(const char*name){
57   return simgrid::s4u::Mailbox::byName(name).get();
58 }
59 int sg_mbox_is_empty(sg_mbox_t mbox) {
60   return mbox->empty();
61 }
62 void sg_mbox_setReceiver(sg_mbox_t mbox, smx_process_t process) {
63   mbox->setReceiver(&process->actor());
64 }
65 smx_process_t sg_mbox_receiver(sg_mbox_t mbox) {
66   return mbox->receiver()->getInferior();
67 }