/
hash_store.cc
61 lines (52 loc) · 1.47 KB
/
hash_store.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "gloo/rendezvous/hash_store.h"
#include "gloo/common/error.h"
#include "gloo/common/logging.h"
namespace gloo {
namespace rendezvous {
void HashStore::set(const std::string& key, const std::vector<char>& data) {
std::unique_lock<std::mutex> lock(m_);
GLOO_ENFORCE(map_.find(key) == map_.end(), "Key '", key, "' already set");
map_[key] = data;
cv_.notify_all();
}
std::vector<char> HashStore::get(const std::string& key) {
std::unique_lock<std::mutex> lock(m_);
auto it = map_.find(key);
if (it == map_.end()) {
return std::vector<char>();
}
return it->second;
}
void HashStore::wait(
const std::vector<std::string>& keys,
const std::chrono::milliseconds& timeout) {
const auto end = std::chrono::steady_clock::now() + timeout;
auto pred = [&](){
auto done = true;
for (const auto& key : keys) {
if (map_.find(key) == map_.end()) {
done = false;
break;
}
}
return done;
};
std::unique_lock<std::mutex> lock(m_);
if (timeout == kNoTimeout) {
cv_.wait(lock, pred);
} else {
if (!cv_.wait_until(lock, end, pred)) {
GLOO_THROW_IO_EXCEPTION(GLOO_ERROR_MSG(
"Wait timeout for key(s): ", ::gloo::MakeString(keys)));
}
}
}
} // namespace rendezvous
} // namespace gloo