Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: base/threading/platform_thread_internal_posix.cc

Issue 1870033002: Fix NiceValueToThreadPriority failing on an unknown nice value. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix comments. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/threading/platform_thread_internal_posix.h" 5 #include "base/threading/platform_thread_internal_posix.h"
6 6
7 #include <limits>
7 #include "base/logging.h" 8 #include "base/logging.h"
8 9
9 namespace base { 10 namespace base {
10 11
11 namespace internal { 12 namespace internal {
12 13
13 int ThreadPriorityToNiceValue(ThreadPriority priority) { 14 int ThreadPriorityToNiceValue(ThreadPriority priority) {
14 for (const ThreadPriorityToNiceValuePair& pair : 15 for (const ThreadPriorityToNiceValuePair& pair :
15 kThreadPriorityToNiceValueMap) { 16 kThreadPriorityToNiceValueMap) {
16 if (pair.priority == priority) 17 if (pair.priority == priority)
17 return pair.nice_value; 18 return pair.nice_value;
18 } 19 }
19 NOTREACHED() << "Unknown ThreadPriority"; 20 NOTREACHED() << "Unknown ThreadPriority";
20 return 0; 21 return 0;
21 } 22 }
22 23
23 ThreadPriority NiceValueToThreadPriority(int nice_value) { 24 ThreadPriority NiceValueToThreadPriority(int nice_value) {
25 // Try to find a priority that best describes |nice_value|. If there isn't
26 // an exact match, we return the priority whose nice value is closest to
gab 2016/04/08 15:50:49 How about: "we return the priority whose nice val
Yuta Kitamura 2016/04/12 10:18:59 Yeah, that's much better (I rewrote the phrase sev
27 // |nice_value| among the priorities whose nice values are more than
28 // |nice_value|.
29
30 ThreadPriority best_priority;
31 int best_nice_value = std::numeric_limits<int>::max();
24 for (const ThreadPriorityToNiceValuePair& pair : 32 for (const ThreadPriorityToNiceValuePair& pair :
25 kThreadPriorityToNiceValueMap) { 33 kThreadPriorityToNiceValueMap) {
gab 2016/04/08 15:50:49 If we add a requirement that the kThreadPriorityTo
Yuta Kitamura 2016/04/12 10:18:59 Adopted your first code. Also added a comment for
26 if (pair.nice_value == nice_value) 34 if (pair.nice_value >= nice_value && pair.nice_value < best_nice_value) {
27 return pair.priority; 35 best_priority = pair.priority;
36 best_nice_value = pair.nice_value;
37 }
28 } 38 }
29 NOTREACHED() << "Unknown nice value"; 39 if (best_nice_value != std::numeric_limits<int>::max())
30 return ThreadPriority::NORMAL; 40 return best_priority;
41
42 // Reaching here means |nice_value| is more than any of the defined
43 // priorites. The lowest priority is suitable in this case.
44 return ThreadPriority::BACKGROUND;
31 } 45 }
32 46
33 } // namespace internal 47 } // namespace internal
34 48
35 } // namespace base 49 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698