• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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_HEAP_CPPGC_TASK_HANDLE_H_
6 #define V8_HEAP_CPPGC_TASK_HANDLE_H_
7 
8 #include <memory>
9 
10 #include "src/base/logging.h"
11 
12 namespace cppgc {
13 namespace internal {
14 
15 // A handle that is used for cancelling individual tasks.
16 struct SingleThreadedHandle {
17   struct NonEmptyTag {};
18 
19   // Default construction results in empty handle.
20   SingleThreadedHandle() = default;
21 
SingleThreadedHandleSingleThreadedHandle22   explicit SingleThreadedHandle(NonEmptyTag)
23       : is_cancelled_(std::make_shared<bool>(false)) {}
24 
CancelSingleThreadedHandle25   void Cancel() {
26     DCHECK(is_cancelled_);
27     *is_cancelled_ = true;
28   }
29 
IsCanceledSingleThreadedHandle30   bool IsCanceled() const {
31     DCHECK(is_cancelled_);
32     return *is_cancelled_;
33   }
34 
35   // A handle is active if it is non-empty and not cancelled.
36   explicit operator bool() const {
37     return is_cancelled_.get() && !*is_cancelled_.get();
38   }
39 
40  private:
41   std::shared_ptr<bool> is_cancelled_;
42 };
43 
44 }  // namespace internal
45 }  // namespace cppgc
46 
47 #endif  // V8_HEAP_CPPGC_TASK_HANDLE_H_
48