Logo AND Algorithmique Numérique Distribuée

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