Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement I/O as asynchronous activities
[simgrid.git] / src / kernel / activity / IoImpl.cpp
1 /* Copyright (c) 2007-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 "src/kernel/activity/IoImpl.hpp"
7 #include "simgrid/kernel/resource/Action.hpp"
8 #include "src/simix/smx_io_private.hpp"
9
10 simgrid::kernel::activity::IoImpl::IoImpl(std::string name, resource::Action* surf_action, s4u::Storage* storage)
11     : ActivityImpl(name), storage_(storage), surf_action_(surf_action)
12 {
13   this->state_ = SIMIX_RUNNING;
14
15   surf_action_->set_data(this);
16
17   XBT_DEBUG("Create exec %p", this);
18 }
19
20 void simgrid::kernel::activity::IoImpl::cancel()
21 {
22   XBT_VERB("This exec %p is canceled", this);
23   if (surf_action_ != nullptr)
24     surf_action_->cancel();
25 }
26
27 void simgrid::kernel::activity::IoImpl::suspend()
28 {
29   if (surf_action_ != nullptr)
30     surf_action_->suspend();
31 }
32
33 void simgrid::kernel::activity::IoImpl::resume()
34 {
35   if (surf_action_ != nullptr)
36     surf_action_->resume();
37 }
38
39 double simgrid::kernel::activity::IoImpl::get_remaining()
40 {
41   return surf_action_ ? surf_action_->get_remains() : 0;
42 }
43
44 void simgrid::kernel::activity::IoImpl::post()
45 {
46   for (smx_simcall_t const& simcall : simcalls_) {
47     switch (simcall->call) {
48       case SIMCALL_STORAGE_WRITE:
49         simcall_storage_write__set__result(simcall, surf_action_->get_cost());
50         break;
51       case SIMCALL_STORAGE_READ:
52         simcall_storage_read__set__result(simcall, surf_action_->get_cost());
53         break;
54       default:
55         break;
56     }
57   }
58
59   switch (surf_action_->get_state()) {
60     case simgrid::kernel::resource::Action::State::FAILED:
61       state_ = SIMIX_FAILED;
62       break;
63     case simgrid::kernel::resource::Action::State::FINISHED:
64       state_ = SIMIX_DONE;
65       break;
66     default:
67       THROW_IMPOSSIBLE;
68       break;
69   }
70
71   SIMIX_io_finish(this);
72 }