platform_thread_internal_posix.cc 1.7 KB

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