Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / kernel / activity / CommImpl.cpp
1 /* Copyright (c) 2007-2019. 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/CommImpl.hpp"
7 #include "simgrid/kernel/resource/Action.hpp"
8 #include "simgrid/modelchecker.h"
9 #include "src/kernel/activity/MailboxImpl.hpp"
10 #include "src/mc/mc_replay.hpp"
11 #include "src/simix/smx_network_private.hpp"
12 #include "src/surf/surf_interface.hpp"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_network);
15
16 simgrid::kernel::activity::CommImpl::CommImpl(e_smx_comm_type_t _type) : type(_type)
17 {
18   state_   = SIMIX_WAITING;
19   src_data = nullptr;
20   dst_data = nullptr;
21   XBT_DEBUG("Create comm activity %p", this);
22 }
23
24 simgrid::kernel::activity::CommImpl::~CommImpl()
25 {
26   XBT_DEBUG("Really free communication %p", this);
27
28   cleanupSurf();
29
30   if (detached && state_ != SIMIX_DONE) {
31     /* the communication has failed and was detached:
32      * we have to free the buffer */
33     if (clean_fun)
34       clean_fun(src_buff);
35     src_buff = nullptr;
36   }
37
38   if (mbox)
39     mbox->remove(this);
40 }
41
42 void simgrid::kernel::activity::CommImpl::suspend()
43 {
44   /* FIXME: shall we suspend also the timeout synchro? */
45   if (surfAction_)
46     surfAction_->suspend();
47   /* in the other case, the action will be suspended on creation, in SIMIX_comm_start() */
48 }
49
50 void simgrid::kernel::activity::CommImpl::resume()
51 {
52   /*FIXME: check what happen with the timeouts */
53   if (surfAction_)
54     surfAction_->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::kernel::activity::CommImpl::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     mbox->remove(this);
64     state_ = SIMIX_CANCELED;
65   } else if (not MC_is_active() /* when running the MC there are no surf actions */
66              && not MC_record_replay_is_active() && (state_ == SIMIX_READY || state_ == SIMIX_RUNNING)) {
67
68     surfAction_->cancel();
69   }
70 }
71
72 /**  @brief get the amount remaining from the communication */
73 double simgrid::kernel::activity::CommImpl::remains()
74 {
75   return surfAction_->get_remains();
76 }
77
78 /** @brief This is part of the cleanup process, probably an internal command */
79 void simgrid::kernel::activity::CommImpl::cleanupSurf()
80 {
81   if (surfAction_) {
82     surfAction_->unref();
83     surfAction_ = nullptr;
84   }
85
86   if (src_timeout) {
87     src_timeout->unref();
88     src_timeout = nullptr;
89   }
90
91   if (dst_timeout) {
92     dst_timeout->unref();
93     dst_timeout = nullptr;
94   }
95 }
96
97 void simgrid::kernel::activity::CommImpl::post()
98 {
99   /* Update synchro state */
100   if (src_timeout && src_timeout->get_state() == simgrid::kernel::resource::Action::State::FINISHED)
101     state_ = SIMIX_SRC_TIMEOUT;
102   else if (dst_timeout && dst_timeout->get_state() == simgrid::kernel::resource::Action::State::FINISHED)
103     state_ = SIMIX_DST_TIMEOUT;
104   else if (src_timeout && src_timeout->get_state() == simgrid::kernel::resource::Action::State::FAILED)
105     state_ = SIMIX_SRC_HOST_FAILURE;
106   else if (dst_timeout && dst_timeout->get_state() == simgrid::kernel::resource::Action::State::FAILED)
107     state_ = SIMIX_DST_HOST_FAILURE;
108   else if (surfAction_ && surfAction_->get_state() == simgrid::kernel::resource::Action::State::FAILED) {
109     state_ = SIMIX_LINK_FAILURE;
110   } else
111     state_ = SIMIX_DONE;
112
113   XBT_DEBUG("SIMIX_post_comm: comm %p, state %d, src_proc %p, dst_proc %p, detached: %d", this, (int)state_, src_proc,
114             dst_proc, detached);
115
116   /* destroy the surf actions associated with the Simix communication */
117   cleanupSurf();
118
119   /* if there are simcalls associated with the synchro, then answer them */
120   if (not simcalls_.empty()) {
121     SIMIX_comm_finish(this);
122   }
123 }