Skip to content

Commit

Permalink
Merge pull request #5166 from rgacogne/rec40-backport-5042
Browse files Browse the repository at this point in the history
Backport #5042: StateHolder: Allocate (and copy if needed) before taking the lock
  • Loading branch information
Habbie committed Mar 20, 2017
2 parents 829e2bd + 651c0e9 commit d57f058
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pdns/sholder.hh
Expand Up @@ -93,9 +93,12 @@ public:

void setState(T state) //!< Safely & slowly change the global state
{
std::lock_guard<std::mutex> l(d_lock);
d_state = std::make_shared<T>(T(state));
d_generation++;
std::shared_ptr<T> newState = std::make_shared<T>(state);
{
std::lock_guard<std::mutex> l(d_lock);
d_state = newState;
d_generation++;
}
}

T getCopy() const //!< Safely & slowly get a copy of the global state
Expand All @@ -110,7 +113,7 @@ public:
std::lock_guard<std::mutex> l(d_lock);
auto state=*d_state; // and yes, these three steps are necessary, can't ever modify state in place, even when locked!
act(state);
d_state = std::make_shared<T>(T(state));
d_state = std::make_shared<T>(state);
++d_generation;
}

Expand Down

0 comments on commit d57f058

Please sign in to comment.