Don't preload rarely seen large images
[chromium-blink-merge.git] / sandbox / linux / services / thread_helpers.cc
blob80766a9bc5dc970666efbb5c3e7ff21ad646c4ff
1 // Copyright 2014 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.
5 #include "sandbox/linux/services/thread_helpers.h"
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <signal.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
14 #include <string>
16 #include "base/bind.h"
17 #include "base/callback.h"
18 #include "base/files/scoped_file.h"
19 #include "base/logging.h"
20 #include "base/posix/eintr_wrapper.h"
21 #include "base/strings/string_number_conversions.h"
22 #include "base/threading/platform_thread.h"
23 #include "base/threading/thread.h"
24 #include "sandbox/linux/services/proc_util.h"
26 namespace sandbox {
28 namespace {
30 const char kAssertSingleThreadedError[] =
31 "Current process is not mono-threaded!";
33 bool IsSingleThreadedImpl(int proc_fd) {
34 CHECK_LE(0, proc_fd);
35 struct stat task_stat;
36 int fstat_ret = fstatat(proc_fd, "self/task/", &task_stat, 0);
37 PCHECK(0 == fstat_ret);
39 // At least "..", "." and the current thread should be present.
40 CHECK_LE(3UL, task_stat.st_nlink);
41 // Counting threads via /proc/self/task could be racy. For the purpose of
42 // determining if the current proces is monothreaded it works: if at any
43 // time it becomes monothreaded, it'll stay so.
44 return task_stat.st_nlink == 3;
47 bool IsThreadPresentInProcFS(int proc_fd,
48 const std::string& thread_id_dir_str) {
49 struct stat task_stat;
50 const int fstat_ret =
51 fstatat(proc_fd, thread_id_dir_str.c_str(), &task_stat, 0);
52 if (fstat_ret < 0) {
53 PCHECK(ENOENT == errno);
54 return false;
56 return true;
59 // Run |cb| in a loop until it returns false. Every time |cb| runs, sleep
60 // for an exponentially increasing amount of time. |cb| is expected to return
61 // false very quickly and this will crash if it doesn't happen within ~64ms on
62 // Debug builds (2s on Release builds).
63 // This is guaranteed to not sleep more than twice as much as the bare minimum
64 // amount of time.
65 void RunWhileTrue(const base::Callback<bool(void)>& cb) {
66 #if defined(NDEBUG)
67 // In Release mode, crash after 30 iterations, which means having spent
68 // roughly 2s in
69 // nanosleep(2) cumulatively.
70 const unsigned int kMaxIterations = 30U;
71 #else
72 // In practice, this never goes through more than a couple iterations. In
73 // debug mode, crash after 64ms (+ eventually 25 times the granularity of
74 // the clock) in nanosleep(2). This ensures that this is not becoming too
75 // slow.
76 const unsigned int kMaxIterations = 25U;
77 #endif
79 // Run |cb| with an exponential back-off, sleeping 2^iterations nanoseconds
80 // in nanosleep(2).
81 // Note: the clock may not allow for nanosecond granularity, in this case the
82 // first iterations would sleep a tiny bit more instead, which would not
83 // change the calculations significantly.
84 for (unsigned int i = 0; i < kMaxIterations; ++i) {
85 if (!cb.Run()) {
86 return;
89 // Increase the waiting time exponentially.
90 struct timespec ts = {0, 1L << i /* nanoseconds */};
91 PCHECK(0 == HANDLE_EINTR(nanosleep(&ts, &ts)));
94 LOG(FATAL) << kAssertSingleThreadedError << " (iterations: " << kMaxIterations
95 << ")";
97 NOTREACHED();
100 bool IsMultiThreaded(int proc_fd) {
101 return !ThreadHelpers::IsSingleThreaded(proc_fd);
104 } // namespace
106 // static
107 bool ThreadHelpers::IsSingleThreaded(int proc_fd) {
108 DCHECK_LE(0, proc_fd);
109 return IsSingleThreadedImpl(proc_fd);
112 // static
113 bool ThreadHelpers::IsSingleThreaded() {
114 base::ScopedFD task_fd(ProcUtil::OpenProc());
115 return IsSingleThreaded(task_fd.get());
118 // static
119 void ThreadHelpers::AssertSingleThreaded(int proc_fd) {
120 DCHECK_LE(0, proc_fd);
121 const base::Callback<bool(void)> cb = base::Bind(&IsMultiThreaded, proc_fd);
122 RunWhileTrue(cb);
125 void ThreadHelpers::AssertSingleThreaded() {
126 base::ScopedFD task_fd(ProcUtil::OpenProc());
127 AssertSingleThreaded(task_fd.get());
130 // static
131 bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_fd,
132 base::Thread* thread) {
133 DCHECK_LE(0, proc_fd);
134 DCHECK(thread);
135 const base::PlatformThreadId thread_id = thread->thread_id();
136 const std::string thread_id_dir_str =
137 "self/task/" + base::IntToString(thread_id) + "/";
139 // The kernel is at liberty to wake the thread id futex before updating
140 // /proc. Following Stop(), the thread is joined, but entries in /proc may
141 // not have been updated.
142 thread->Stop();
144 const base::Callback<bool(void)> cb =
145 base::Bind(&IsThreadPresentInProcFS, proc_fd, thread_id_dir_str);
147 RunWhileTrue(cb);
149 return true;
152 // static
153 const char* ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests() {
154 return kAssertSingleThreadedError;
157 } // namespace sandbox