xref: /aosp_15_r20/external/cronet/base/task/thread_pool/task_source_sort_key.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 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_THREAD_POOL_TASK_SOURCE_SORT_KEY_H_
6 #define BASE_TASK_THREAD_POOL_TASK_SOURCE_SORT_KEY_H_
7 
8 #include "base/base_export.h"
9 #include "base/task/task_traits.h"
10 #include "base/time/time.h"
11 
12 namespace base {
13 namespace internal {
14 
15 // An immutable but assignable representation of the priority of a Sequence.
16 class BASE_EXPORT TaskSourceSortKey final {
17  public:
18   TaskSourceSortKey() = default;
19   TaskSourceSortKey(TaskPriority priority,
20                     TimeTicks ready_time,
21                     uint8_t worker_count = 0);
22 
priority()23   TaskPriority priority() const { return priority_; }
worker_count()24   uint8_t worker_count() const { return worker_count_; }
ready_time()25   TimeTicks ready_time() const { return ready_time_; }
26 
27   // Used for a max-heap.
28   bool operator<(const TaskSourceSortKey& other) const;
29 
30   bool operator==(const TaskSourceSortKey& other) const {
31     return priority_ == other.priority_ &&
32            worker_count_ == other.worker_count_ &&
33            ready_time_ == other.ready_time_;
34   }
35   bool operator!=(const TaskSourceSortKey& other) const {
36     return !(other == *this);
37   }
38 
39  private:
40   // The private section allows this class to keep its immutable property while
41   // being copy-assignable (i.e. instead of making its members const).
42 
43   // Highest task priority in the sequence at the time this sort key was
44   // created.
45   TaskPriority priority_;
46 
47   // Number of workers running the task source, used as secondary sort key
48   // prioritizing task sources with fewer workers.
49   uint8_t worker_count_;
50 
51   // Time since the task source has been ready to run upcoming work, used as
52   // secondary sort key after |worker_count| prioritizing older task sources.
53   TimeTicks ready_time_;
54 };
55 
56 }  // namespace internal
57 }  // namespace base
58 
59 #endif  // BASE_TASK_THREAD_POOL_TASK_SOURCE_SORT_KEY_H_
60