Logo AND Algorithmique Numérique Distribuée

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