Logo AND Algorithmique Numérique Distribuée

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