OpenSDN source code
lifetime.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "base/lifetime.h"
7 
8 #include <boost/bind/bind.hpp>
9 #include "base/backtrace.h"
10 #include "base/time_util.h"
11 
12 using namespace boost::placeholders;
13 
15  : ref_(this, actor) {
16 }
17 
19 }
20 
22  : manager_(manager), refcount_(0), shutdown_invoked_(false),
23  delete_paused_(false),
24  create_time_stamp_usecs_(UTCTimestampUsec()),
25  delete_time_stamp_usecs_(0) {
26  deleted_ = false;
27 }
28 
30  assert(refcount_ == 0);
31  assert(dependents_.empty());
32 }
33 
34 //
35 // Concurrency: called in the context of any Task or the main thread.
36 //
37 // Used to trigger delete for managed object and it's dependents. Enqueue
38 // this actor to the Lifetime Manager. Propagation of the delete operation
39 // to dependents happens in the context of the LifetimeManager's Task.
40 //
42  std::scoped_lock lock(mutex_);
43  if (deleted_.exchange(true)) {
44  return;
45  }
47  refcount_++;
49 }
50 
51 //
52 // Concurrency: called in the context of the LifetimeManager's Task.
53 //
54 // Used to propagate delete for a managed object to it's dependents.
55 //
56 // Walk the list of dependent LifetimeRefs and propagate the delete. A mutex is
57 // used to ensure that the dependent list does not change while we are walking
58 // through it.
59 //
61  assert(deleted_);
62  std::scoped_lock lock(mutex_);
64  iter != dependents_.end(); ++iter) {
65  iter->Delete();
66  }
67 }
68 
69 //
70 // Concurrency: called in the context of any Task or the main thread.
71 //
72 // Enqueue a delete event for this actor to the LifetimeManager.
73 //
75  assert(deleted_);
76  manager_->Enqueue(this);
77 }
78 
79 //
80 // Concurrency: called in the context of the LifetimeManager's Task.
81 //
82 // Called immediately before the object is destroyed.
83 //
85 }
86 
87 //
88 // Concurrency: called in the context of the LifetimeManager's Task.
89 //
90 // Can be called multiple times - when the managed object is initially deleted
91 // and whenever a delete event is enqueued to the Lifetime Manager. The latter
92 // happens when the list of dependents becomes empty or the refcount of the
93 // lightweight dependents goes to 0.
94 //
96 }
97 
98 //
99 // Concurrency: called in the context of main thread.
100 // TaskScheduler should be stopped prior to invoking this method.
101 //
102 // Prevent object from getting destroyed - testing only.
103 //
105  std::scoped_lock lock(mutex_);
106  assert(!deleted_);
107  delete_paused_ = true;
108 }
109 
110 //
111 // Concurrency: called in the context of main thread.
112 // TaskScheduler should be stopped prior to invoking this method.
113 //
114 // Allow object to get destroyed - testing only.
115 //
117  std::scoped_lock lock(mutex_);
118  assert(deleted_);
119  delete_paused_ = false;
120  refcount_++;
122 }
123 
124 //
125 // Concurrency: called in the context of any Task.
126 //
127 // Add the LifetimeRef as a dependent for this Actor.
128 //
131  std::scoped_lock lock(mutex_);
132  assert(!deleted_);
133  dependents_.Add(node);
134 }
135 
136 //
137 // Concurrency: called in the context of the LifetimeManager's Task.
138 //
139 // Remove the LifetimeRef as a dependent of this Actor. Note that this can
140 // happen when the dependent object itself is being deleted i.e. this actor
141 // itself need not be marked deleted.
142 //
145  std::scoped_lock lock(mutex_);
146  dependents_.Remove(node);
147  if (deleted_ && dependents_.empty()) {
148  refcount_++;
150  }
151 }
152 
153 // When the actor is placed in the queue the caller must still hold an
154 // "lock" on the object in the form of either a dependency or an
155 // explicit test performed by the derived class MayDelete() method.
157  std::scoped_lock lock(mutex_);
158  refcount_++;
159 }
160 
162  std::scoped_lock lock(mutex_);
163  refcount_--;
164  return (refcount_ == 0 && dependents_.empty() && !delete_paused_ &&
165  MayDelete());
166 }
167 
169  : defer_count_(0),
170  queue_(task_id, 0,
171  boost::bind(&LifetimeManager::DeleteExecutor, this, _1)) {
172  queue_.set_name("LifetimeManager");
173 }
174 
176  queue_.Shutdown();
177 }
178 
179 //
180 // Disable/Enable the WorkQueue - testing only.
181 //
182 void LifetimeManager::SetQueueDisable(bool disabled) {
183  queue_.set_disable(disabled);
184 }
185 
186 //
187 // Concurrency: called in the context of any Task or the main thread.
188 //
189 // Enqueue a delete event for the actor.
190 //
192  LifetimeActorRef actor_ref;
193  actor->ReferenceIncrement();
194  actor_ref.actor = actor;
195  queue_.Enqueue(actor_ref);
196 }
197 
199  LifetimeActorRef actor_ref;
200  actor_ref.actor = actor;
201  queue_.Enqueue(actor_ref);
202 }
203 
204 //
205 // Concurrency: called in the context of the LifetimeManager's Task.
206 //
207 // If this is the first time that the delete actor is being processed, we
208 // propagate the delete to it's dependents and ask the managed object to
209 // shut itself i.e. take care of cleaning up any state not represented as
210 // an explicit LifetimeRef dependent.
211 //
212 // If global conditions for object destruction are not satisfied, enqueue
213 // the delete actor again and defer processing of the queue. Do not bump
214 // up the refcount in this case.
215 // Else go ahead and destroy the object if all conditions are satisfied.
216 //
218  LifetimeActor *actor = actor_ref.actor;
219  assert(actor->IsDeleted());
220  if (!actor->shutdown_invoked()) {
221  actor->PropagateDelete();
222  actor->Shutdown();
223  actor->set_shutdown_invoked();
224  }
225  if (!MayDestroy()) {
226  EnqueueNoIncrement(actor);
227  defer_count_++;
228  return false;
229  }
230  if (actor->ReferenceDecrementAndTest()) {
231  actor->DeleteComplete();
232  actor->Destroy();
233  }
234  return true;
235 }
void Add(DependencyRef< NodeType, ObjectType > *node)
Definition: dependency.h:99
void Remove(DependencyRef< NodeType, ObjectType > *node)
Definition: dependency.h:103
iterator end()
Definition: dependency.h:117
bool empty() const
Definition: dependency.h:125
iterator begin()
Definition: dependency.h:116
IteratorBase< LifetimeRefBase, typename List::iterator > iterator
Definition: dependency.h:93
LifetimeManager * manager_
Definition: lifetime.h:171
std::mutex mutex_
Definition: lifetime.h:170
virtual void Shutdown()
Definition: lifetime.cc:95
virtual ~LifetimeActor()
Definition: lifetime.cc:29
virtual void Delete()
Definition: lifetime.cc:41
virtual bool MayDelete() const =0
bool shutdown_invoked()
Definition: lifetime.h:167
void PropagateDelete()
Definition: lifetime.cc:60
bool IsDeleted() const
Definition: lifetime.h:132
void DependencyAdd(DependencyRef< LifetimeRefBase, LifetimeActor > *node)
Definition: lifetime.cc:129
bool ReferenceDecrementAndTest()
Definition: lifetime.cc:161
virtual void Destroy()=0
void RetryDelete()
Definition: lifetime.cc:74
void ReferenceIncrement()
Definition: lifetime.cc:156
void set_shutdown_invoked()
Definition: lifetime.h:168
LifetimeActor(LifetimeManager *manager)
Definition: lifetime.cc:21
virtual void DeleteComplete()
Definition: lifetime.cc:84
std::atomic< bool > deleted_
Definition: lifetime.h:172
void PauseDelete()
Definition: lifetime.cc:104
Dependents dependents_
Definition: lifetime.h:178
void DependencyRemove(DependencyRef< LifetimeRefBase, LifetimeActor > *node)
Definition: lifetime.cc:143
void ResumeDelete()
Definition: lifetime.cc:116
uint64_t delete_time_stamp_usecs_
Definition: lifetime.h:177
bool delete_paused_
Definition: lifetime.h:175
void EnqueueNoIncrement(LifetimeActor *actor)
Definition: lifetime.cc:198
virtual void SetQueueDisable(bool disabled)
Definition: lifetime.cc:182
virtual ~LifetimeManager()
Definition: lifetime.cc:175
WorkQueue< LifetimeActorRef > queue_
Definition: lifetime.h:221
virtual bool MayDestroy()
Definition: lifetime.h:216
LifetimeManager(int task_id)
Definition: lifetime.cc:168
bool DeleteExecutor(LifetimeActorRef actor_ref)
Definition: lifetime.cc:217
void Enqueue(LifetimeActor *actor)
Definition: lifetime.cc:191
LifetimeRefBase(LifetimeActor *actor)
Definition: lifetime.cc:14
virtual ~LifetimeRefBase()
Definition: lifetime.cc:18
static uint64_t UTCTimestampUsec()
Definition: time_util.h:14