Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Objectify Semaphore kernel counterpart
[simgrid.git] / src / simix / smx_io.cpp
1 /* Copyright (c) 2007-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 "mc/mc.h"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "simgrid/s4u/Io.hpp"
10
11 #include "smx_private.hpp"
12 #include "src/kernel/activity/IoImpl.hpp"
13 #include "src/mc/mc_replay.hpp"
14 #include "src/simix/smx_io_private.hpp"
15 #include "src/surf/StorageImpl.hpp"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
18
19 void simcall_HANDLER_io_wait(smx_simcall_t simcall, smx_activity_t synchro)
20 {
21   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (int)synchro->state_);
22
23   /* Associate this simcall to the synchro */
24   synchro->simcalls_.push_back(simcall);
25   simcall->issuer->waiting_synchro = synchro;
26
27   /* set surf's synchro */
28   if (MC_is_active() || MC_record_replay_is_active()) {
29     synchro->state_ = SIMIX_DONE;
30     SIMIX_io_finish(synchro);
31     return;
32   }
33
34   /* If the synchro is already finished then perform the error handling */
35   if (synchro->state_ != SIMIX_RUNNING)
36     SIMIX_io_finish(synchro);
37 }
38
39 void SIMIX_io_finish(smx_activity_t synchro)
40 {
41   for (smx_simcall_t const& simcall : synchro->simcalls_) {
42     switch (synchro->state_) {
43       case SIMIX_DONE:
44         /* do nothing, synchro done */
45         break;
46       case SIMIX_FAILED:
47         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
48         break;
49       case SIMIX_CANCELED:
50         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
51         break;
52       default:
53         xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state_));
54     }
55
56     simcall->issuer->waiting_synchro = nullptr;
57     if (simcall->issuer->host_->is_on())
58       SIMIX_simcall_answer(simcall);
59     else
60       simcall->issuer->context_->iwannadie = true;
61   }
62 }