Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
da8f12d8254a80b3a2e7f4ccee141bae44c86ce7
[simgrid.git] / src / kernel / activity / ActivityImpl.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 "src/kernel/activity/ActivityImpl.hpp"
7
8 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
9
10 namespace simgrid {
11 namespace kernel {
12 namespace activity {
13
14 ActivityImpl::~ActivityImpl()
15 {
16   clean_action();
17   XBT_DEBUG("Destroy activity %p", this);
18 }
19
20 void ActivityImpl::clean_action()
21 {
22   if (surf_action_) {
23     surf_action_->unref();
24     surf_action_ = nullptr;
25   }
26 }
27
28 double ActivityImpl::get_remaining() const
29 {
30   return surf_action_ ? surf_action_->get_remains() : 0;
31 }
32
33 void ActivityImpl::suspend()
34 {
35   if (surf_action_ == nullptr)
36     return;
37   XBT_VERB("This activity is suspended (remain: %f)", surf_action_->get_remains());
38   surf_action_->suspend();
39   on_suspended(*this);
40 }
41
42 void ActivityImpl::resume()
43 {
44   if (surf_action_ == nullptr)
45     return;
46   XBT_VERB("This activity is resumed (remain: %f)", surf_action_->get_remains());
47   surf_action_->resume();
48   on_resumed(*this);
49 }
50
51 void ActivityImpl::cancel()
52 {
53   XBT_VERB("Activity %p is canceled", this);
54   if (surf_action_ != nullptr)
55     surf_action_->cancel();
56 }
57
58 // boost::intrusive_ptr<Activity> support:
59 void intrusive_ptr_add_ref(simgrid::kernel::activity::ActivityImpl* activity)
60 {
61   activity->refcount_.fetch_add(1, std::memory_order_relaxed);
62 }
63
64 void intrusive_ptr_release(simgrid::kernel::activity::ActivityImpl* activity)
65 {
66   if (activity->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
67     std::atomic_thread_fence(std::memory_order_acquire);
68     delete activity;
69   }
70 }
71 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_resumed;
72 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_suspended;
73 }
74 }
75 } // namespace simgrid::kernel::activity::