Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d1bc5df85c4fbc4473a163e1c834c24cce4bbf7e
[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   return this;
27 }
28
29 Activity* Exec::wait(double timeout)
30 {
31   THROW_UNIMPLEMENTED;
32   return this;
33 }
34
35 bool Exec::test()
36 {
37   xbt_assert(state_ == inited || state_ == started || state_ == finished);
38
39   if (state_ == finished) {
40     return true;
41   }
42
43   if (state_ == inited) {
44     this->start();
45   }
46
47   if (simcall_execution_test(pimpl_)) {
48     state_ = finished;
49     return true;
50   }
51
52   return false;
53 }
54
55 ExecPtr Exec::setPriority(double priority)
56 {
57   xbt_assert(state_ == inited, "Cannot change the priority of an exec after its start");
58   priority_ = priority;
59   return this;
60 }
61 ExecPtr Exec::setHost(Host* host)
62 {
63   xbt_assert(state_ == inited, "Cannot change the host of an exec after its start");
64   host_ = host;
65   return this;
66 }
67
68 double Exec::getRemains()
69 {
70   return simgrid::simix::kernelImmediate(
71       [this]() { return boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->remains(); });
72 }
73 double Exec::getRemainingRatio()
74 {
75   return simgrid::simix::kernelImmediate(
76       [this]() { return boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->remainingRatio(); });
77 }
78
79 void intrusive_ptr_release(simgrid::s4u::Exec* e)
80 {
81   if (e->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
82     std::atomic_thread_fence(std::memory_order_acquire);
83     delete e;
84   }
85 }
86
87 void intrusive_ptr_add_ref(simgrid::s4u::Exec* e)
88 {
89   e->refcount_.fetch_add(1, std::memory_order_relaxed);
90 }
91 }
92 }