Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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.);
19   state_ = started;
20 }
21
22 void Exec::wait()
23 {
24   this->wait(-1);
25 }
26
27 void Exec::wait(double timeout)
28 {
29   simcall_execution_wait(pimpl_);
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   priority_ = priority;
50   return this;
51 }
52
53 double Exec::getRemains()
54 {
55   return simgrid::simix::kernelImmediate(
56       [this]() { return boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(pimpl_)->remains(); });
57 }
58
59 void intrusive_ptr_release(simgrid::s4u::Exec* e)
60 {
61   if (e->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
62     std::atomic_thread_fence(std::memory_order_acquire);
63     delete e;
64   }
65 }
66
67 void intrusive_ptr_add_ref(simgrid::s4u::Exec* e)
68 {
69   e->refcount_.fetch_add(1, std::memory_order_relaxed);
70 }
71 }
72 }