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 WorkerThreadWaitableEvent; 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 WorkerThreadWaitableEvent* a, 28 const WorkerThreadWaitableEvent* 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 WorkerThreadWaitableEvent previously at the front of the set, if it 39 // changed, or |worker| as unused. 40 void Insert(WorkerThreadWaitableEvent* 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 WorkerThreadWaitableEvent* Take(); 46 47 // Returns the front WorkerThread from the set, nullptr if empty. 48 WorkerThreadWaitableEvent* Peek() const; 49 50 // Returns true if |worker| is already in the set. 51 bool Contains(const WorkerThreadWaitableEvent* 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 WorkerThreadWaitableEvent* 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<WorkerThreadWaitableEvent*, Compare> set_; 65 }; 66 67 } // namespace internal 68 } // namespace base 69 70 #endif // BASE_TASK_THREAD_POOL_WORKER_THREAD_SET_H_ 71