tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

platform_thread_internal_posix.cc (1752B)


      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/notreached.h"
     15 
     16 namespace base {
     17 
     18 namespace internal {
     19 
     20 BASE_EXPORT int ThreadTypeToNiceValue(ThreadType thread_type) {
     21  for (const auto& pair : kThreadTypeToNiceValueMap) {
     22    if (pair.thread_type == thread_type)
     23      return pair.nice_value;
     24  }
     25  NOTREACHED() << "Unknown ThreadType";
     26  return 0;
     27 }
     28 
     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 
     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