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