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