Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1949b21a0515eba4b13f55c7025f9a525e527c5c
[simgrid.git] / src / kernel / activity / 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/kernel/activity/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::kernel::activity::Comm::Comm(e_smx_comm_type_t _type) : type(_type)
15 {
16   state = SIMIX_WAITING;
17   src_data=nullptr;
18   dst_data=nullptr;
19
20   XBT_DEBUG("Create communicate synchro %p", this);
21 }
22
23 simgrid::kernel::activity::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 = nullptr;
35   }
36
37   if(mbox)
38     mbox->remove(this);
39 }
40 void simgrid::kernel::activity::Comm::suspend()
41 {
42   /* FIXME: shall we suspend also the timeout synchro? */
43   if (surf_comm)
44     surf_comm->suspend();
45   /* in the other case, the action will be suspended on creation, in SIMIX_comm_start() */
46
47 }
48
49 void simgrid::kernel::activity::Comm::resume()
50 {
51   /*FIXME: check what happen with the timeouts */
52   if (surf_comm)
53     surf_comm->resume();
54   /* in the other case, the synchro were not really suspended yet, see SIMIX_comm_suspend() and SIMIX_comm_start() */
55 }
56
57 void simgrid::kernel::activity::Comm::cancel()
58 {
59   /* if the synchro is a waiting state means that it is still in a mbox */
60   /* so remove from it and delete it */
61   if (state == SIMIX_WAITING) {
62     mbox->remove(this);
63     state = SIMIX_CANCELED;
64   } else if (not MC_is_active() /* when running the MC there are no surf actions */
65              && not MC_record_replay_is_active() && (state == SIMIX_READY || state == SIMIX_RUNNING)) {
66
67     surf_comm->cancel();
68   }
69 }
70
71 /**  @brief get the amount remaining from the communication */
72 double simgrid::kernel::activity::Comm::remains()
73 {
74   if (state == SIMIX_RUNNING)
75     return surf_comm->getRemains();
76
77   /* FIXME: check what should be returned in the other cases */
78   return 0;
79 }
80
81 /** @brief This is part of the cleanup process, probably an internal command */
82 void simgrid::kernel::activity::Comm::cleanupSurf()
83 {
84   if (surf_comm){
85     surf_comm->unref();
86     surf_comm = nullptr;
87   }
88
89   if (src_timeout){
90     src_timeout->unref();
91     src_timeout = nullptr;
92   }
93
94   if (dst_timeout){
95     dst_timeout->unref();
96     dst_timeout = nullptr;
97   }
98 }
99
100 void simgrid::kernel::activity::Comm::post()
101 {
102   /* Update synchro state */
103   if (src_timeout &&  src_timeout->getState() == simgrid::surf::Action::State::done)
104     state = SIMIX_SRC_TIMEOUT;
105   else if (dst_timeout && dst_timeout->getState() == simgrid::surf::Action::State::done)
106     state = SIMIX_DST_TIMEOUT;
107   else if (src_timeout && src_timeout->getState() == simgrid::surf::Action::State::failed)
108     state = SIMIX_SRC_HOST_FAILURE;
109   else if (dst_timeout && dst_timeout->getState() == simgrid::surf::Action::State::failed)
110     state = SIMIX_DST_HOST_FAILURE;
111   else if (surf_comm && surf_comm->getState() == simgrid::surf::Action::State::failed) {
112     state = SIMIX_LINK_FAILURE;
113   } else
114     state = SIMIX_DONE;
115
116   XBT_DEBUG("SIMIX_post_comm: comm %p, state %d, src_proc %p, dst_proc %p, detached: %d",
117             this, (int)state, src_proc, dst_proc, detached);
118
119   /* destroy the surf actions associated with the Simix communication */
120   cleanupSurf();
121
122   /* if there are simcalls associated with the synchro, then answer them */
123   if (not simcalls.empty())
124     SIMIX_comm_finish(this);
125 }