3 /* Copyright (c) 2009 Cristian Rosa.
4 All rights reserved. */
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. */
12 /******************************************************************************/
13 /* Rendez-Vous Points */
14 /******************************************************************************/
17 * \brief Creates a new rendez-vous point
18 * \param name The name of the rendez-vous point
19 * \return The created rendez-vous point
21 smx_rdv_t SIMIX_rdv_create(char *name)
23 smx_rdv_t rvp = xbt_new0(s_smx_rvpoint_t, 1);
24 rvp->name = xbt_strdup(name);
25 rvp->read = SIMIX_mutex_init();
26 rvp->write = SIMIX_mutex_init();
27 rvp->comm_fifo = xbt_fifo_new();
33 * \brief Destroy a rendez-vous point
34 * \param name The rendez-vous point to destroy
36 void SIMIX_rdv_destroy(smx_rdv_t rvp)
39 SIMIX_mutex_destroy(rvp->read);
40 SIMIX_mutex_destroy(rvp->write);
41 xbt_fifo_free(rvp->comm_fifo);
46 * \brief Push a communication request into a rendez-vous point
47 * The communications request are dequeued by the two functions below
48 * \param rvp The rendez-vous point
49 * \param comm The communication request
51 static inline void SIMIX_rdv_push(smx_rdv_t rvp, smx_comm_t comm)
53 xbt_fifo_push(rvp->comm_fifo, comm);
57 * \brief Checks if there is a communication request queued in a rendez-vous matching our needs
58 * \param rvp The rendez-vous with the queue
59 * \param look_for_src boolean. True: we are receiver looking for sender; False: other way round
60 * \return The communication request if found, or a newly created one otherwise.
62 smx_comm_t SIMIX_rdv_get_request_or_create(smx_rdv_t rvp, int look_for_src) {
63 /* Get a communication request from the rendez-vous queue. If it is the kind
64 of request we are looking for then return it, otherwise put it again in the
67 smx_comm_t comm = xbt_fifo_shift(rvp->comm_fifo);
70 if (( look_for_src && comm->src_host != NULL) ||
71 (!look_for_src && comm->dst_host != NULL)) {
73 SIMIX_communication_use(comm);
77 xbt_fifo_unshift(rvp->comm_fifo, comm);
79 /* no relevant request found. Create a new comm action and set it up */
80 comm = SIMIX_communication_new(rvp);
81 SIMIX_rdv_push(rvp, comm);
82 SIMIX_communication_use(comm);
87 /******************************************************************************/
88 /* Communication Requests */
89 /******************************************************************************/
92 * \brief Creates a new communication request
93 * \param sender The process starting the communication (by send)
94 * \param receiver The process receiving the communication (by recv)
95 * \return the communication request
97 smx_comm_t SIMIX_communication_new(smx_rdv_t rdv)
99 /* alloc structures */
100 smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
101 comm->cond = SIMIX_cond_init();
108 * \brief Destroy a communication request
109 * \param comm The request to be destroyed
111 void SIMIX_communication_destroy(smx_comm_t comm)
114 if(comm->refcount == 0){
115 if(comm->act != NULL)
116 SIMIX_action_destroy(comm->act);
118 xbt_free(comm->cond);
124 * \brief Increase the number of users of the communication.
125 * \param comm The communication request
126 * Each communication request can be used by more than one process, so it is
127 * necessary to know number of them at destroy time, to avoid freeing stuff that
128 * maybe is in use by others.
131 static inline void SIMIX_communication_use(smx_comm_t comm)
137 * \brief Start the simulation of a communication request
138 * \param comm The communication request
140 static inline void SIMIX_communication_start(smx_comm_t comm)
142 comm->act = SIMIX_action_communicate(comm->src_host, comm->dst_host, NULL,
143 comm->task_size, comm->rate);
145 SIMIX_register_action_to_condition(comm->act, comm->cond);
149 * \brief Waits for communication completion and performs error checking
150 * \param comm The communication
151 * \param timeout The max amount of time to wait for the communication to finish
154 * - host_error if peer failed
155 * - timeout_error if communication reached the timeout specified
156 * - network_error if network failed or peer issued a timeout
158 static inline void SIMIX_communication_wait_for_completion(smx_comm_t comm, double timeout)
164 SIMIX_cond_wait_timeout(comm->cond, NULL, timeout);
167 /* If it's a timeout then cancel the communication and signal the other peer */
168 if(e.category == timeout_error)
169 SIMIX_action_cancel(comm->act);
170 SIMIX_cond_signal(comm->cond);
174 SIMIX_cond_wait(comm->cond, NULL);
177 SIMIX_unregister_action_to_condition(comm->act, comm->cond);
179 /* Check for errors */
180 if (SIMIX_host_get_state(comm->dst_host) == 0){
181 THROW1(host_error, 0, "Destination host %s failed", comm->dst_host->name);
182 } else if (SIMIX_host_get_state(comm->src_host) == 0){
183 THROW1(host_error, 0, "Source host %s failed", comm->src_host->name);
184 } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED){
185 THROW0(network_error, 0, "Link failure");
189 /******************************************************************************/
190 /* Synchronous Communication */
191 /******************************************************************************/
193 * - host_error if peer failed
194 * - timeout_error if communication reached the timeout specified
195 * - network_error if network failed or peer issued a timeout
197 void SIMIX_network_send(smx_rdv_t rdv, double task_size, double rate, double timeout, void *data, size_t data_size)
201 /* Setup communication request */
202 comm = SIMIX_rdv_get_request_or_create(rdv, 0);
203 comm->src_host = SIMIX_host_self();
204 comm->task_size = task_size;
207 comm->data_size = data_size;
209 /* If the receiver is already there, start the communication */
210 /* If it's not here already, it will start that communication itself later on */
211 if(comm->dst_host != NULL)
212 SIMIX_communication_start(comm);
214 /* Wait for communication completion */
215 /* FIXME: if the semantic is non blocking, it shouldn't wait on the condition here */
216 /* FIXME: add timeout checking stuff */
217 SIMIX_communication_wait_for_completion(comm, timeout);
219 SIMIX_communication_destroy(comm);
223 * - host_error if peer failed
224 * - timeout_error if communication reached the timeout specified
225 * - network_error if network failed or peer issued a timeout
227 void SIMIX_network_recv(smx_rdv_t rdv, double timeout, void **data, size_t *data_size)
231 /* Setup communication request */
232 comm = SIMIX_rdv_get_request_or_create(rdv, 1);
233 comm->dst_host = SIMIX_host_self();
234 comm->dest_buff = data;
235 comm->dest_buff_size = data_size;
237 /* If the sender is already there, start the communication */
238 /* If it's not here already, it will start that communication itself later on */
239 if (comm->src_host != NULL)
240 SIMIX_communication_start(comm);
242 /* Wait for communication completion */
243 /* FIXME: if the semantic is non blocking, it shouldn't wait on the condition here */
244 /* FIXME: add timeout checking stuff */
245 SIMIX_communication_wait_for_completion(comm, timeout);
247 SIMIX_communication_destroy(comm);
250 /******************************************************************************/
251 /* Asynchronous Communication */
252 /******************************************************************************/
255 void SIMIX_network_wait(smx_action_t comm, double timeout)
258 SIMIX_cond_wait_timeout(rvp_cond, rvp_comm_mutex, timeout - start_time);
260 SIMIX_cond_wait(rvp_cond, rvp_comm_mutex);
265 XBT_PUBLIC(int) SIMIX_network_test(smx_action_t comm)
267 if(SIMIX_action_get_state (comm) == SURF_ACTION_DONE){
270 return SIMIX_action_get_state (comm) == SURF_ACTION_DONE ? TRUE : FALSE;
275 /* FIXME: MOVE DATA COPY TO MAESTRO AT ACTION SIGNAL TIME
276 We are OK, let's copy the message to receiver's buffer
277 *size = *size < comm->data_size ? *size : comm->data_size;
278 memcpy(*data, comm->data, *size);*/