blob: d9421065c4c8e5a5592ca3db7c359a11f2942d74 [file] [log] [blame]
pwyckoff99b000b2008-04-03 19:30:55 +00001// 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
9using namespace facebook::fb303;
10using facebook::thrift::concurrency::Guard;
11
David Reiss0b7d6fa2009-02-07 02:36:35 +000012FacebookBase::FacebookBase(std::string name) :
pwyckoff99b000b2008-04-03 19:30:55 +000013 name_(name) {
14 aliveSince_ = (int64_t) time(NULL);
pwyckoff99b000b2008-04-03 19:30:55 +000015}
16
17inline void FacebookBase::getName(std::string& _return) {
18 _return = name_;
19}
20
21void FacebookBase::setOption(const std::string& key, const std::string& value) {
22 Guard g(optionsLock_);
23 options_[key] = value;
24}
25
26void FacebookBase::getOption(std::string& _return, const std::string& key) {
27 Guard g(optionsLock_);
28 _return = options_[key];
29}
30
31void FacebookBase::getOptions(std::map<std::string, std::string> & _return) {
32 Guard g(optionsLock_);
33 _return = options_;
34}
35
36int64_t FacebookBase::incrementCounter(const std::string& key, int64_t amount) {
37 counters_.acquireRead();
38
39 // if we didn't find the key, we need to write lock the whole map to create it
40 ReadWriteCounterMap::iterator it = counters_.find(key);
41 if (it == counters_.end()) {
42 counters_.release();
43 counters_.acquireWrite();
44
45 // we need to check again to make sure someone didn't create this key
46 // already while we released the lock
47 it = counters_.find(key);
48 if(it == counters_.end()){
49 counters_[key].value = amount;
50 counters_.release();
51 return amount;
52 }
53 }
54
55 it->second.acquireWrite();
56 int64_t count = it->second.value + amount;
57 it->second.value = count;
58 it->second.release();
59 counters_.release();
60 return count;
61}
62
63int64_t FacebookBase::setCounter(const std::string& key, int64_t value) {
64 counters_.acquireRead();
65
66 // if we didn't find the key, we need to write lock the whole map to create it
67 ReadWriteCounterMap::iterator it = counters_.find(key);
68 if (it == counters_.end()) {
69 counters_.release();
70 counters_.acquireWrite();
71 counters_[key].value = value;
72 counters_.release();
73 return value;
74 }
75
76 it->second.acquireWrite();
77 it->second.value = value;
78 it->second.release();
79 counters_.release();
80 return value;
81}
82
83void FacebookBase::getCounters(std::map<std::string, int64_t>& _return) {
84 // we need to lock the whole thing and actually build the map since we don't
85 // want our read/write structure to go over the wire
86 counters_.acquireRead();
87 for(ReadWriteCounterMap::iterator it = counters_.begin();
88 it != counters_.end(); it++)
89 {
90 _return[it->first] = it->second.value;
91 }
92 counters_.release();
93}
94
95int64_t FacebookBase::getCounter(const std::string& key) {
96 int64_t rv = 0;
97 counters_.acquireRead();
98 ReadWriteCounterMap::iterator it = counters_.find(key);
99 if (it != counters_.end()) {
100 it->second.acquireRead();
101 rv = it->second.value;
102 it->second.release();
103 }
104 counters_.release();
105 return rv;
106}
107
108inline int64_t FacebookBase::aliveSince() {
109 return aliveSince_;
110}
111