Skip to content

Commit

Permalink
Move the check for a locked GIL into a cold function and closer to th…
Browse files Browse the repository at this point in the history
…e locking.
  • Loading branch information
adamreichold committed May 24, 2023
1 parent 9b7c37e commit 0ab1365
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/gil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,17 @@ impl LockGIL {

Self { count }
}

#[cold]
fn check(current: isize) {
match current {
GIL_LOCKED_DURING_TRAVERSE => panic!(
"Access to the GIL is prohibited while a __traverse__ implmentation is running."
),
current if current < 0 => panic!("Access to the GIL is currently prohibited."),
_ => (),
}
}
}

impl Drop for LockGIL {
Expand Down Expand Up @@ -452,13 +463,7 @@ fn increment_gil_count() {
// Ignores the error in case this function called from `atexit`.
let _ = GIL_COUNT.try_with(|c| {
let current = c.get();
match current {
GIL_LOCKED_DURING_TRAVERSE => panic!(
"Access to the GIL is prohibited while a __traverse__ implmentation is running."
),
current if current < 0 => panic!("Access to the GIL is currently prohibited."),
_ => (),
}
LockGIL::check(current);
c.set(current + 1);
});
}
Expand Down

0 comments on commit 0ab1365

Please sign in to comment.