• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_WORKER_THREAD_SET_H_
6 #define BASE_TASK_THREAD_POOL_WORKER_THREAD_SET_H_
7 
8 #include <stddef.h>
9 
10 #include <set>
11 
12 #include "base/base_export.h"
13 #include "base/memory/raw_ptr.h"
14 
15 namespace base {
16 namespace internal {
17 
18 class WorkerThread;
19 
20 // An ordered set of WorkerThreads which has custom logic to treat the worker at
21 // the front of the set as being "in-use" (so its time in that position doesn't
22 // count towards being inactive / reclaimable). Supports removal of arbitrary
23 // WorkerThreads. DCHECKs when a WorkerThread is inserted multiple times.
24 // WorkerThreads are not owned by the set. All operations are amortized
25 // O(log(n)). This class is NOT thread-safe.
26 class BASE_EXPORT WorkerThreadSet {
27   struct Compare {
28     bool operator()(const WorkerThread* a, const WorkerThread* b) const;
29   };
30 
31  public:
32   WorkerThreadSet();
33   WorkerThreadSet(const WorkerThreadSet&) = delete;
34   WorkerThreadSet& operator=(const WorkerThreadSet&) = delete;
35   ~WorkerThreadSet();
36 
37   // Inserts |worker| in the set. |worker| must not already be on the set. Flags
38   // the WorkerThread previously at the front of the set, if it
39   // changed, or |worker| as unused.
40   void Insert(WorkerThread* worker);
41 
42   // Removes the front WorkerThread from the set and returns it. Returns nullptr
43   // if the set is empty. Flags the WorkerThread now at the front of the set, if
44   // any, as being in-use.
45   WorkerThread* Take();
46 
47   // Returns the front WorkerThread from the set, nullptr if empty.
48   WorkerThread* Peek() const;
49 
50   // Returns true if |worker| is already in the set.
51   bool Contains(const WorkerThread* worker) const;
52 
53   // Removes |worker| from the set. Must not be invoked for the first worker
54   // on the set.
55   void Remove(const WorkerThread* worker);
56 
57   // Returns the number of WorkerThreads on the set.
Size()58   size_t Size() const { return set_.size(); }
59 
60   // Returns true if the set is empty.
IsEmpty()61   bool IsEmpty() const { return set_.empty(); }
62 
63  private:
64   std::set<raw_ptr<WorkerThread, SetExperimental>, Compare> set_;
65 };
66 
67 }  // namespace internal
68 }  // namespace base
69 
70 #endif  // BASE_TASK_THREAD_POOL_WORKER_THREAD_SET_H_
71