Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Take simcalls {execution,io}_wait toward modernity.
[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   state_ = State::STARTED;
48   return this;
49 }
50
51 Io* Io::cancel()
52 {
53   simgrid::kernel::actor::simcall([this] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->cancel(); });
54   state_ = State::CANCELED;
55   return this;
56 }
57
58 Io* Io::wait()
59 {
60   return this->wait_for(-1);
61 }
62
63 Io* Io::wait_for(double timeout)
64 {
65   if (state_ == State::INITED)
66     vetoable_start();
67
68   kernel::actor::ActorImpl* issuer = Actor::self()->get_impl();
69   kernel::actor::simcall_blocking<void>([this, issuer, timeout] { this->get_impl()->wait_for(issuer, timeout); });
70   state_ = State::FINISHED;
71   this->release_dependencies();
72   return this;
73 }
74
75 /** @brief Returns the amount of flops that remain to be done */
76 double Io::get_remaining() const
77 {
78   return kernel::actor::simcall(
79       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
80 }
81
82 sg_size_t Io::get_performed_ioops() const
83 {
84   return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops();
85 }
86
87 } // namespace s4u
88 } // namespace simgrid