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 5 #include "base/task_scheduler/scoped_set_task_priority_for_current_thread.h" 6 7 #include "base/lazy_instance.h" 8 #include "base/logging.h" 9 #include "base/threading/thread_local.h" 10 11 namespace base { 12 namespace internal { 13 14 namespace { 15 16 LazyInstance<ThreadLocalPointer<const TaskPriority>>::Leaky 17 tls_task_priority_for_current_thread = LAZY_INSTANCE_INITIALIZER; 18 19 } // namespace 20 ScopedSetTaskPriorityForCurrentThread(TaskPriority priority)21ScopedSetTaskPriorityForCurrentThread::ScopedSetTaskPriorityForCurrentThread( 22 TaskPriority priority) 23 : priority_(priority) { 24 DCHECK(!tls_task_priority_for_current_thread.Get().Get()); 25 tls_task_priority_for_current_thread.Get().Set(&priority_); 26 } 27 28 ScopedSetTaskPriorityForCurrentThread:: ~ScopedSetTaskPriorityForCurrentThread()29 ~ScopedSetTaskPriorityForCurrentThread() { 30 DCHECK_EQ(&priority_, tls_task_priority_for_current_thread.Get().Get()); 31 tls_task_priority_for_current_thread.Get().Set(nullptr); 32 } 33 GetTaskPriorityForCurrentThread()34TaskPriority GetTaskPriorityForCurrentThread() { 35 const TaskPriority* priority = 36 tls_task_priority_for_current_thread.Get().Get(); 37 return priority ? *priority : TaskPriority::USER_VISIBLE; 38 } 39 40 } // namespace internal 41 } // namespace base 42