Logo AND Algorithmique Numérique Distribuée

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