Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Project #1 Deadlock Test #741

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions test/buffer/buffer_pool_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,44 @@ TEST(BufferPoolManagerTest, DISABLED_ContentionTest) {
thread1.join();
}

TEST(BufferPoolManagerTest, DISABLED_DeadlockTest) {
auto disk_manager = std::make_shared<DiskManager>(db_fname);
auto bpm = std::make_shared<BufferPoolManager>(FRAMES, disk_manager.get(), K_DIST);

auto pid0 = bpm->NewPage();
auto pid1 = bpm->NewPage();

auto guard0 = bpm->WritePage(pid0);

// A crude way of synchronizing threads, but works for this small case.
std::atomic<bool> start = false;

auto child = std::thread([&]() {
// Acknowledge that we can begin the test.
start.store(true);

// Attempt to write to page 0.
auto guard0 = bpm->WritePage(pid0);
});

// Wait for the other thread to begin before we start the test.
while (!start.load()) {
}

// Make the other thread wait for a bit.
// This mimics the main thread doing some work while holding the write latch on page 0.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));

// If your latching mechanism is incorrect, the next line of code will deadlock.
// Think about what might happen if you hold a certain "all-encompassing" latch for too long...

// While holding page 0, take the latch on page 1.
auto guard1 = bpm->WritePage(pid1);

// Let the child thread have the page 0 since we're done with it.
guard0.Drop();

child.join();
}

} // namespace bustub
Loading