Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into disk
[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/Disk.hpp"
7 #include "simgrid/s4u/Io.hpp"
8 #include "simgrid/s4u/Storage.hpp"
9 #include "src/kernel/activity/IoImpl.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_disk_t disk, sg_size_t size, OpType type) : disk_(disk), size_(size), type_(type)
18 {
19   Activity::set_remaining(size_);
20   pimpl_ = kernel::activity::IoImplPtr(new kernel::activity::IoImpl());
21 }
22
23 Io::Io(sg_storage_t storage, sg_size_t size, OpType type) : storage_(storage), size_(size), type_(type)
24 {
25   Activity::set_remaining(size_);
26   pimpl_ = kernel::activity::IoImplPtr(new kernel::activity::IoImpl());
27 }
28
29 Io* Io::start()
30 {
31   kernel::actor::simcall([this] {
32     if (storage_) {
33       (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_))
34           .set_name(name_)
35           .set_storage(storage_->get_impl())
36           .set_size(size_)
37           .set_type(type_)
38           .start();
39     } else {
40       (*boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_))
41           .set_name(name_)
42           .set_disk(disk_->get_impl())
43           .set_size(size_)
44           .set_type(type_)
45           .start();
46     }
47   });
48   state_ = State::STARTED;
49   return this;
50 }
51
52 Io* Io::cancel()
53 {
54   simgrid::kernel::actor::simcall([this] { boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->cancel(); });
55   state_ = State::CANCELED;
56   return this;
57 }
58
59 Io* Io::wait()
60 {
61   simcall_io_wait(pimpl_);
62   state_ = State::FINISHED;
63   return this;
64 }
65
66 Io* Io::wait_for(double)
67 {
68   THROW_UNIMPLEMENTED;
69 }
70
71 bool Io::test()
72 {
73   xbt_assert(state_ == State::INITED || state_ == State::STARTED || state_ == State::FINISHED);
74
75   if (state_ == State::FINISHED)
76     return true;
77
78   if (state_ == State::INITED)
79     this->start();
80
81   THROW_UNIMPLEMENTED;
82
83   // return false
84 }
85
86 /** @brief Returns the amount of flops that remain to be done */
87 double Io::get_remaining()
88 {
89   return kernel::actor::simcall(
90       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_remaining(); });
91 }
92
93 sg_size_t Io::get_performed_ioops()
94 {
95   return kernel::actor::simcall(
96       [this]() { return boost::static_pointer_cast<kernel::activity::IoImpl>(pimpl_)->get_performed_ioops(); });
97 }
98
99 void intrusive_ptr_release(simgrid::s4u::Io* i)
100 {
101   if (i->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
102     std::atomic_thread_fence(std::memory_order_acquire);
103     delete i;
104   }
105 }
106
107 void intrusive_ptr_add_ref(simgrid::s4u::Io* i)
108 {
109   i->refcount_.fetch_add(1, std::memory_order_relaxed);
110 }
111 } // namespace s4u
112 } // namespace simgrid