Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The creation of the pimpl needs no simcall
[simgrid.git] / src / s4u / s4u_Io.cpp
1 /* Copyright (c) 2018-2019. 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/Io.hpp"
7 #include "simgrid/s4u/Storage.hpp"
8 #include "src/kernel/activity/IoImpl.hpp"
9 #include "xbt/log.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_io, s4u_activity, "S4U asynchronous IOs");
12
13 namespace simgrid {
14 namespace s4u {
15
16 Io::Io(sg_storage_t storage, sg_size_t size, OpType type) : Activity(), storage_(storage), size_(size), type_(type)
17 {
18   Activity::set_remaining(size_);
19   pimpl_ = simix::simcall(
20       [this] { return kernel::activity::IoImplPtr(new kernel::activity::IoImpl(name_, storage_->get_impl())); });
21 }
22
23 Io* Io::start()
24 {
25   simix::simcall([this] { static_cast<kernel::activity::IoImpl*>(pimpl_.get())->start(size_, type_); });
26   state_ = State::STARTED;
27   return this;
28 }
29
30 Io* Io::cancel()
31 {
32   simgrid::simix::simcall([this] { static_cast<kernel::activity::IoImpl*>(pimpl_.get())->cancel(); });
33   state_ = State::CANCELED;
34   return this;
35 }
36
37 Io* Io::wait()
38 {
39   simcall_io_wait(pimpl_);
40   state_ = State::FINISHED;
41   return this;
42 }
43
44 Io* Io::wait_for(double)
45 {
46   THROW_UNIMPLEMENTED;
47 }
48
49 bool Io::test()
50 {
51   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
52
53   if (state_ == State::FINISHED)
54     return true;
55
56   if (state_ == State::INITED)
57     this->start();
58
59   THROW_UNIMPLEMENTED;
60
61   // return false
62 }
63
64 /** @brief Returns the amount of flops that remain to be done */
65 double Io::get_remaining()
66 {
67   return simgrid::simix::simcall(
68       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
69 }
70
71 sg_size_t Io::get_performed_ioops()
72 {
73   return simgrid::simix::simcall(
74       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops(); });
75 }
76
77 void intrusive_ptr_release(simgrid::s4u::Io* i)
78 {
79   if (i->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
80     std::atomic_thread_fence(std::memory_order_acquire);
81     delete i;
82   }
83 }
84
85 void intrusive_ptr_add_ref(simgrid::s4u::Io* i)
86 {
87   i->refcount_.fetch_add(1, std::memory_order_relaxed);
88 }
89 } // namespace s4u
90 } // namespace simgrid