pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame^] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
| 7 | #include "FacebookBase.h" |
| 8 | |
| 9 | using namespace facebook::fb303; |
| 10 | using facebook::thrift::concurrency::Guard; |
| 11 | |
| 12 | FacebookBase::FacebookBase(std::string name, get_static_limref_ptr reflect_lim) : |
| 13 | name_(name) { |
| 14 | aliveSince_ = (int64_t) time(NULL); |
| 15 | if (reflect_lim) { |
| 16 | reflect_lim(reflection_limited_); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | inline void FacebookBase::getName(std::string& _return) { |
| 21 | _return = name_; |
| 22 | } |
| 23 | |
| 24 | void FacebookBase::setOption(const std::string& key, const std::string& value) { |
| 25 | Guard g(optionsLock_); |
| 26 | options_[key] = value; |
| 27 | } |
| 28 | |
| 29 | void FacebookBase::getOption(std::string& _return, const std::string& key) { |
| 30 | Guard g(optionsLock_); |
| 31 | _return = options_[key]; |
| 32 | } |
| 33 | |
| 34 | void FacebookBase::getOptions(std::map<std::string, std::string> & _return) { |
| 35 | Guard g(optionsLock_); |
| 36 | _return = options_; |
| 37 | } |
| 38 | |
| 39 | int64_t FacebookBase::incrementCounter(const std::string& key, int64_t amount) { |
| 40 | counters_.acquireRead(); |
| 41 | |
| 42 | // if we didn't find the key, we need to write lock the whole map to create it |
| 43 | ReadWriteCounterMap::iterator it = counters_.find(key); |
| 44 | if (it == counters_.end()) { |
| 45 | counters_.release(); |
| 46 | counters_.acquireWrite(); |
| 47 | |
| 48 | // we need to check again to make sure someone didn't create this key |
| 49 | // already while we released the lock |
| 50 | it = counters_.find(key); |
| 51 | if(it == counters_.end()){ |
| 52 | counters_[key].value = amount; |
| 53 | counters_.release(); |
| 54 | return amount; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | it->second.acquireWrite(); |
| 59 | int64_t count = it->second.value + amount; |
| 60 | it->second.value = count; |
| 61 | it->second.release(); |
| 62 | counters_.release(); |
| 63 | return count; |
| 64 | } |
| 65 | |
| 66 | int64_t FacebookBase::setCounter(const std::string& key, int64_t value) { |
| 67 | counters_.acquireRead(); |
| 68 | |
| 69 | // if we didn't find the key, we need to write lock the whole map to create it |
| 70 | ReadWriteCounterMap::iterator it = counters_.find(key); |
| 71 | if (it == counters_.end()) { |
| 72 | counters_.release(); |
| 73 | counters_.acquireWrite(); |
| 74 | counters_[key].value = value; |
| 75 | counters_.release(); |
| 76 | return value; |
| 77 | } |
| 78 | |
| 79 | it->second.acquireWrite(); |
| 80 | it->second.value = value; |
| 81 | it->second.release(); |
| 82 | counters_.release(); |
| 83 | return value; |
| 84 | } |
| 85 | |
| 86 | void FacebookBase::getCounters(std::map<std::string, int64_t>& _return) { |
| 87 | // we need to lock the whole thing and actually build the map since we don't |
| 88 | // want our read/write structure to go over the wire |
| 89 | counters_.acquireRead(); |
| 90 | for(ReadWriteCounterMap::iterator it = counters_.begin(); |
| 91 | it != counters_.end(); it++) |
| 92 | { |
| 93 | _return[it->first] = it->second.value; |
| 94 | } |
| 95 | counters_.release(); |
| 96 | } |
| 97 | |
| 98 | int64_t FacebookBase::getCounter(const std::string& key) { |
| 99 | int64_t rv = 0; |
| 100 | counters_.acquireRead(); |
| 101 | ReadWriteCounterMap::iterator it = counters_.find(key); |
| 102 | if (it != counters_.end()) { |
| 103 | it->second.acquireRead(); |
| 104 | rv = it->second.value; |
| 105 | it->second.release(); |
| 106 | } |
| 107 | counters_.release(); |
| 108 | return rv; |
| 109 | } |
| 110 | |
| 111 | inline int64_t FacebookBase::aliveSince() { |
| 112 | return aliveSince_; |
| 113 | } |
| 114 | |