1 // Copyright 2021 The Chromium Authors 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 BASE_TASK_SEQUENCE_MANAGER_FENCE_H_ 6 #define BASE_TASK_SEQUENCE_MANAGER_FENCE_H_ 7 8 #include "base/base_export.h" 9 #include "base/compiler_specific.h" 10 #include "base/task/sequence_manager/enqueue_order.h" 11 #include "base/task/sequence_manager/task_order.h" 12 13 namespace base { 14 15 class TimeTicks; 16 17 namespace sequence_manager { 18 namespace internal { 19 20 class TaskQueueImpl; 21 22 // `Fence`s are used to prevent the execution of tasks starting with a 23 // particular `TaskOrder`, such that for a `Task` and a `Fence`, if 24 // task.task_order() >= fence.task_order(), then the task is blocked from 25 // running. Blocking fences are a special kind of fence that have a `TaskOrder` 26 // less than that of any `Task`. 27 class BASE_EXPORT Fence { 28 public: 29 // Creates a `Fence` with the same `TaskOrder` as `task_order`, which is 30 // useful for creating a fence relative to a particular `Task`. 31 // `task_order.enqueue_order()` must be "set", i.e. it cannot be 32 // `EnqueueOrder::none()`. 33 explicit Fence(const TaskOrder& task_order); 34 Fence(const Fence& other); 35 Fence& operator=(const Fence& other); 36 ~Fence(); 37 38 // Creates a blocking fence which has a `TaskOrder` that is less than that of 39 // all tasks. 40 static Fence BlockingFence(); 41 task_order()42 const TaskOrder& task_order() const LIFETIME_BOUND { return task_order_; } 43 44 // Returns true iff this is a blocking fence. IsBlockingFence()45 bool IsBlockingFence() const { 46 return task_order_.enqueue_order() == EnqueueOrder::blocking_fence(); 47 } 48 49 private: 50 friend class TaskQueueImpl; // For `CreateWithEnqueueOrder()`. 51 52 Fence(EnqueueOrder enqueue_order, 53 TimeTicks delayed_run_time, 54 int sequence_num); 55 56 // Creates a `Fence` with `enqueue_order` and a null delayed run time. 57 // `enqueue_order` cannot be EnqueueOrder::none(). 58 static Fence CreateWithEnqueueOrder(EnqueueOrder enqueue_order); 59 60 TaskOrder task_order_; 61 }; 62 63 } // namespace internal 64 } // namespace sequence_manager 65 } // namespace base 66 67 #endif // BASE_TASK_SEQUENCE_MANAGER_FENCE_H_ 68