Logo AND Algorithmique Numérique Distribuée

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