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

Introduce a RAII state-guard. #1435

Open
1uc opened this issue Sep 18, 2024 · 1 comment
Open

Introduce a RAII state-guard. #1435

1uc opened this issue Sep 18, 2024 · 1 comment

Comments

@1uc
Copy link
Collaborator

1uc commented Sep 18, 2024

While printing code we have the following pattern:

void print_net_receive() {
  printing_net_receive = true;
  // ...
  printing_net_receive = false;
}

The problem is that:

  1. We need to be sure that print_net_receive is not re-entrant otherwise printing_net_receive will be toggled back off. This is easy in this particular case, but can be non-obvious in other cases.
  2. We must not return, because otherwise printing_net_receive will stay true indefinitely.

The solution is to use a RAII guard:

struct ToggleGuard {
    bool * flag;
    bool previous_value;

   ToggleGuard(bool* flag, bool new_value) : flag(flag), old_value(*flag) {}
   ~ToggleGuard() { *flag = old_value; }
};
@alkino
Copy link
Member

alkino commented Sep 18, 2024

Maybe we can check too for reentrant and throw?

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

2 participants