Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modernize simcall_execution_test().
[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 bool ActivityImpl::test()
41 {
42   if (state_ != State::WAITING && state_ != State::RUNNING) {
43     finish();
44     return true;
45   }
46   return false;
47 }
48
49 void ActivityImpl::suspend()
50 {
51   if (surf_action_ == nullptr)
52     return;
53   XBT_VERB("This activity is suspended (remain: %f)", surf_action_->get_remains());
54   surf_action_->suspend();
55   on_suspended(*this);
56 }
57
58 void ActivityImpl::resume()
59 {
60   if (surf_action_ == nullptr)
61     return;
62   XBT_VERB("This activity is resumed (remain: %f)", surf_action_->get_remains());
63   surf_action_->resume();
64   on_resumed(*this);
65 }
66
67 void ActivityImpl::cancel()
68 {
69   XBT_VERB("Activity %p is canceled", this);
70   if (surf_action_ != nullptr)
71     surf_action_->cancel();
72   state_ = State::CANCELED;
73 }
74
75 // boost::intrusive_ptr<Activity> support:
76 void intrusive_ptr_add_ref(simgrid::kernel::activity::ActivityImpl* activity)
77 {
78   activity->refcount_.fetch_add(1, std::memory_order_relaxed);
79 }
80
81 void intrusive_ptr_release(simgrid::kernel::activity::ActivityImpl* activity)
82 {
83   if (activity->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
84     std::atomic_thread_fence(std::memory_order_acquire);
85     delete activity;
86   }
87 }
88 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_resumed;
89 xbt::signal<void(ActivityImpl const&)> ActivityImpl::on_suspended;
90 }
91 }
92 } // namespace simgrid::kernel::activity::