Logo AND Algorithmique Numérique Distribuée

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