facebook / facebook/folly

use ParkingLot to construct WaitableMutex will crash

Open
#2,238 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
30.5k
Forks
5.9k
PR merge metrics
No merged PRs in 30d

Description

``` cpp
class WaitableMutex : public std::mutex {
using Lot = ParkingLot>;
static Lot lot;

public:
void unlock() {
bool unparked = false;
lot.unpark(uint64_t(this), [&](std::function wfunc) {
if (wfunc()) {
unparked = true;
return UnparkControl::RemoveBreak;
} else {
return UnparkControl::RemoveContinue;
}
});

if (!unparked) {
std::mutex::unlock();
}
}

template
void wait(Wait wfunc) {
lot.park(
uint64_t(this), wfunc, [&]() { return !wfunc(); }, [&]() { std::mutex::unlock(); });
}
};

WaitableMutex::Lot WaitableMutex::lot;

int main() {
// test();
std::atomic go{false};
WaitableMutex mu;
std::thread t([&]() {
std::unique_lock g(mu);
mu.wait([&]() { return go == true; });
});
std::this_thread::sleep_for(std::chrono::seconds(1));

{
std::lock_guard g(mu);
go = true;
}
t.join();
}
/*
error: unlock of unowned mutex
reason:
"When destructed twice, the unlock method will be called twice. The first time it will not execute std::mutex::unlock(), but the second time it will. And during the wait operation, std::mutex::unlock() has already been called once."
*/

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.