Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use C++ to implement dynamic dispatch
[simgrid.git] / src / simix / SynchroComm.cpp
1 /* Copyright (c) 2007-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/simix/SynchroComm.hpp"
7 #include "src/surf/surf_interface.hpp"
8 #include "src/simix/smx_network_private.h"
9 #include "simgrid/modelchecker.h"
10 #include "src/mc/mc_replay.h"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_network);
13
14 simgrid::simix::Comm::Comm(e_smx_comm_type_t _type) {
15   state = SIMIX_WAITING;
16   this->type = _type;
17   src_data=NULL;
18   dst_data=NULL;
19
20   XBT_DEBUG("Create communicate synchro %p", this);
21 }
22
23 simgrid::simix::Comm::~Comm()
24 {
25   XBT_DEBUG("Really free communication %p", this);
26
27   cleanupSurf();
28
29   if (detached && state != SIMIX_DONE) {
30     /* the communication has failed and was detached:
31      * we have to free the buffer */
32     if (clean_fun)
33       clean_fun(src_buff);
34     src_buff = NULL;
35   }
36
37   if(mbox)
38     SIMIX_mbox_remove(mbox, this);
39
40 }
41 void simgrid::simix::Comm::suspend()
42 {
43   /* FIXME: shall we suspend also the timeout synchro? */
44   if (surf_comm)
45     surf_comm->suspend();
46   /* in the other case, the action will be suspended on creation, in SIMIX_comm_start() */
47
48 }
49
50 void simgrid::simix::Comm::resume()
51 {
52   /*FIXME: check what happen with the timeouts */
53   if (surf_comm)
54     surf_comm->resume();
55   /* in the other case, the synchro were not really suspended yet, see SIMIX_comm_suspend() and SIMIX_comm_start() */
56 }
57
58 void simgrid::simix::Comm::cancel()
59 {
60   /* if the synchro is a waiting state means that it is still in a mbox */
61   /* so remove from it and delete it */
62   if (state == SIMIX_WAITING) {
63     SIMIX_mbox_remove(mbox, this);
64     state = SIMIX_CANCELED;
65   }
66   else if (!MC_is_active() /* when running the MC there are no surf actions */
67            && !MC_record_replay_is_active()
68            && (state == SIMIX_READY || state == SIMIX_RUNNING)) {
69
70     surf_comm->cancel();
71   }
72 }
73
74 /**  @brief get the amount remaining from the communication */
75 double simgrid::simix::Comm::remains()
76 {
77   switch (state) {
78
79   case SIMIX_RUNNING:
80     return surf_comm->getRemains();
81     break;
82
83   case SIMIX_WAITING:
84   case SIMIX_READY:
85     return 0; /*FIXME: check what should be returned */
86     break;
87
88   default:
89     return 0; /*FIXME: is this correct? */
90     break;
91   }
92 }
93
94 /** @brief This is part of the cleanup process, probably an internal command */
95 void simgrid::simix::Comm::cleanupSurf()
96 {
97   if (surf_comm){
98     surf_comm->unref();
99     surf_comm = NULL;
100   }
101
102   if (src_timeout){
103     src_timeout->unref();
104     src_timeout = NULL;
105   }
106
107   if (dst_timeout){
108     dst_timeout->unref();
109     dst_timeout = NULL;
110   }
111 }
112
113 void simgrid::simix::Comm::post()
114 {
115   /* Update synchro state */
116   if (src_timeout &&  src_timeout->getState() == simgrid::surf::Action::State::done)
117     state = SIMIX_SRC_TIMEOUT;
118   else if (dst_timeout && dst_timeout->getState() == simgrid::surf::Action::State::done)
119     state = SIMIX_DST_TIMEOUT;
120   else if (src_timeout && src_timeout->getState() == simgrid::surf::Action::State::failed)
121     state = SIMIX_SRC_HOST_FAILURE;
122   else if (dst_timeout && dst_timeout->getState() == simgrid::surf::Action::State::failed)
123     state = SIMIX_DST_HOST_FAILURE;
124   else if (surf_comm && surf_comm->getState() == simgrid::surf::Action::State::failed) {
125     state = SIMIX_LINK_FAILURE;
126   } else
127     state = SIMIX_DONE;
128
129   XBT_DEBUG("SIMIX_post_comm: comm %p, state %d, src_proc %p, dst_proc %p, detached: %d",
130             this, (int)state, src_proc, dst_proc, detached);
131
132   /* destroy the surf actions associated with the Simix communication */
133   cleanupSurf();
134
135   /* if there are simcalls associated with the synchro, then answer them */
136   if (xbt_fifo_size(simcalls))
137     SIMIX_comm_finish(this);
138 }