Logo AND Algorithmique Numérique Distribuée

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