Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
attempt to please clang11
[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/Disk.hpp"
7 #include "simgrid/s4u/Io.hpp"
8 #include "simgrid/s4u/Storage.hpp"
9 #include "src/kernel/activity/IoImpl.hpp"
10 #include "xbt/log.h"
11
12 namespace simgrid {
13 namespace s4u {
14
15 Io::Io(sg_disk_t disk, sg_size_t size, OpType type) : disk_(disk), size_(size), type_(type)
16 {
17   Activity::set_remaining(size_);
18   pimpl_ = kernel::activity::IoImplPtr(new kernel::activity::IoImpl());
19 }
20
21 Io::Io(sg_storage_t storage, sg_size_t size, OpType type) : storage_(storage), size_(size), type_(type)
22 {
23   Activity::set_remaining(size_);
24   pimpl_ = kernel::activity::IoImplPtr(new kernel::activity::IoImpl());
25 }
26
27 Io* Io::start()
28 {
29   kernel::actor::simcall([this] {
30     if (storage_) {
31       (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_))
32           .set_name(get_name())
33           .set_storage(storage_->get_impl())
34           .set_size(size_)
35           .set_type(type_)
36           .start();
37     } else {
38       (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_))
39           .set_name(get_name())
40           .set_disk(disk_->get_impl())
41           .set_size(size_)
42           .set_type(type_)
43           .start();
44     }
45   });
46   state_ = State::STARTED;
47   return this;
48 }
49
50 Io* Io::cancel()
51 {
52   simgrid::kernel::actor::simcall([this] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->cancel(); });
53   state_ = State::CANCELED;
54   return this;
55 }
56
57 Io* Io::wait()
58 {
59   return this->wait_for(-1);
60 }
61
62 Io* Io::wait_for(double timeout)
63 {
64   simcall_io_wait(pimpl_, timeout);
65   state_ = State::FINISHED;
66   return this;
67 }
68
69 bool Io::test()
70 {
71   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
72
73   if (state_ == State::FINISHED)
74     return true;
75
76   if (state_ == State::INITED)
77     this->start();
78
79   if (simcall_io_test(pimpl_)) {
80     state_ = State::FINISHED;
81     return true;
82   }
83
84   return false;
85 }
86
87 /** @brief Returns the amount of flops that remain to be done */
88 double Io::get_remaining()
89 {
90   return kernel::actor::simcall(
91       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
92 }
93
94 sg_size_t Io::get_performed_ioops()
95 {
96   return kernel::actor::simcall(
97       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops(); });
98 }
99
100 } // namespace s4u
101 } // namespace simgrid