Logo AND Algorithmique Numérique Distribuée

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