Skip to content

Commit

Permalink
p1: fix benchmark
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Chi <iskyzh@gmail.com>
  • Loading branch information
skyzh committed Sep 9, 2023
1 parent 240625a commit f9b4af3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
22 changes: 15 additions & 7 deletions src/include/common/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
#include <mutex> // NOLINT
#include <queue>
#include <utility>
#include "readerwriterqueue/readerwriterqueue.h"

namespace bustub {

/**
* Channels allow for safe sharing of data between threads.
* Channels allow for safe sharing of data between threads. This is a multi-producer multi-consumer channel.
*/
template <class T>
class Channel {
Expand All @@ -34,18 +33,27 @@ class Channel {
*
* @param element The element to be inserted.
*/
void Put(T element) { q_.enqueue(std::move(element)); }
void Put(T element) {
std::unique_lock<std::mutex> lk(m_);
q_.push(std::move(element));
lk.unlock();
cv_.notify_all();
}

/**
* @brief Gets an element from the shared queue. If the queue is empty, blocks until an element is available.
*/
auto Get() -> T {
T x;
q_.wait_dequeue(x);
return x;
std::unique_lock<std::mutex> lk(m_);
cv_.wait(lk, [&]() { return !q_.empty(); });
T element = std::move(q_.front());
q_.pop();
return element;
}

private:
moodycamel::BlockingReaderWriterQueue<T> q_;
std::mutex m_;
std::condition_variable cv_;
std::queue<T> q_;
};
} // namespace bustub
2 changes: 1 addition & 1 deletion third_party/readerwriterqueue/readerwriterqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class MOODYCAMEL_MAYBE_ALIGN_TO_CACHELINE ReaderWriterQueue
// returns false instead. If the queue has at least one element,
// moves front to result using operator=, then returns true.
template<typename U>
bool try_dequeue(U& result) AE_NO_TSAN
bool try_dequeue(U&& result) AE_NO_TSAN
{
#ifndef NDEBUG
ReentrantGuard guard(this->dequeuing);
Expand Down
2 changes: 1 addition & 1 deletion tools/bpm_bench/bpm_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ auto main(int argc, char **argv) -> int {

uint64_t lru_k_size = 16;
if (program.present("--lru-k-size")) {
bustub_page_cnt = std::stoi(program.get("--lru-k-size"));
lru_k_size = std::stoi(program.get("--lru-k-size"));
}

auto disk_manager = std::make_unique<DiskManagerUnlimitedMemory>();
Expand Down

0 comments on commit f9b4af3

Please sign in to comment.