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

Possibly wrong readers count in boost::thread_v2::upgrade_mutex #362

Open
daniel-boehme opened this issue Nov 5, 2021 · 0 comments
Open

Comments

@daniel-boehme
Copy link

Because of issues (#265, #361) in the default shared mutex implementation, I reviewed the code of the V2 mutex classes.

It seems like upgrade_mutex::try_unlock_shared_and_lock_until(const boost::chrono::time_point<Clock, Duration>& abs_time) and upgrade_mutex::try_unlock_upgrade_and_lock_until(const boost::chrono::time_point<Clock, Duration>& abs_time) can set the readers count to a wrong value.

Both functions contain the same piece of code:

      count_t num_readers = (state_ & n_readers_) - 1;
      state_ &= ~n_readers_;
      state_ |= (write_entered_ | num_readers);
      if (!gate2_.wait_until(lk, abs_time, boost::bind(
            &upgrade_mutex::no_readers, boost::ref(*this))))
      {
        ++num_readers;
        state_ &= ~(write_entered_ | n_readers_);
        state_ |= num_readers;

The problem is with ++num_readers. Between reading it out of the state and then setting it again, the mutex is unlocked, so readers can unlock_shared(). This is even necessary, as the code actually waits until the readers count reaches 0.

So IMHO, the line in question should read:

num_readers = (state_ & n_readers_) +1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant