Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
allow to call s4u::Exec->setHost() after its start, to migrate it
[simgrid.git] / src / s4u / s4u_exec.cpp
1 /* Copyright (c) 2006-2017. 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 #include "xbt/log.h"
6
7 #include "simgrid/s4u/Actor.hpp"
8 #include "simgrid/s4u/Exec.hpp"
9 #include "src/kernel/activity/ExecImpl.hpp"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_exec, s4u_activity, "S4U asynchronous executions");
12
13 namespace simgrid {
14 namespace s4u {
15
16 Activity* Exec::start()
17 {
18   pimpl_ = simcall_execution_start(nullptr, flops_amount_, 1 / priority_, 0., host_);
19   state_ = started;
20   return this;
21 }
22
23 Activity* Exec::wait()
24 {
25   simcall_execution_wait(pimpl_);
26   state_ = finished;
27   return this;
28 }
29
30 Activity* Exec::wait(double timeout)
31 {
32   THROW_UNIMPLEMENTED;
33   return this;
34 }
35
36 bool Exec::test()
37 {
38   xbt_assert(state_ == inited || state_ == started || state_ == finished);
39
40   if (state_ == finished) {
41     return true;
42   }
43
44   if (state_ == inited) {
45     this->start();
46   }
47
48   if (simcall_execution_test(pimpl_)) {
49     state_ = finished;
50     return true;
51   }
52
53   return false;
54 }
55
56 ExecPtr Exec::setPriority(double priority)
57 {
58   xbt_assert(state_ == inited, "Cannot change the priority of an exec after its start");
59   priority_ = priority;
60   return this;
61 }
62 ExecPtr Exec::setHost(Host* host)
63 {
64   xbt_assert(state_ == inited || state_ == started, "Cannot change the host of an exec once it's done (state: %d)", state_);
65   if (state_ == started)
66     boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->migrate(host);
67   host_ = host;
68   return this;
69 }
70
71 double Exec::getRemains()
72 {
73   return simgrid::simix::kernelImmediate(
74       [this]() { return boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->remains(); });
75 }
76 double Exec::getRemainingRatio()
77 {
78   return simgrid::simix::kernelImmediate(
79       [this]() { return boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->remainingRatio(); });
80 }
81
82 void intrusive_ptr_release(simgrid::s4u::Exec* e)
83 {
84   if (e->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
85     std::atomic_thread_fence(std::memory_order_acquire);
86     delete e;
87   }
88 }
89
90 void intrusive_ptr_add_ref(simgrid::s4u::Exec* e)
91 {
92   e->refcount_.fetch_add(1, std::memory_order_relaxed);
93 }
94 }
95 }