Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define overridable Activity::complete() to be called on activity completion.
[simgrid.git] / src / s4u / s4u_Io.cpp
1 /* Copyright (c) 2018-2021. 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 "simgrid/s4u/Actor.hpp"
7 #include "simgrid/s4u/Disk.hpp"
8 #include "simgrid/s4u/Io.hpp"
9 #include "src/kernel/activity/IoImpl.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11 #include "xbt/log.h"
12
13 namespace simgrid {
14 namespace s4u {
15 xbt::signal<void(Io const&)> Io::on_start;
16 xbt::signal<void(Io const&)> Io::on_completion;
17
18 Io::Io(kernel::activity::IoImplPtr pimpl)
19 {
20   pimpl_ = pimpl;
21 }
22
23 void Io::complete(Activity::State state)
24 {
25   Activity::complete(state);
26   on_completion(*this);
27 }
28
29 IoPtr Io::init()
30 {
31   auto pimpl = kernel::activity::IoImplPtr(new kernel::activity::IoImpl());
32   return IoPtr(pimpl->get_iface());
33 }
34
35 Io* Io::start()
36 {
37   kernel::actor::simcall(
38       [this] { (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)).set_name(get_name()).start(); });
39
40   if (suspended_)
41     pimpl_->suspend();
42
43   state_ = State::STARTED;
44   on_start(*this);
45   return this;
46 }
47
48 Io* Io::cancel()
49 {
50   kernel::actor::simcall([this] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->cancel(); });
51   complete(State::CANCELED);
52   return this;
53 }
54
55 Io* Io::wait()
56 {
57   return this->wait_for(-1);
58 }
59
60 Io* Io::wait_for(double timeout)
61 {
62   if (state_ == State::INITED)
63     vetoable_start();
64
65   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
66   kernel::actor::simcall_blocking([this, issuer, timeout] { this->get_impl()->wait_for(issuer, timeout); });
67   complete(state_ = State::FINISHED);
68   return this;
69 }
70
71 IoPtr Io::set_disk(const_sg_disk_t disk)
72 {
73   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set disk once the Io is started");
74
75   kernel::actor::simcall(
76       [this, disk] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_disk(disk->get_impl()); });
77
78   // Setting the disk may allow to start the activity, let's try
79   if (state_ == State::STARTING)
80     vetoable_start();
81
82  return this;
83 }
84
85 IoPtr Io::set_size(sg_size_t size)
86 {
87   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set size once the Io is started");
88   kernel::actor::simcall(
89       [this, size] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_size(size); });
90   Activity::set_remaining(size);
91   return this;
92 }
93
94 IoPtr Io::set_op_type(OpType type)
95 {
96   xbt_assert(state_ == State::INITED || state_ == State::STARTING, "Cannot set size once the Io is started");
97   kernel::actor::simcall(
98       [this, type] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->set_type(type); });
99   return this;
100 }
101
102 /** @brief Returns the amount of flops that remain to be done */
103 double Io::get_remaining() const
104 {
105   return kernel::actor::simcall(
106       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
107 }
108
109 sg_size_t Io::get_performed_ioops() const
110 {
111   return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops();
112 }
113
114 bool Io::is_assigned() const
115 {
116   return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_disk() != nullptr;
117 }
118
119 } // namespace s4u
120 } // namespace simgrid