1// Copyright 2019 the V8 project 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.
4
5#ifndef V8_EXECUTION_INTERRUPTS_SCOPE_H_
6#define V8_EXECUTION_INTERRUPTS_SCOPE_H_
7
8#include "src/execution/stack-guard.h"
9
10namespace v8 {
11namespace internal {
12
13class Isolate;
14
15// Scope intercepts only interrupt which is part of its interrupt_mask and does
16// not affect other interrupts.
17class V8_NODISCARD InterruptsScope {
18 public:
19  enum Mode { kPostponeInterrupts, kRunInterrupts, kNoop };
20
21  V8_EXPORT_PRIVATE InterruptsScope(Isolate* isolate, intptr_t intercept_mask,
22                                    Mode mode);
23
24  virtual ~InterruptsScope() {
25    if (mode_ != kNoop) stack_guard_->PopInterruptsScope();
26  }
27
28  // Find the scope that intercepts this interrupt.
29  // It may be outermost PostponeInterruptsScope or innermost
30  // SafeForInterruptsScope if any.
31  // Return whether the interrupt has been intercepted.
32  bool Intercept(StackGuard::InterruptFlag flag);
33
34 private:
35  StackGuard* stack_guard_;
36  intptr_t intercept_mask_;
37  intptr_t intercepted_flags_;
38  Mode mode_;
39  InterruptsScope* prev_;
40
41  friend class StackGuard;
42};
43
44// Support for temporarily postponing interrupts. When the outermost
45// postpone scope is left the interrupts will be re-enabled and any
46// interrupts that occurred while in the scope will be taken into
47// account.
48class V8_NODISCARD PostponeInterruptsScope : public InterruptsScope {
49 public:
50  PostponeInterruptsScope(Isolate* isolate,
51                          int intercept_mask = StackGuard::ALL_INTERRUPTS)
52      : InterruptsScope(isolate, intercept_mask,
53                        InterruptsScope::kPostponeInterrupts) {}
54  ~PostponeInterruptsScope() override = default;
55};
56
57// Support for overriding PostponeInterruptsScope. Interrupt is not ignored if
58// innermost scope is SafeForInterruptsScope ignoring any outer
59// PostponeInterruptsScopes.
60class V8_NODISCARD SafeForInterruptsScope : public InterruptsScope {
61 public:
62  SafeForInterruptsScope(Isolate* isolate,
63                         int intercept_mask = StackGuard::ALL_INTERRUPTS)
64      : InterruptsScope(isolate, intercept_mask,
65                        InterruptsScope::kRunInterrupts) {}
66  ~SafeForInterruptsScope() override = default;
67};
68
69}  // namespace internal
70}  // namespace v8
71
72#endif  // V8_EXECUTION_INTERRUPTS_SCOPE_H_
73