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 "db/db_entry.h" 7 : 8 : #include <tbb/mutex.h> 9 : 10 : #include "base/time_util.h" 11 : #include "db/db_table_partition.h" 12 : 13 : using namespace std; 14 : 15 9783830 : DBEntryBase::DBEntryBase() 16 9783830 : : tpart_(NULL), flags(0), last_change_at_(UTCTimestampUsec()) { 17 9784348 : onremoveq_ = false; 18 9785115 : } 19 : 20 9777263 : DBEntryBase::~DBEntryBase() { 21 9777263 : } 22 : 23 3753027 : void DBEntryBase::SetState(DBTableBase *tbl_base, ListenerId listener, 24 : DBState *state) { 25 3753027 : DBTablePartBase *tpart = tbl_base->GetTablePartition(this); 26 3753008 : tbb::spin_rw_mutex::scoped_lock lock(tpart->dbstate_mutex(), true); 27 3755396 : pair<StateMap::iterator, bool> res = state_.insert( 28 3754940 : make_pair(listener, state)); 29 3755133 : if (!res.second) { 30 1228677 : res.first->second = state; 31 : } else { 32 2526456 : assert(!IsDeleted()); 33 : // Account for state addition for this listener. 34 2526455 : tbl_base->AddToDBStateCount(listener, 1); 35 : } 36 3754800 : } 37 : 38 12447382 : DBState *DBEntryBase::GetState(DBTableBase *tbl_base, ListenerId listener) const { 39 12447382 : DBTablePartBase *tpart = tbl_base->GetTablePartition(this); 40 12442476 : tbb::spin_rw_mutex::scoped_lock lock(tpart->dbstate_mutex(), false); 41 12459425 : StateMap::const_iterator loc = state_.find(listener); 42 12437353 : if (loc != state_.end()) { 43 7976438 : return loc->second; 44 : } 45 4465414 : return NULL; 46 12441297 : } 47 : 48 763 : const DBState *DBEntryBase::GetState(const DBTableBase *tbl_base, 49 : ListenerId listener) const { 50 763 : DBTableBase *table = const_cast<DBTableBase *>(tbl_base); 51 763 : DBTablePartBase *tpart = table->GetTablePartition(this); 52 763 : tbb::spin_rw_mutex::scoped_lock lock(tpart->dbstate_mutex(), false); 53 763 : StateMap::const_iterator loc = state_.find(listener); 54 763 : if (loc != state_.end()) { 55 736 : return loc->second; 56 : } 57 27 : return NULL; 58 763 : } 59 : 60 : // 61 : // Concurrency: called from arbitrary task. 62 : // 63 : // Evaluate concurrency issues with DBTablePartBase::RunNotify when making 64 : // changes to this method. We expect that either this method or RunNotify 65 : // is responsible for removing the DBEntryBase when they run concurrently, 66 : // assuming the DBEntryBase is eligible for removal. The dbstate_mutex in 67 : // in DBTablePartBase is used for synchronization. 68 : // 69 : // Remove DBState on this DBEntryBase for the given listener and enqueue 70 : // for removal if appropriate. 71 : // Note that the entry cannot be removed from DBTablePartBase here since 72 : // this method may be called from an arbitrary Task. 73 : // 74 2525635 : void DBEntryBase::ClearState(DBTableBase *tbl_base, ListenerId listener) { 75 2525635 : DBTablePartBase *tpart = tbl_base->GetTablePartition(this); 76 2525476 : tbb::spin_rw_mutex::scoped_lock lock(tpart->dbstate_mutex(), true); 77 : 78 2526313 : assert(state_.erase(listener) != 0); 79 : 80 : // Account for state removal for this listener. 81 2525613 : tbl_base->AddToDBStateCount(listener, -1); 82 : 83 2526393 : if (state_.empty() && IsDeleted() && !is_onlist() && !IsOnRemoveQ()) { 84 523884 : tbl_base->EnqueueRemove(this); 85 : } 86 2526354 : } 87 : 88 1070385 : bool DBEntryBase::is_state_empty(DBTablePartBase *tpart) { 89 1070385 : tbb::spin_rw_mutex::scoped_lock lock(tpart->dbstate_mutex(), false); 90 2140694 : return state_.empty(); 91 1070229 : } 92 : 93 523864 : bool DBEntryBase::is_state_empty_unlocked(DBTablePartBase *tpart) { 94 523864 : return state_.empty(); 95 : } 96 : 97 2195933 : void DBEntryBase::set_last_change_at_to_now() { 98 2195933 : last_change_at_ = UTCTimestampUsec(); 99 2196069 : } 100 : 101 0 : void DBEntryBase::set_last_change_at(uint64_t time) { 102 0 : last_change_at_ = time; 103 0 : } 104 : 105 2328666 : void DBEntryBase::set_last_update_at_to_now() { 106 2328666 : last_update_at_ = UTCTimestampUsec(); 107 2328702 : } 108 : 109 152 : void DBEntryBase::set_last_update_at(uint64_t time) { 110 152 : last_update_at_ = time; 111 152 : } 112 1448849 : void DBEntryBase::set_table_partition(DBTablePartBase *tpart) { 113 1448849 : tpart_ = tpart; 114 1448849 : } 115 : 116 3256233 : DBTablePartBase *DBEntryBase::get_table_partition() const { 117 3256233 : return tpart_; 118 : } 119 : 120 9770152 : DBTableBase *DBEntryBase::get_table() const { 121 9770152 : return (tpart_ ? tpart_->parent() : NULL); 122 : } 123 : 124 0 : const std::string DBEntryBase::last_change_at_str() const { 125 0 : return duration_usecs_to_string(UTCTimestampUsec() - last_change_at_); 126 : } 127 : 128 60152 : void DBEntryBase::Notify() { 129 60152 : tpart_->Notify(this); 130 60152 : } 131 : 132 48080 : void DBEntryBase::Delete() { 133 48080 : if (!IsDeleted()) 134 48080 : tpart_->Delete(this); 135 48080 : }