Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'async_io' into 'master'
[simgrid.git] / src / s4u / s4u_Io.cpp
1 /* Copyright (c) 2018. 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 Activity* Io::start()
18 {
19   set_remaining(size_);
20   pimpl_ = simix::simcall([this] { return SIMIX_io_start(name_, size_, storage_, type_); });
21   state_ = State::STARTED;
22   return this;
23 }
24
25 Activity* Io::cancel()
26 {
27   simgrid::simix::simcall([this] { dynamic_cast<kernel::activity::IoImpl*>(pimpl_.get())->cancel(); });
28   state_ = State::CANCELED;
29   return this;
30 }
31
32 Activity* Io::wait()
33 {
34   simcall_io_wait(pimpl_);
35   state_ = State::FINISHED;
36   return this;
37 }
38
39 Activity* Io::wait(double timeout)
40 {
41   THROW_UNIMPLEMENTED;
42   return this;
43 }
44
45 /** @brief Returns the amount of flops that remain to be done */
46 double Io::get_remaining()
47 {
48   return simgrid::simix::simcall(
49       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
50 }
51
52 sg_size_t Io::get_performed_ioops()
53 {
54   return simgrid::simix::simcall(
55       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops(); });
56 }
57
58 IoPtr Io::set_io_type(OpType type)
59 {
60   xbt_assert(state_ == State::INITED, "Cannot change the name of an exec after its start");
61   type_ = type;
62   return this;
63 }
64
65 void intrusive_ptr_release(simgrid::s4u::Io* i)
66 {
67   if (i->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
68     std::atomic_thread_fence(std::memory_order_acquire);
69     delete i;
70   }
71 }
72
73 void intrusive_ptr_add_ref(simgrid::s4u::Io* i)
74 {
75   i->refcount_.fetch_add(1, std::memory_order_relaxed);
76 }
77 } // namespace s4u
78 } // namespace simgrid