forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_space.h
48 lines (37 loc) · 1.45 KB
/
stack_space.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// 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 {
// <function body>
// });
template <typename Fn>
auto RunWithExtraStack(Fn fn) -> decltype(fn()) {
using ReturnType = decltype(fn());
static_assert(!std::is_reference_v<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_