thread_group_native.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TASK_THREAD_POOL_THREAD_GROUP_NATIVE_H_
  5. #define BASE_TASK_THREAD_POOL_THREAD_GROUP_NATIVE_H_
  6. #include "base/base_export.h"
  7. #include "base/synchronization/atomic_flag.h"
  8. #include "base/task/thread_pool/thread_group.h"
  9. namespace base {
  10. namespace internal {
  11. class BASE_EXPORT ThreadGroupNative : public ThreadGroup {
  12. public:
  13. // Destroying a ThreadGroupNative is not allowed in
  14. // production; it is always leaked. In tests, it can only be destroyed after
  15. // JoinForTesting() has returned.
  16. ~ThreadGroupNative() override;
  17. // Starts the thread group and allows tasks to begin running.
  18. void Start(WorkerEnvironment worker_environment = WorkerEnvironment::NONE);
  19. // ThreadGroup:
  20. void JoinForTesting() override;
  21. size_t GetMaxConcurrentNonBlockedTasksDeprecated() const override;
  22. void DidUpdateCanRunPolicy() override;
  23. protected:
  24. ThreadGroupNative(TrackedRef<TaskTracker> task_tracker,
  25. TrackedRef<Delegate> delegate,
  26. ThreadGroup* predecessor_thread_group);
  27. // Runs a task off the next task source on the |priority_queue_|. Called by
  28. // callbacks posted to platform native thread pools.
  29. void RunNextTaskSourceImpl();
  30. virtual void JoinImpl() = 0;
  31. virtual void StartImpl() = 0;
  32. virtual void SubmitWork() = 0;
  33. // Used to control the worker environment. Supports COM MTA on Windows.
  34. WorkerEnvironment worker_environment_ = WorkerEnvironment::NONE;
  35. private:
  36. class ScopedCommandsExecutor;
  37. // ThreadGroup:
  38. void UpdateSortKey(TaskSource::Transaction transaction) override;
  39. void PushTaskSourceAndWakeUpWorkers(
  40. TransactionWithRegisteredTaskSource transaction_with_task_source)
  41. override;
  42. void EnsureEnoughWorkersLockRequired(BaseScopedCommandsExecutor* executor)
  43. override EXCLUSIVE_LOCKS_REQUIRED(lock_);
  44. // Updates the minimum priority allowed to run below which tasks should yield,
  45. // based on task sources in |priority_queue_|.
  46. void UpdateMinAllowedPriorityLockRequired() EXCLUSIVE_LOCKS_REQUIRED(lock_);
  47. // Returns the top TaskSource off the |priority_queue_|. Returns nullptr
  48. // if the |priority_queue_| is empty.
  49. RegisteredTaskSource GetWork();
  50. // Indicates whether the thread group has been started yet.
  51. bool started_ GUARDED_BY(lock_) = false;
  52. // Number of threadpool work submitted to the thread group which haven't
  53. // popped a TaskSource from the PriorityQueue yet.
  54. size_t num_pending_threadpool_work_ GUARDED_BY(lock_) = 0;
  55. #if DCHECK_IS_ON()
  56. // Set once JoinForTesting() has returned.
  57. bool join_for_testing_returned_ = false;
  58. #endif
  59. DISALLOW_COPY_AND_ASSIGN(ThreadGroupNative);
  60. };
  61. } // namespace internal
  62. } // namespace base
  63. #endif // BASE_TASK_THREAD_POOL_THREAD_GROUP_NATIVE_H_