Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove obsolete entries in .gitignore.
[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 "simgrid/s4u/Storage.hpp"
10 #include "src/kernel/activity/IoImpl.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(sg_disk_t disk, sg_size_t size, OpType type) : disk_(disk), size_(size), type_(type)
19 {
20   Activity::set_remaining(size_);
21   pimpl_ = kernel::activity::IoImplPtr(new kernel::activity::IoImpl());
22 }
23
24 Io::Io(sg_storage_t storage, sg_size_t size, OpType type) : storage_(storage), size_(size), type_(type)
25 {
26   Activity::set_remaining(size_);
27   pimpl_ = kernel::activity::IoImplPtr(new kernel::activity::IoImpl());
28 }
29
30 Io* Io::start()
31 {
32   kernel::actor::simcall([this] {
33     if (storage_) {
34       (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_))
35           .set_name(get_name())
36           .set_storage(storage_->get_impl())
37           .set_size(size_)
38           .set_type(type_)
39           .start();
40     } else {
41       (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_))
42           .set_name(get_name())
43           .set_disk(disk_->get_impl())
44           .set_size(size_)
45           .set_type(type_)
46           .start();
47     }
48   });
49
50   if (suspended_)
51     pimpl_->suspend();
52
53   state_ = State::STARTED;
54   on_start(*this);
55   return this;
56 }
57
58 Io* Io::cancel()
59 {
60   simgrid::kernel::actor::simcall([this] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->cancel(); });
61   state_ = State::CANCELED;
62   return this;
63 }
64
65 Io* Io::wait()
66 {
67   return this->wait_for(-1);
68 }
69
70 Io* Io::wait_for(double timeout)
71 {
72   if (state_ == State::INITED)
73     vetoable_start();
74
75   kernel::actor::ActorImpl* issuer = Actor::self()->get_impl();
76   kernel::actor::simcall_blocking<void>([this, issuer, timeout] { this->get_impl()->wait_for(issuer, timeout); });
77   state_ = State::FINISHED;
78   this->release_dependencies();
79
80   on_completion(*this);
81   return this;
82 }
83
84 /** @brief Returns the amount of flops that remain to be done */
85 double Io::get_remaining() const
86 {
87   return kernel::actor::simcall(
88       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
89 }
90
91 sg_size_t Io::get_performed_ioops() const
92 {
93   return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops();
94 }
95
96 } // namespace s4u
97 } // namespace simgrid