scoped_set_task_priority_for_current_thread.cc 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2016 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. #include "base/task/scoped_set_task_priority_for_current_thread.h"
  5. #include "base/check_op.h"
  6. #include "base/lazy_instance.h"
  7. #include "base/threading/thread_local.h"
  8. namespace base {
  9. namespace internal {
  10. namespace {
  11. LazyInstance<ThreadLocalPointer<const TaskPriority>>::Leaky
  12. tls_task_priority_for_current_thread = LAZY_INSTANCE_INITIALIZER;
  13. } // namespace
  14. ScopedSetTaskPriorityForCurrentThread::ScopedSetTaskPriorityForCurrentThread(
  15. TaskPriority priority)
  16. : priority_(priority) {
  17. DCHECK(!tls_task_priority_for_current_thread.Get().Get());
  18. tls_task_priority_for_current_thread.Get().Set(&priority_);
  19. }
  20. ScopedSetTaskPriorityForCurrentThread::
  21. ~ScopedSetTaskPriorityForCurrentThread() {
  22. DCHECK_EQ(&priority_, tls_task_priority_for_current_thread.Get().Get());
  23. tls_task_priority_for_current_thread.Get().Set(nullptr);
  24. }
  25. TaskPriority GetTaskPriorityForCurrentThread() {
  26. const TaskPriority* priority =
  27. tls_task_priority_for_current_thread.Get().Get();
  28. return priority ? *priority : TaskPriority::USER_BLOCKING;
  29. }
  30. } // namespace internal
  31. } // namespace base