• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 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 #include "base/threading/platform_thread_internal_posix.h"
6 
7 #include <errno.h>
8 #include <sys/resource.h>
9 
10 #include <ostream>
11 
12 #include "base/containers/adapters.h"
13 #include "base/logging.h"
14 #include "base/notimplemented.h"
15 #include "base/notreached.h"
16 
17 namespace base {
18 
19 namespace internal {
20 
ThreadTypeToNiceValue(ThreadType thread_type)21 BASE_EXPORT int ThreadTypeToNiceValue(ThreadType thread_type) {
22   for (const auto& pair : kThreadTypeToNiceValueMap) {
23     if (pair.thread_type == thread_type)
24       return pair.nice_value;
25   }
26   NOTREACHED() << "Unknown ThreadType";
27 }
28 
NiceValueToThreadPriorityForTest(int nice_value)29 ThreadPriorityForTest NiceValueToThreadPriorityForTest(int nice_value) {
30   // Try to find a priority that best describes |nice_value|. If there isn't
31   // an exact match, this method returns the closest priority whose nice value
32   // is higher (lower priority) than |nice_value|.
33   for (const auto& pair : kThreadPriorityToNiceValueMapForTest) {
34     if (pair.nice_value >= nice_value)
35       return pair.priority;
36   }
37 
38   // Reaching here means |nice_value| is more than any of the defined
39   // priorities. The lowest priority is suitable in this case.
40   return ThreadPriorityForTest::kBackground;
41 }
42 
GetCurrentThreadNiceValue()43 int GetCurrentThreadNiceValue() {
44 #if BUILDFLAG(IS_NACL)
45   NOTIMPLEMENTED();
46   return 0;
47 #else
48 
49   // Need to clear errno before calling getpriority():
50   // http://man7.org/linux/man-pages/man2/getpriority.2.html
51   errno = 0;
52   int nice_value = getpriority(PRIO_PROCESS, 0);
53   if (errno != 0) {
54     DVPLOG(1) << "Failed to get nice value of thread ("
55               << PlatformThread::CurrentId() << ")";
56     return 0;
57   }
58 
59   return nice_value;
60 #endif
61 }
62 
63 }  // namespace internal
64 
65 }  // namespace base
66