interrupts-scope.h 2.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_EXECUTION_INTERRUPTS_SCOPE_H_
#define V8_EXECUTION_INTERRUPTS_SCOPE_H_

#include "src/execution/stack-guard.h"

namespace v8 {
namespace internal {

class Isolate;

// Scope intercepts only interrupt which is part of its interrupt_mask and does
// not affect other interrupts.
17
class V8_NODISCARD InterruptsScope {
18 19 20
 public:
  enum Mode { kPostponeInterrupts, kRunInterrupts, kNoop };

21
  V8_EXPORT_PRIVATE InterruptsScope(Isolate* isolate, intptr_t intercept_mask,
22 23 24 25 26 27 28 29 30 31 32 33 34 35
                                    Mode mode);

  virtual ~InterruptsScope() {
    if (mode_ != kNoop) stack_guard_->PopInterruptsScope();
  }

  // Find the scope that intercepts this interrupt.
  // It may be outermost PostponeInterruptsScope or innermost
  // SafeForInterruptsScope if any.
  // Return whether the interrupt has been intercepted.
  bool Intercept(StackGuard::InterruptFlag flag);

 private:
  StackGuard* stack_guard_;
36 37
  intptr_t intercept_mask_;
  intptr_t intercepted_flags_;
38 39 40 41 42 43 44 45 46 47
  Mode mode_;
  InterruptsScope* prev_;

  friend class StackGuard;
};

// Support for temporarily postponing interrupts. When the outermost
// postpone scope is left the interrupts will be re-enabled and any
// interrupts that occurred while in the scope will be taken into
// account.
48
class V8_NODISCARD PostponeInterruptsScope : public InterruptsScope {
49 50 51 52 53 54 55 56 57 58 59
 public:
  PostponeInterruptsScope(Isolate* isolate,
                          int intercept_mask = StackGuard::ALL_INTERRUPTS)
      : InterruptsScope(isolate, intercept_mask,
                        InterruptsScope::kPostponeInterrupts) {}
  ~PostponeInterruptsScope() override = default;
};

// Support for overriding PostponeInterruptsScope. Interrupt is not ignored if
// innermost scope is SafeForInterruptsScope ignoring any outer
// PostponeInterruptsScopes.
60
class V8_NODISCARD SafeForInterruptsScope : public InterruptsScope {
61 62 63 64 65 66 67 68 69 70 71 72
 public:
  SafeForInterruptsScope(Isolate* isolate,
                         int intercept_mask = StackGuard::ALL_INTERRUPTS)
      : InterruptsScope(isolate, intercept_mask,
                        InterruptsScope::kRunInterrupts) {}
  ~SafeForInterruptsScope() override = default;
};

}  // namespace internal
}  // namespace v8

#endif  // V8_EXECUTION_INTERRUPTS_SCOPE_H_