Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ea4b947e9f5999f92d74d41f28e350082eb0dd12
[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 msg_task_t MSG_mailbox_front(msg_mailbox_t mailbox)
18 {
19   simgrid::kernel::activity::Comm* comm = static_cast<simgrid::kernel::activity::Comm*>(mailbox->front());
20
21   if (!comm)
22     return nullptr;
23
24   return (msg_task_t) comm->src_data;
25 }
26
27 /** \ingroup msg_mailbox_management
28  * \brief Set the mailbox to receive in asynchronous mode
29  *
30  * All messages sent to this mailbox will be transferred to the receiver without waiting for the receive call.
31  * The receive call will still be necessary to use the received data.
32  * If there is a need to receive some messages asynchronously, and some not, two different mailboxes should be used.
33  *
34  * \param alias The name of the mailbox
35  */
36 void MSG_mailbox_set_async(const char *alias){
37   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(alias);
38
39   simcall_mbox_set_receiver(mailbox->getImpl(), SIMIX_process_self());
40   XBT_VERB("%s mailbox set to receive eagerly for myself\n",alias);
41 }
42
43 /** \ingroup msg_mailbox_management
44  * \brief Get a task from a mailbox on a given host
45  *
46  * \param mailbox The mailbox where the task was sent
47  * \param task a memory location for storing a #msg_task_t.
48  * \param host a #msg_host_t host from where the task was sent
49  * \param timeout a timeout
50
51  * \return Returns
52  * #MSG_OK if the task was successfully received,
53  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
54  */
55 msg_error_t MSG_mailbox_get_task_ext(msg_mailbox_t mailbox, msg_task_t *task, msg_host_t host, double timeout)
56 {
57   return MSG_mailbox_get_task_ext_bounded(mailbox, task, host, timeout, -1.0);
58 }
59
60 /** \ingroup msg_mailbox_management
61  * \brief Get a task from a mailbox on a given host at a given rate
62  *
63  * \param mailbox The mailbox where the task was sent
64  * \param task a memory location for storing a #msg_task_t.
65  * \param host a #msg_host_t host from where the task was sent
66  * \param timeout a timeout
67  * \param rate a rate
68
69  * \return Returns
70  * #MSG_OK if the task was successfully received,
71  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
72  */
73 msg_error_t MSG_mailbox_get_task_ext_bounded(msg_mailbox_t mailbox, msg_task_t * task, msg_host_t host, double timeout,
74                                              double rate)
75 {
76   msg_error_t ret = MSG_OK;
77   /* We no longer support getting a task from a specific host */
78   if (host)
79     THROW_UNIMPLEMENTED;
80
81   TRACE_msg_task_get_start();
82   double start_time = MSG_get_clock();
83
84   /* Sanity check */
85   xbt_assert(task, "Null pointer for the task storage");
86
87   if (*task)
88     XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
89
90   /* Try to receive it by calling SIMIX network layer */
91   try {
92     simcall_comm_recv(MSG_process_self(), mailbox->getImpl(), task, nullptr, nullptr, nullptr, nullptr, timeout, rate);
93     XBT_DEBUG("Got task %s from %s",(*task)->name,mailbox->getName());
94     (*task)->simdata->setNotUsed();
95   }
96   catch (xbt_ex& e) {
97     switch (e.category) {
98     case cancel_error:
99       ret = MSG_HOST_FAILURE;
100       break;
101     case network_error:
102       ret = MSG_TRANSFER_FAILURE;
103       break;
104     case timeout_error:
105       ret = MSG_TIMEOUT;
106       break;
107     case host_error:
108       ret = MSG_HOST_FAILURE;
109       break;
110     default:
111       throw;
112     }
113   }
114
115   if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
116     TRACE_msg_task_get_end(start_time, *task);
117   }
118   MSG_RETURN(ret);
119 }