| 25 | namespace cachelib { |
| 26 | namespace cachebench { |
| 27 | ShortThreadId ShortThreadIdMap::getShort(std::thread::id tid) { |
| 28 | { |
| 29 | std::shared_lock<folly::SharedMutex> lock{mutex_}; |
| 30 | auto iter = tids_.find(tid); |
| 31 | if (iter != tids_.end()) { |
| 32 | return iter->second; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // Now we have to insert a new TID. Take writer lock. Remember to check |
| 37 | // again if present! |
| 38 | std::lock_guard<folly::SharedMutex> lock{mutex_}; |
| 39 | auto iter = tids_.find(tid); |
| 40 | if (iter != tids_.end()) { |
| 41 | return iter->second; |
| 42 | } |
| 43 | auto size = tids_.size(); |
| 44 | if (size > std::numeric_limits<ShortThreadId>::max()) { |
| 45 | throw std::out_of_range("too many threads"); |
| 46 | } |
| 47 | // "first" is an iterator pointing to the inserted key/value pair |
| 48 | return tids_.emplace(tid, static_cast<ShortThreadId>(size)).first->second; |
| 49 | } |
| 50 | } // namespace cachebench |
| 51 | } // namespace cachelib |
| 52 | } // namespace facebook |