Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
27fbaae27f48f9d0bd51dc6f68bd9887b5ad72f9
[simgrid.git] / src / msg / msg_mailbox.cpp
1 /* Mailboxes in MSG */
2
3 /* Copyright (c) 2008-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <xbt/ex.hpp>
10
11 #include "simgrid/msg.h"
12 #include "msg_private.h"
13 #include "simgrid/s4u/mailbox.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_mailbox, msg, "Logging specific to MSG (mailbox)");
16
17 /** \ingroup msg_mailbox_management
18  * \brief Set the mailbox to receive in asynchronous mode
19  *
20  * All messages sent to this mailbox will be transferred to the receiver without waiting for the receive call.
21  * The receive call will still be necessary to use the received data.
22  * If there is a need to receive some messages asynchronously, and some not, two different mailboxes should be used.
23  *
24  * \param alias The name of the mailbox
25  */
26 void MSG_mailbox_set_async(const char *alias){
27   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
28
29   simcall_mbox_set_receiver(mailbox->getImpl(), SIMIX_process_self());
30   XBT_VERB("%s mailbox set to receive eagerly for myself\n",alias);
31 }
32
33 /** \ingroup msg_mailbox_management
34  * \brief Get a task from a mailbox on a given host at a given rate
35  *
36  * \param mailbox The mailbox where the task was sent
37  * \param task a memory location for storing a #msg_task_t.
38  * \param host a #msg_host_t host from where the task was sent
39  * \param timeout a timeout
40  * \param rate a rate
41
42  * \return Returns
43  * #MSG_OK if the task was successfully received,
44  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
45  */
46 msg_error_t MSG_mailbox_get_task_ext_bounded(msg_mailbox_t mailbox, msg_task_t * task, msg_host_t host, double timeout,
47                                              double rate)
48 {
49   msg_error_t ret = MSG_OK;
50   /* We no longer support getting a task from a specific host */
51   if (host)
52     THROW_UNIMPLEMENTED;
53
54   TRACE_msg_task_get_start();
55   double start_time = MSG_get_clock();
56
57   /* Sanity check */
58   xbt_assert(task, "Null pointer for the task storage");
59
60   if (*task)
61     XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
62
63   /* Try to receive it by calling SIMIX network layer */
64   try {
65     simcall_comm_recv(MSG_process_self(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
66     XBT_DEBUG("Got task %s from %s",(*task)->name,mailbox->getName());
67     (*task)->simdata->setNotUsed();
68   }
69   catch (xbt_ex& e) {
70     switch (e.category) {
71     case cancel_error:
72       ret = MSG_HOST_FAILURE;
73       break;
74     case network_error:
75       ret = MSG_TRANSFER_FAILURE;
76       break;
77     case timeout_error:
78       ret = MSG_TIMEOUT;
79       break;
80     case host_error:
81       ret = MSG_HOST_FAILURE;
82       break;
83     default:
84       throw;
85     }
86   }
87
88   if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
89     TRACE_msg_task_get_end(start_time, *task);
90   }
91   MSG_RETURN(ret);
92 }