Logo AND Algorithmique Numérique Distribuée

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