Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
25cb01261e87e97a7fb3f3959e4744c1e7bd3918
[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   Activity::set_remaining(size_);
20   pimpl_ = simix::simcall([this] {
21     return boost::static_pointer_cast<kernel::activity::IoImpl>(SIMIX_io_start(name_, size_, storage_, type_));
22   });
23   state_ = State::STARTED;
24   return this;
25 }
26
27 Activity* Io::cancel()
28 {
29   simgrid::simix::simcall([this] { dynamic_cast<kernel::activity::IoImpl*>(pimpl_.get())->cancel(); });
30   state_ = State::CANCELED;
31   return this;
32 }
33
34 Activity* Io::wait()
35 {
36   simcall_io_wait(pimpl_);
37   state_ = State::FINISHED;
38   return this;
39 }
40
41 Activity* Io::wait_for(double timeout)
42 {
43   THROW_UNIMPLEMENTED;
44   return this;
45 }
46
47 bool Io::test()
48 {
49   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
50
51   if (state_ == State::FINISHED)
52     return true;
53
54   if (state_ == State::INITED)
55     this->start();
56
57   THROW_UNIMPLEMENTED;
58
59   return false;
60 }
61
62 /** @brief Returns the amount of flops that remain to be done */
63 double Io::get_remaining()
64 {
65   return simgrid::simix::simcall(
66       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
67 }
68
69 sg_size_t Io::get_performed_ioops()
70 {
71   return simgrid::simix::simcall(
72       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops(); });
73 }
74
75 void intrusive_ptr_release(simgrid::s4u::Io* i)
76 {
77   if (i->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
78     std::atomic_thread_fence(std::memory_order_acquire);
79     delete i;
80   }
81 }
82
83 void intrusive_ptr_add_ref(simgrid::s4u::Io* i)
84 {
85   i->refcount_.fetch_add(1, std::memory_order_relaxed);
86 }
87 } // namespace s4u
88 } // namespace simgrid