1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use std::cell::Cell;
67/// Executes the given function in a special context that cannot be recursively re-entered
8/// on the same execution stack.
9///
10/// It can be used to prevent unwanted recursive function executions.
11pub fn with_recursion_guard<T>(f: impl FnOnce() -> T) -> T {
12match with_recursion_guard_impl(f) {
13Ok(result) => result,
14Err(UnwantedRecursionError) => {
15// WARNING! Do not call panic! because it may itself allocate and cause further
16 // recursion. This still results in a backtrace in the log.
17std::process::abort()
18 }
19 }
20}
2122thread_local! {
23/// Whether the current thread is currently in a `with_recursion_guard_impl` call or not.
24static RECURSION_GUARD: Cell<bool> = const { Cell::new(false) };
25}
2627#[derive(Debug)]
28struct UnwantedRecursionError;
2930fn with_recursion_guard_impl<T>(f: impl FnOnce() -> T) -> Result<T, UnwantedRecursionError> {
31 RECURSION_GUARD.with(|cell| {
32let was_already_acquired = cell.replace(true);
33if was_already_acquired {
34return Err(UnwantedRecursionError);
35 }
3637let result = f();
3839 cell.set(false);
4041Ok(result)
42 })
43}
4445#[cfg(test)]
46mod tests {
47use super::*;
48use assert_matches::assert_matches;
4950// Verify that executing a non-recursive function succeeds.
51#[test]
52fn test_recursion_guard_ok() {
53let result = with_recursion_guard_impl(|| 42);
54assert_matches!(result, Ok(42));
55 }
5657// Verify that the inner recursive call fails.
58#[test]
59fn test_recursion_guard_violation() {
60let result = with_recursion_guard_impl(|| with_recursion_guard_impl(|| 42));
61assert_matches!(result, Ok(Err(UnwantedRecursionError)));
62 }
63}