Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
allow to cancel a s4u::Exec
[simgrid.git] / src / simix / smx_io.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 "simgrid/s4u/Host.hpp"
7 #include "xbt/ex.hpp"
8
9 #include "smx_private.hpp"
10 #include "src/kernel/activity/IoImpl.hpp"
11 #include "src/simix/smx_io_private.hpp"
12 #include "src/surf/StorageImpl.hpp"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
15
16 void simcall_HANDLER_storage_read(smx_simcall_t simcall, surf_storage_t st, sg_size_t size)
17 {
18   smx_activity_t synchro = SIMIX_storage_read(st, size);
19   synchro->simcalls_.push_back(simcall);
20   simcall->issuer->waiting_synchro = synchro;
21 }
22
23 smx_activity_t SIMIX_storage_read(surf_storage_t st, sg_size_t size)
24 {
25   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
26   synchro->surf_action_                      = st->read(size);
27
28   synchro->surf_action_->set_data(synchro);
29   XBT_DEBUG("Create io synchro %p", synchro);
30
31   return synchro;
32 }
33
34 void simcall_HANDLER_storage_write(smx_simcall_t simcall, surf_storage_t st, sg_size_t size)
35 {
36   smx_activity_t synchro = SIMIX_storage_write(st, size);
37   synchro->simcalls_.push_back(simcall);
38   simcall->issuer->waiting_synchro = synchro;
39 }
40
41 smx_activity_t SIMIX_storage_write(surf_storage_t st, sg_size_t size)
42 {
43   simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
44   synchro->surf_action_                      = st->write(size);
45   synchro->surf_action_->set_data(synchro);
46   XBT_DEBUG("Create io synchro %p", synchro);
47
48   return synchro;
49 }
50
51 void SIMIX_io_destroy(smx_activity_t synchro)
52 {
53   simgrid::kernel::activity::IoImplPtr io = boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(synchro);
54   XBT_DEBUG("Destroy synchro %p", synchro.get());
55   if (io->surf_action_)
56     io->surf_action_->unref();
57 }
58
59 void SIMIX_io_finish(smx_activity_t synchro)
60 {
61   for (smx_simcall_t const& simcall : synchro->simcalls_) {
62     switch (synchro->state_) {
63       case SIMIX_DONE:
64         /* do nothing, synchro done */
65         break;
66       case SIMIX_FAILED:
67         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
68         break;
69       case SIMIX_CANCELED:
70         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
71         break;
72       default:
73         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state_));
74     }
75
76     if (simcall->issuer->host_->is_off()) {
77       simcall->issuer->context_->iwannadie = 1;
78     }
79
80     simcall->issuer->waiting_synchro = nullptr;
81     SIMIX_simcall_answer(simcall);
82   }
83
84   /* We no longer need it */
85   SIMIX_io_destroy(synchro);
86 }