Line data Source code
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 : 14 1222691 : LifetimeRefBase::LifetimeRefBase(LifetimeActor *actor) 15 1222691 : : ref_(this, actor) { 16 1222726 : } 17 : 18 1223079 : LifetimeRefBase::~LifetimeRefBase() { 19 1223079 : } 20 : 21 1034899 : LifetimeActor::LifetimeActor(LifetimeManager *manager) 22 1034831 : : manager_(manager), refcount_(0), shutdown_invoked_(false), 23 1034831 : delete_paused_(false), 24 1034831 : create_time_stamp_usecs_(UTCTimestampUsec()), 25 2069914 : delete_time_stamp_usecs_(0) { 26 1034858 : deleted_ = false; 27 1035117 : } 28 : 29 1035135 : LifetimeActor::~LifetimeActor() { 30 1035135 : assert(refcount_ == 0); 31 1035135 : assert(dependents_.empty()); 32 1035135 : } 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 : // 41 1057950 : void LifetimeActor::Delete() { 42 1057950 : std::scoped_lock lock(mutex_); 43 1057951 : if (deleted_.exchange(true)) { 44 24366 : return; 45 : } 46 1033585 : delete_time_stamp_usecs_ = UTCTimestampUsec(); 47 1033585 : refcount_++; 48 1033585 : manager_->EnqueueNoIncrement(this); 49 1057951 : } 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 : // 60 1033585 : void LifetimeActor::PropagateDelete() { 61 1033585 : assert(deleted_); 62 1033585 : std::scoped_lock lock(mutex_); 63 1033585 : for (Dependents::iterator iter = dependents_.begin(); 64 2164385 : iter != dependents_.end(); ++iter) { 65 1130800 : iter->Delete(); 66 : } 67 1033585 : } 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 : // 74 448270 : void LifetimeActor::RetryDelete() { 75 448270 : assert(deleted_); 76 448272 : manager_->Enqueue(this); 77 448280 : } 78 : 79 : // 80 : // Concurrency: called in the context of the LifetimeManager's Task. 81 : // 82 : // Called immediately before the object is destroyed. 83 : // 84 1033583 : void LifetimeActor::DeleteComplete() { 85 1033583 : } 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 : // 95 171804 : void LifetimeActor::Shutdown() { 96 171804 : } 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 : // 104 2617 : void LifetimeActor::PauseDelete() { 105 2617 : std::scoped_lock lock(mutex_); 106 2617 : assert(!deleted_); 107 2617 : delete_paused_ = true; 108 2617 : } 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 : // 116 2617 : void LifetimeActor::ResumeDelete() { 117 2617 : std::scoped_lock lock(mutex_); 118 2617 : assert(deleted_); 119 2617 : delete_paused_ = false; 120 2617 : refcount_++; 121 2617 : manager_->EnqueueNoIncrement(this); 122 2617 : } 123 : 124 : // 125 : // Concurrency: called in the context of any Task. 126 : // 127 : // Add the LifetimeRef as a dependent for this Actor. 128 : // 129 1210444 : void LifetimeActor::DependencyAdd( 130 : DependencyRef<LifetimeRefBase, LifetimeActor> *node) { 131 1210444 : std::scoped_lock lock(mutex_); 132 1210531 : assert(!deleted_); 133 1210469 : dependents_.Add(node); 134 1210306 : } 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 : // 143 1210633 : void LifetimeActor::DependencyRemove( 144 : DependencyRef<LifetimeRefBase, LifetimeActor> *node) { 145 1210633 : std::scoped_lock lock(mutex_); 146 1210634 : dependents_.Remove(node); 147 1210632 : if (deleted_ && dependents_.empty()) { 148 358512 : refcount_++; 149 358512 : manager_->EnqueueNoIncrement(this); 150 : } 151 1210632 : } 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. 156 448271 : void LifetimeActor::ReferenceIncrement() { 157 448271 : std::scoped_lock lock(mutex_); 158 448280 : refcount_++; 159 448280 : } 160 : 161 1842996 : bool LifetimeActor::ReferenceDecrementAndTest() { 162 1842996 : std::scoped_lock lock(mutex_); 163 1842996 : refcount_--; 164 2908740 : return (refcount_ == 0 && dependents_.empty() && !delete_paused_ && 165 2908740 : MayDelete()); 166 1842996 : } 167 : 168 17440 : LifetimeManager::LifetimeManager(int task_id) 169 17440 : : defer_count_(0), 170 17440 : queue_(task_id, 0, 171 17440 : boost::bind(&LifetimeManager::DeleteExecutor, this, _1)) { 172 17440 : queue_.set_name("LifetimeManager"); 173 17440 : } 174 : 175 22946 : LifetimeManager::~LifetimeManager() { 176 17425 : queue_.Shutdown(); 177 22946 : } 178 : 179 : // 180 : // Disable/Enable the WorkQueue - testing only. 181 : // 182 96 : void LifetimeManager::SetQueueDisable(bool disabled) { 183 96 : queue_.set_disable(disabled); 184 96 : } 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 : // 191 448272 : void LifetimeManager::Enqueue(LifetimeActor *actor) { 192 : LifetimeActorRef actor_ref; 193 448272 : actor->ReferenceIncrement(); 194 448278 : actor_ref.actor = actor; 195 448278 : queue_.Enqueue(actor_ref); 196 448280 : } 197 : 198 1920615 : void LifetimeManager::EnqueueNoIncrement(LifetimeActor *actor) { 199 : LifetimeActorRef actor_ref; 200 1920615 : actor_ref.actor = actor; 201 1920615 : queue_.Enqueue(actor_ref); 202 1920617 : } 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 : // 217 2368897 : bool LifetimeManager::DeleteExecutor(LifetimeActorRef actor_ref) { 218 2368897 : LifetimeActor *actor = actor_ref.actor; 219 2368897 : assert(actor->IsDeleted()); 220 2368897 : if (!actor->shutdown_invoked()) { 221 1033585 : actor->PropagateDelete(); 222 1033585 : actor->Shutdown(); 223 1033585 : actor->set_shutdown_invoked(); 224 : } 225 2368897 : if (!MayDestroy()) { 226 525901 : EnqueueNoIncrement(actor); 227 525901 : defer_count_++; 228 525901 : return false; 229 : } 230 1842996 : if (actor->ReferenceDecrementAndTest()) { 231 1033583 : actor->DeleteComplete(); 232 1033583 : actor->Destroy(); 233 : } 234 1842996 : return true; 235 : }