Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement I/O as asynchronous activities
[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 "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 Activity* Io::start()
17 {
18   pimpl_ = simcall_io_start(size_, storage_);
19   state_ = State::STARTED;
20   return this;
21 }
22
23 Activity* Io::cancel()
24 {
25   simgrid::simix::simcall([this] { dynamic_cast<kernel::activity::IoImpl*>(pimpl_.get())->cancel(); });
26   state_ = State::CANCELED;
27   return this;
28 }
29
30 // Activity* Exec::wait()
31 //{
32 //  simcall_execution_wait(pimpl_);
33 //  state_ = State::FINISHED;
34 //  return this;
35 //}
36 //
37 // Activity* Exec::wait(double timeout)
38 //{
39 //  THROW_UNIMPLEMENTED;
40 //  return this;
41 //}
42 //
43 ///** @brief Returns whether the state of the exec is finished */
44 // bool Exec::test()
45 //{
46 //  xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
47 //
48 //  if (state_ == State::FINISHED)
49 //    return true;
50 //
51 //  if (state_ == State::INITED)
52 //    this->start();
53 //
54 //  if (simcall_execution_test(pimpl_)) {
55 //    state_ = State::FINISHED;
56 //    return true;
57 //  }
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<simgrid::kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
67 }
68
69 // double Io::get_remaining_ratio()
70 //{
71 //  return simgrid::simix::simcall([this]() {
72 //    return boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(pimpl_)->get_remaining_ratio();
73 //  });
74 //}
75
76 void intrusive_ptr_release(simgrid::s4u::Io* e)
77 {
78   if (e->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
79     std::atomic_thread_fence(std::memory_order_acquire);
80     delete e;
81   }
82 }
83
84 void intrusive_ptr_add_ref(simgrid::s4u::Io* e)
85 {
86   e->refcount_.fetch_add(1, std::memory_order_relaxed);
87 }
88 } // namespace s4u
89 } // namespace simgrid