Derecho  0.9
Distributed systems toolkit for RDMA
poll_utils.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <numeric>
3 #include <functional>
4 
6 
7 namespace sst {
8 namespace util {
9 
10 //std::vector<std::list<std::pair<int32_t, int32_t>>> PollingData::completion_entries;
11 //std::map<std::thread::id, uint32_t> PollingData::tid_to_index;
12 //std::vector<bool> PollingData::if_waiting;
13 //std::condition_variable PollingData::poll_cv;
14 //std::mutex PollingData::poll_mutex;
15 
16 //Single global instance, defined here
18 
20  return std::accumulate(if_waiting.begin(), if_waiting.end(), false, [](bool a, bool b) { return a || b; });
21 }
22 
23 void PollingData::insert_completion_entry(uint32_t index, std::pair<int32_t, int32_t> ce) {
24  std::lock_guard<std::mutex> lk(poll_mutex);
25  completion_entries[index].push_back(ce);
26 }
27 
28 std::optional<std::pair<int32_t, int32_t>> PollingData::get_completion_entry(const std::thread::id id) {
29  std::lock_guard<std::mutex> lk(poll_mutex);
30  auto index = tid_to_index[id];
31  if(completion_entries[index].empty()) {
32  return {};
33  }
34  auto ce = completion_entries[index].front();
35  completion_entries[index].pop_front();
36  return ce;
37 }
38 
39 uint32_t PollingData::get_index(const std::thread::id id) {
40  std::lock_guard<std::mutex> lk(poll_mutex);
41  if(tid_to_index.find(id) == tid_to_index.end()) {
42  completion_entries.push_back(std::list<std::pair<int32_t, int32_t>>());
43  tid_to_index[id] = completion_entries.size() - 1;
44  if_waiting.push_back(false);
45  }
46  return tid_to_index[id];
47 }
48 
49 void PollingData::set_waiting(const std::thread::id id) {
50  std::lock_guard<std::mutex> lk(poll_mutex);
51  auto index = tid_to_index[id];
52  if_waiting[index] = true;
53  poll_cv.notify_all();
54 }
55 
56 void PollingData::reset_waiting(const std::thread::id id) {
57  std::lock_guard<std::mutex> lk(poll_mutex);
58  auto index = tid_to_index[id];
59  if_waiting[index] = false;
60 }
61 
63  std::unique_lock<std::mutex> lk(poll_mutex);
64  // poll_cv.wait(lk, check_waiting);
65  poll_cv.wait(lk, std::bind(&PollingData::check_waiting, this));
66 }
67 } // namespace util
68 } // namespace sst
uint32_t get_index(const std::thread::id id)
Definition: poll_utils.cpp:39
void insert_completion_entry(uint32_t index, std::pair< int32_t, int32_t > ce)
Definition: poll_utils.cpp:23
PollingData polling_data
Definition: poll_utils.cpp:17
void set_waiting(const std::thread::id id)
Definition: poll_utils.cpp:49
std::map< std::thread::id, uint32_t > tid_to_index
Definition: poll_utils.hpp:22
void reset_waiting(const std::thread::id id)
Definition: poll_utils.cpp:56
std::condition_variable poll_cv
Definition: poll_utils.hpp:24
std::vector< std::list< std::pair< int32_t, int32_t > > > completion_entries
Definition: poll_utils.hpp:21
std::optional< std::pair< int32_t, int32_t > > get_completion_entry(const std::thread::id id)
Definition: poll_utils.cpp:28
std::vector< bool > if_waiting
Definition: poll_utils.hpp:23