mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-18 23:51:02 +00:00
Adds stack space handling similar to Clang's approach, but with more support for forwarding return values. Refactors ParseAndExecute for better sharing, and for centralization of the InitStackSpace handling. Fixes #2795
47 lines
1.4 KiB
C++
47 lines
1.4 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#ifndef CARBON_EXPLORER_INTERPRETER_STACK_SPACE_H_
|
|
#define CARBON_EXPLORER_INTERPRETER_STACK_SPACE_H_
|
|
|
|
#include <optional>
|
|
|
|
#include "llvm/ADT/STLFunctionalExtras.h"
|
|
|
|
namespace Carbon {
|
|
|
|
namespace Internal {
|
|
|
|
// Returns true if a new thread should be started for more stack space.
|
|
auto IsStackSpaceNearlyExhausted() -> bool;
|
|
|
|
// Starts a thread to run the function.
|
|
auto RunWithExtraStackHelper(llvm::function_ref<void()> fn) -> void;
|
|
|
|
} // namespace Internal
|
|
|
|
// Runs `fn` after ensuring there is a reasonable amount of space left on the
|
|
// stack for it to run in. This will run `fn` in a separate thread if there is
|
|
// not enough space left on the current stack, or if RunWithExtraStack didn't
|
|
// create the current thread.
|
|
//
|
|
// Usage:
|
|
// return RunWithExtraStack<ReturnType>([&]() -> ReturnType {
|
|
// <function body>
|
|
// });
|
|
template <typename ReturnType>
|
|
auto RunWithExtraStack(llvm::function_ref<ReturnType()> fn) -> ReturnType {
|
|
if (Internal::IsStackSpaceNearlyExhausted()) {
|
|
std::optional<ReturnType> result;
|
|
Internal::RunWithExtraStackHelper([&] { result = fn(); });
|
|
return std::move(*result);
|
|
} else {
|
|
return fn();
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_EXPLORER_INTERPRETER_STACK_SPACE_H_
|