Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A canceled Activity is a completed Activity too
[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 "xbt/log.h"
11
12 namespace simgrid {
13 namespace s4u {
14 xbt::signal<void(Io const&)> Io::on_start;
15 xbt::signal<void(Io const&)> Io::on_completion;
16
17 Io::Io()
18 {
19   pimpl_ = kernel::activity::IoImplPtr(new kernel::activity::IoImpl());
20 }
21
22 IoPtr Io::init()
23 {
24  return IoPtr(new Io());
25 }
26
27 Io* Io::start()
28 {
29   kernel::actor::simcall([this] {
30     (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_))
31                   .set_name(get_name())
32                   .set_disk(disk_->get_impl())
33                   .set_size(size_)
34                   .set_type(type_)
35                   .start();
36   });
37
38   if (suspended_)
39     pimpl_->suspend();
40
41   state_ = State::STARTED;
42   on_start(*this);
43   return this;
44 }
45
46 Io* Io::cancel()
47 {
48   simgrid::kernel::actor::simcall([this] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->cancel(); });
49   state_ = State::CANCELED;
50   on_completion(*this);
51   return this;
52 }
53
54 Io* Io::wait()
55 {
56   return this->wait_for(-1);
57 }
58
59 Io* Io::wait_for(double timeout)
60 {
61   if (state_ == State::INITED)
62     vetoable_start();
63
64   kernel::actor::ActorImpl* issuer = Actor::self()->get_impl();
65   kernel::actor::simcall_blocking<void>([this, issuer, timeout] { this->get_impl()->wait_for(issuer, timeout); });
66   state_ = State::FINISHED;
67   this->release_dependencies();
68
69   on_completion(*this);
70   return this;
71 }
72
73 IoPtr Io::set_disk(sg_disk_t disk)
74 {
75   disk_ = disk;
76
77   // Setting the disk may allow to start the activity, let's try
78   if (state_ == State::STARTING)
79     vetoable_start();
80
81  return this;
82 }
83
84 IoPtr Io::set_size(sg_size_t size)
85 {
86   size_ = size;
87   Activity::set_remaining(size_);
88   return this;
89 }
90
91 IoPtr Io::set_op_type(OpType type)
92 {
93   type_ = type;
94   return this;
95 }
96
97 /** @brief Returns the amount of flops that remain to be done */
98 double Io::get_remaining() const
99 {
100   return kernel::actor::simcall(
101       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
102 }
103
104 sg_size_t Io::get_performed_ioops() const
105 {
106   return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops();
107 }
108
109 } // namespace s4u
110 } // namespace simgrid