| 47 | } |
| 48 | |
| 49 | int64_t FacebookBase::incrementCounter(const std::string& key, int64_t amount) { |
| 50 | counters_.lock(); |
| 51 | |
| 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()) { |
| 55 | |
| 56 | // we need to check again to make sure someone didn't create this key |
| 57 | // already while we released the lock |
| 58 | it = counters_.find(key); |
| 59 | if(it == counters_.end()){ |
| 60 | counters_[key].value = amount; |
| 61 | counters_.unlock(); |
| 62 | return amount; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | it->second.lock(); |
| 67 | int64_t count = it->second.value + amount; |
| 68 | it->second.value = count; |
| 69 | it->second.unlock(); |
| 70 | counters_.unlock(); |
| 71 | return count; |
| 72 | } |
| 73 | |
| 74 | int64_t FacebookBase::setCounter(const std::string& key, int64_t value) { |
| 75 | counters_.lock(); |
no test coverage detected