]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/contrib/fb303/cpp/FacebookBase.cpp
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 #include "FacebookBase.h"
22 using namespace facebook::fb303
;
23 using apache::thrift::concurrency::Guard
;
25 FacebookBase::FacebookBase(std::string name
) :
27 aliveSince_
= (int64_t) time(NULL
);
30 inline void FacebookBase::getName(std::string
& _return
) {
34 void FacebookBase::setOption(const std::string
& key
, const std::string
& value
) {
35 Guard
g(optionsLock_
);
36 options_
[key
] = value
;
39 void FacebookBase::getOption(std::string
& _return
, const std::string
& key
) {
40 Guard
g(optionsLock_
);
41 _return
= options_
[key
];
44 void FacebookBase::getOptions(std::map
<std::string
, std::string
> & _return
) {
45 Guard
g(optionsLock_
);
49 int64_t FacebookBase::incrementCounter(const std::string
& key
, int64_t amount
) {
50 counters_
.acquireRead();
52 // if we didn't find the key, we need to write lock the whole map to create it
53 ReadWriteCounterMap::iterator it
= counters_
.find(key
);
54 if (it
== counters_
.end()) {
56 counters_
.acquireWrite();
58 // we need to check again to make sure someone didn't create this key
59 // already while we released the lock
60 it
= counters_
.find(key
);
61 if(it
== counters_
.end()){
62 counters_
[key
].value
= amount
;
68 it
->second
.acquireWrite();
69 int64_t count
= it
->second
.value
+ amount
;
70 it
->second
.value
= count
;
76 int64_t FacebookBase::setCounter(const std::string
& key
, int64_t value
) {
77 counters_
.acquireRead();
79 // if we didn't find the key, we need to write lock the whole map to create it
80 ReadWriteCounterMap::iterator it
= counters_
.find(key
);
81 if (it
== counters_
.end()) {
83 counters_
.acquireWrite();
84 counters_
[key
].value
= value
;
89 it
->second
.acquireWrite();
90 it
->second
.value
= value
;
96 void FacebookBase::getCounters(std::map
<std::string
, int64_t>& _return
) {
97 // we need to lock the whole thing and actually build the map since we don't
98 // want our read/write structure to go over the wire
99 counters_
.acquireRead();
100 for(ReadWriteCounterMap::iterator it
= counters_
.begin();
101 it
!= counters_
.end(); ++it
)
103 _return
[it
->first
] = it
->second
.value
;
108 int64_t FacebookBase::getCounter(const std::string
& key
) {
110 counters_
.acquireRead();
111 ReadWriteCounterMap::iterator it
= counters_
.find(key
);
112 if (it
!= counters_
.end()) {
113 it
->second
.acquireRead();
114 rv
= it
->second
.value
;
115 it
->second
.release();
121 inline int64_t FacebookBase::aliveSince() {