Logo AND Algorithmique Numérique Distribuée

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