tor-browser

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

platform_thread_linux.cpp (3319B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 // This is a cut down version of Chromium source file base/threading/platform_thread_linux.h
      8 // with only the functions required. It also has a dummy implementation of
      9 // SetCurrentThreadTypeForPlatform, which should not be called.
     10 
     11 #include "base/threading/platform_thread.h"
     12 
     13 #include "base/message_loop/message_pump_type.h"
     14 #include "base/threading/platform_thread_internal_posix.h"
     15 #include "third_party/abseil-cpp/absl/types/optional.h"
     16 
     17 #include "mozilla/Assertions.h"
     18 
     19 namespace base {
     20 namespace internal {
     21 
     22 const ThreadPriorityToNiceValuePairForTest
     23    kThreadPriorityToNiceValueMapForTest[7] = {
     24        {ThreadPriorityForTest::kRealtimeAudio, -10},
     25        {ThreadPriorityForTest::kDisplay, -8},
     26        {ThreadPriorityForTest::kNormal, 0},
     27        {ThreadPriorityForTest::kResourceEfficient, 1},
     28        {ThreadPriorityForTest::kUtility, 2},
     29        {ThreadPriorityForTest::kBackground, 10},
     30 };
     31 
     32 // These nice values are shared with ChromeOS platform code
     33 // (platform_thread_cros.cc) and have to be unique as ChromeOS has a unique
     34 // type -> nice value mapping. An exception is kCompositing and
     35 // kDisplayCritical where aliasing is OK as they have the same scheduler
     36 // attributes (cpusets, latency_sensitive etc) including nice value.
     37 // The uniqueness of the nice value per-type helps to change and restore the
     38 // scheduling params of threads when their process toggles between FG and BG.
     39 const ThreadTypeToNiceValuePair kThreadTypeToNiceValueMap[7] = {
     40    {ThreadType::kBackground, 10},       {ThreadType::kUtility, 2},
     41    {ThreadType::kResourceEfficient, 1}, {ThreadType::kDefault, 0},
     42 #if BUILDFLAG(IS_CHROMEOS)
     43    {ThreadType::kCompositing, -8},
     44 #else
     45    // TODO(1329208): Experiment with bringing IS_LINUX inline with IS_CHROMEOS.
     46    {ThreadType::kCompositing, -1},
     47 #endif
     48    {ThreadType::kDisplayCritical, -8},  {ThreadType::kRealtimeAudio, -10},
     49 };
     50 
     51 bool SetCurrentThreadTypeForPlatform(ThreadType thread_type,
     52                                     MessagePumpType pump_type_hint) {
     53  MOZ_CRASH();
     54 }
     55 
     56 absl::optional<ThreadPriorityForTest>
     57 GetCurrentThreadPriorityForPlatformForTest() {
     58  int maybe_sched_rr = 0;
     59  struct sched_param maybe_realtime_prio = {0};
     60  if (pthread_getschedparam(pthread_self(), &maybe_sched_rr,
     61                            &maybe_realtime_prio) == 0 &&
     62      maybe_sched_rr == SCHED_RR &&
     63      maybe_realtime_prio.sched_priority ==
     64          PlatformThreadLinux::kRealTimeAudioPrio.sched_priority) {
     65    return absl::make_optional(ThreadPriorityForTest::kRealtimeAudio);
     66  }
     67  return absl::nullopt;
     68 }
     69 
     70 }  // namespace internal
     71 
     72 void InitThreading() {}
     73 
     74 void TerminateOnThread() {}
     75 
     76 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
     77 #if !defined(THREAD_SANITIZER)
     78  return 0;
     79 #else
     80  // ThreadSanitizer bloats the stack heavily. Evidence has been that the
     81  // default stack size isn't enough for some browser tests.
     82  return 2 * (1 << 23);  // 2 times 8192K (the default stack size on Linux).
     83 #endif
     84 }
     85 
     86 }  // namespace base