browser_thread_impl.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
1// Copyright (c) 2012 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
5#include "content/browser/browser_thread_impl.h"
6
7#include <string>
8
9#include "base/atomicops.h"
10#include "base/bind.h"
11#include "base/compiler_specific.h"
12#include "base/lazy_instance.h"
13#include "base/message_loop/message_loop.h"
14#include "base/message_loop/message_loop_proxy.h"
15#include "base/threading/sequenced_worker_pool.h"
16#include "base/threading/thread_restrictions.h"
17#include "content/public/browser/browser_thread_delegate.h"
18
19namespace content {
20
21namespace {
22
23// Friendly names for the well-known threads.
24static const char* g_browser_thread_names[BrowserThread::ID_COUNT] = {
25  "",  // UI (name assembled in browser_main.cc).
26  "Chrome_DBThread",  // DB
27  "Chrome_FileThread",  // FILE
28  "Chrome_FileUserBlockingThread",  // FILE_USER_BLOCKING
29  "Chrome_ProcessLauncherThread",  // PROCESS_LAUNCHER
30  "Chrome_CacheThread",  // CACHE
31  "Chrome_IOThread",  // IO
32};
33
34struct BrowserThreadGlobals {
35  BrowserThreadGlobals()
36      : blocking_pool(new base::SequencedWorkerPool(3, "BrowserBlocking")) {
37    memset(threads, 0, BrowserThread::ID_COUNT * sizeof(threads[0]));
38    memset(thread_delegates, 0,
39           BrowserThread::ID_COUNT * sizeof(thread_delegates[0]));
40  }
41
42  // This lock protects |threads|. Do not read or modify that array
43  // without holding this lock. Do not block while holding this lock.
44  base::Lock lock;
45
46  // This array is protected by |lock|. The threads are not owned by this
47  // array. Typically, the threads are owned on the UI thread by
48  // BrowserMainLoop. BrowserThreadImpl objects remove themselves from this
49  // array upon destruction.
50  BrowserThreadImpl* threads[BrowserThread::ID_COUNT];
51
52  // Only atomic operations are used on this array. The delegates are not owned
53  // by this array, rather by whoever calls BrowserThread::SetDelegate.
54  BrowserThreadDelegate* thread_delegates[BrowserThread::ID_COUNT];
55
56  const scoped_refptr<base::SequencedWorkerPool> blocking_pool;
57};
58
59base::LazyInstance<BrowserThreadGlobals>::Leaky
60    g_globals = LAZY_INSTANCE_INITIALIZER;
61
62}  // namespace
63
64BrowserThreadImpl::BrowserThreadImpl(ID identifier)
65    : Thread(g_browser_thread_names[identifier]),
66      identifier_(identifier) {
67  Initialize();
68}
69
70BrowserThreadImpl::BrowserThreadImpl(ID identifier,
71                                     base::MessageLoop* message_loop)
72    : Thread(message_loop->thread_name()), identifier_(identifier) {
73  set_message_loop(message_loop);
74  Initialize();
75}
76
77// static
78void BrowserThreadImpl::ShutdownThreadPool() {
79  // The goal is to make it impossible for chrome to 'infinite loop' during
80  // shutdown, but to reasonably expect that all BLOCKING_SHUTDOWN tasks queued
81  // during shutdown get run. There's nothing particularly scientific about the
82  // number chosen.
83  const int kMaxNewShutdownBlockingTasks = 1000;
84  BrowserThreadGlobals& globals = g_globals.Get();
85  globals.blocking_pool->Shutdown(kMaxNewShutdownBlockingTasks);
86}
87
88// static
89void BrowserThreadImpl::FlushThreadPoolHelper() {
90  // We don't want to create a pool if none exists.
91  if (g_globals == NULL)
92    return;
93  g_globals.Get().blocking_pool->FlushForTesting();
94}
95
96void BrowserThreadImpl::Init() {
97  BrowserThreadGlobals& globals = g_globals.Get();
98
99  using base::subtle::AtomicWord;
100  AtomicWord* storage =
101      reinterpret_cast<AtomicWord*>(&globals.thread_delegates[identifier_]);
102  AtomicWord stored_pointer = base::subtle::NoBarrier_Load(storage);
103  BrowserThreadDelegate* delegate =
104      reinterpret_cast<BrowserThreadDelegate*>(stored_pointer);
105  if (delegate) {
106    delegate->Init();
107    message_loop()->PostTask(FROM_HERE,
108                             base::Bind(&BrowserThreadDelegate::InitAsync,
109                                        // Delegate is expected to exist for the
110                                        // duration of the thread's lifetime
111                                        base::Unretained(delegate)));
112  }
113}
114
115// We disable optimizations for this block of functions so the compiler doesn't
116// merge them all together.
117MSVC_DISABLE_OPTIMIZE()
118MSVC_PUSH_DISABLE_WARNING(4748)
119
120NOINLINE void BrowserThreadImpl::UIThreadRun(base::MessageLoop* message_loop) {
121  volatile int line_number = __LINE__;
122  Thread::Run(message_loop);
123  CHECK_GT(line_number, 0);
124}
125
126NOINLINE void BrowserThreadImpl::DBThreadRun(base::MessageLoop* message_loop) {
127  volatile int line_number = __LINE__;
128  Thread::Run(message_loop);
129  CHECK_GT(line_number, 0);
130}
131
132NOINLINE void BrowserThreadImpl::FileThreadRun(
133    base::MessageLoop* message_loop) {
134  volatile int line_number = __LINE__;
135  Thread::Run(message_loop);
136  CHECK_GT(line_number, 0);
137}
138
139NOINLINE void BrowserThreadImpl::FileUserBlockingThreadRun(
140    base::MessageLoop* message_loop) {
141  volatile int line_number = __LINE__;
142  Thread::Run(message_loop);
143  CHECK_GT(line_number, 0);
144}
145
146NOINLINE void BrowserThreadImpl::ProcessLauncherThreadRun(
147    base::MessageLoop* message_loop) {
148  volatile int line_number = __LINE__;
149  Thread::Run(message_loop);
150  CHECK_GT(line_number, 0);
151}
152
153NOINLINE void BrowserThreadImpl::CacheThreadRun(
154    base::MessageLoop* message_loop) {
155  volatile int line_number = __LINE__;
156  Thread::Run(message_loop);
157  CHECK_GT(line_number, 0);
158}
159
160NOINLINE void BrowserThreadImpl::IOThreadRun(base::MessageLoop* message_loop) {
161  volatile int line_number = __LINE__;
162  Thread::Run(message_loop);
163  CHECK_GT(line_number, 0);
164}
165
166MSVC_POP_WARNING()
167MSVC_ENABLE_OPTIMIZE();
168
169void BrowserThreadImpl::Run(base::MessageLoop* message_loop) {
170  BrowserThread::ID thread_id = ID_COUNT;
171  if (!GetCurrentThreadIdentifier(&thread_id))
172    return Thread::Run(message_loop);
173
174  switch (thread_id) {
175    case BrowserThread::UI:
176      return UIThreadRun(message_loop);
177    case BrowserThread::DB:
178      return DBThreadRun(message_loop);
179    case BrowserThread::FILE:
180      return FileThreadRun(message_loop);
181    case BrowserThread::FILE_USER_BLOCKING:
182      return FileUserBlockingThreadRun(message_loop);
183    case BrowserThread::PROCESS_LAUNCHER:
184      return ProcessLauncherThreadRun(message_loop);
185    case BrowserThread::CACHE:
186      return CacheThreadRun(message_loop);
187    case BrowserThread::IO:
188      return IOThreadRun(message_loop);
189    case BrowserThread::ID_COUNT:
190      CHECK(false);  // This shouldn't actually be reached!
191      break;
192  }
193  Thread::Run(message_loop);
194}
195
196void BrowserThreadImpl::CleanUp() {
197  BrowserThreadGlobals& globals = g_globals.Get();
198
199  using base::subtle::AtomicWord;
200  AtomicWord* storage =
201      reinterpret_cast<AtomicWord*>(&globals.thread_delegates[identifier_]);
202  AtomicWord stored_pointer = base::subtle::NoBarrier_Load(storage);
203  BrowserThreadDelegate* delegate =
204      reinterpret_cast<BrowserThreadDelegate*>(stored_pointer);
205
206  if (delegate)
207    delegate->CleanUp();
208}
209
210void BrowserThreadImpl::Initialize() {
211  BrowserThreadGlobals& globals = g_globals.Get();
212
213  base::AutoLock lock(globals.lock);
214  DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT);
215  DCHECK(globals.threads[identifier_] == NULL);
216  globals.threads[identifier_] = this;
217}
218
219BrowserThreadImpl::~BrowserThreadImpl() {
220  // All Thread subclasses must call Stop() in the destructor. This is
221  // doubly important here as various bits of code check they are on
222  // the right BrowserThread.
223  Stop();
224
225  BrowserThreadGlobals& globals = g_globals.Get();
226  base::AutoLock lock(globals.lock);
227  globals.threads[identifier_] = NULL;
228#ifndef NDEBUG
229  // Double check that the threads are ordered correctly in the enumeration.
230  for (int i = identifier_ + 1; i < ID_COUNT; ++i) {
231    DCHECK(!globals.threads[i]) <<
232        "Threads must be listed in the reverse order that they die";
233  }
234#endif
235}
236
237// static
238bool BrowserThreadImpl::PostTaskHelper(
239    BrowserThread::ID identifier,
240    const tracked_objects::Location& from_here,
241    const base::Closure& task,
242    base::TimeDelta delay,
243    bool nestable) {
244  DCHECK(identifier >= 0 && identifier < ID_COUNT);
245  // Optimization: to avoid unnecessary locks, we listed the ID enumeration in
246  // order of lifetime.  So no need to lock if we know that the target thread
247  // outlives current thread.
248  // Note: since the array is so small, ok to loop instead of creating a map,
249  // which would require a lock because std::map isn't thread safe, defeating
250  // the whole purpose of this optimization.
251  BrowserThread::ID current_thread = ID_COUNT;
252  bool target_thread_outlives_current =
253      GetCurrentThreadIdentifier(&current_thread) &&
254      current_thread >= identifier;
255
256  BrowserThreadGlobals& globals = g_globals.Get();
257  if (!target_thread_outlives_current)
258    globals.lock.Acquire();
259
260  base::MessageLoop* message_loop =
261      globals.threads[identifier] ? globals.threads[identifier]->message_loop()
262                                  : NULL;
263  if (message_loop) {
264    if (nestable) {
265      message_loop->PostDelayedTask(from_here, task, delay);
266    } else {
267      message_loop->PostNonNestableDelayedTask(from_here, task, delay);
268    }
269  }
270
271  if (!target_thread_outlives_current)
272    globals.lock.Release();
273
274  return !!message_loop;
275}
276
277// An implementation of MessageLoopProxy to be used in conjunction
278// with BrowserThread.
279class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy {
280 public:
281  explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier)
282      : id_(identifier) {
283  }
284
285  // MessageLoopProxy implementation.
286  virtual bool PostDelayedTask(
287      const tracked_objects::Location& from_here,
288      const base::Closure& task, base::TimeDelta delay) OVERRIDE {
289    return BrowserThread::PostDelayedTask(id_, from_here, task, delay);
290  }
291
292  virtual bool PostNonNestableDelayedTask(
293      const tracked_objects::Location& from_here,
294      const base::Closure& task,
295      base::TimeDelta delay) OVERRIDE {
296    return BrowserThread::PostNonNestableDelayedTask(id_, from_here, task,
297                                                     delay);
298  }
299
300  virtual bool RunsTasksOnCurrentThread() const OVERRIDE {
301    return BrowserThread::CurrentlyOn(id_);
302  }
303
304 protected:
305  virtual ~BrowserThreadMessageLoopProxy() {}
306
307 private:
308  BrowserThread::ID id_;
309  DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy);
310};
311
312// static
313bool BrowserThread::PostBlockingPoolTask(
314    const tracked_objects::Location& from_here,
315    const base::Closure& task) {
316  return g_globals.Get().blocking_pool->PostWorkerTask(from_here, task);
317}
318
319// static
320bool BrowserThread::PostBlockingPoolTaskAndReply(
321    const tracked_objects::Location& from_here,
322    const base::Closure& task,
323    const base::Closure& reply) {
324  return g_globals.Get().blocking_pool->PostTaskAndReply(
325      from_here, task, reply);
326}
327
328// static
329bool BrowserThread::PostBlockingPoolSequencedTask(
330    const std::string& sequence_token_name,
331    const tracked_objects::Location& from_here,
332    const base::Closure& task) {
333  return g_globals.Get().blocking_pool->PostNamedSequencedWorkerTask(
334      sequence_token_name, from_here, task);
335}
336
337// static
338base::SequencedWorkerPool* BrowserThread::GetBlockingPool() {
339  return g_globals.Get().blocking_pool.get();
340}
341
342// static
343bool BrowserThread::IsThreadInitialized(ID identifier) {
344  if (g_globals == NULL)
345    return false;
346
347  BrowserThreadGlobals& globals = g_globals.Get();
348  base::AutoLock lock(globals.lock);
349  DCHECK(identifier >= 0 && identifier < ID_COUNT);
350  return globals.threads[identifier] != NULL;
351}
352
353// static
354bool BrowserThread::CurrentlyOn(ID identifier) {
355  // We shouldn't use MessageLoop::current() since it uses LazyInstance which
356  // may be deleted by ~AtExitManager when a WorkerPool thread calls this
357  // function.
358  // http://crbug.com/63678
359  base::ThreadRestrictions::ScopedAllowSingleton allow_singleton;
360  BrowserThreadGlobals& globals = g_globals.Get();
361  base::AutoLock lock(globals.lock);
362  DCHECK(identifier >= 0 && identifier < ID_COUNT);
363  return globals.threads[identifier] &&
364         globals.threads[identifier]->message_loop() ==
365             base::MessageLoop::current();
366}
367
368static const char* GetThreadName(BrowserThread::ID thread) {
369  if (BrowserThread::UI < thread && thread < BrowserThread::ID_COUNT)
370    return g_browser_thread_names[thread];
371  if (thread == BrowserThread::UI)
372    return "Chrome_UIThread";
373  return "Unknown Thread";
374}
375
376// static
377std::string BrowserThread::GetDCheckCurrentlyOnErrorMessage(ID expected) {
378  const std::string& message_loop_name =
379      base::MessageLoop::current()->thread_name();
380  ID actual_browser_thread;
381  const char* actual_name = "Unknown Thread";
382  if (!message_loop_name.empty()) {
383    actual_name = message_loop_name.c_str();
384  } else if (GetCurrentThreadIdentifier(&actual_browser_thread)) {
385    actual_name = GetThreadName(actual_browser_thread);
386  }
387  std::string result = "Must be called on ";
388  result += GetThreadName(expected);
389  result += "; actually called on ";
390  result += actual_name;
391  result += ".";
392  return result;
393}
394
395// static
396bool BrowserThread::IsMessageLoopValid(ID identifier) {
397  if (g_globals == NULL)
398    return false;
399
400  BrowserThreadGlobals& globals = g_globals.Get();
401  base::AutoLock lock(globals.lock);
402  DCHECK(identifier >= 0 && identifier < ID_COUNT);
403  return globals.threads[identifier] &&
404         globals.threads[identifier]->message_loop();
405}
406
407// static
408bool BrowserThread::PostTask(ID identifier,
409                             const tracked_objects::Location& from_here,
410                             const base::Closure& task) {
411  return BrowserThreadImpl::PostTaskHelper(
412      identifier, from_here, task, base::TimeDelta(), true);
413}
414
415// static
416bool BrowserThread::PostDelayedTask(ID identifier,
417                                    const tracked_objects::Location& from_here,
418                                    const base::Closure& task,
419                                    base::TimeDelta delay) {
420  return BrowserThreadImpl::PostTaskHelper(
421      identifier, from_here, task, delay, true);
422}
423
424// static
425bool BrowserThread::PostNonNestableTask(
426    ID identifier,
427    const tracked_objects::Location& from_here,
428    const base::Closure& task) {
429  return BrowserThreadImpl::PostTaskHelper(
430      identifier, from_here, task, base::TimeDelta(), false);
431}
432
433// static
434bool BrowserThread::PostNonNestableDelayedTask(
435    ID identifier,
436    const tracked_objects::Location& from_here,
437    const base::Closure& task,
438    base::TimeDelta delay) {
439  return BrowserThreadImpl::PostTaskHelper(
440      identifier, from_here, task, delay, false);
441}
442
443// static
444bool BrowserThread::PostTaskAndReply(
445    ID identifier,
446    const tracked_objects::Location& from_here,
447    const base::Closure& task,
448    const base::Closure& reply) {
449  return GetMessageLoopProxyForThread(identifier)->PostTaskAndReply(from_here,
450                                                                    task,
451                                                                    reply);
452}
453
454// static
455bool BrowserThread::GetCurrentThreadIdentifier(ID* identifier) {
456  if (g_globals == NULL)
457    return false;
458
459  // We shouldn't use MessageLoop::current() since it uses LazyInstance which
460  // may be deleted by ~AtExitManager when a WorkerPool thread calls this
461  // function.
462  // http://crbug.com/63678
463  base::ThreadRestrictions::ScopedAllowSingleton allow_singleton;
464  base::MessageLoop* cur_message_loop = base::MessageLoop::current();
465  BrowserThreadGlobals& globals = g_globals.Get();
466  for (int i = 0; i < ID_COUNT; ++i) {
467    if (globals.threads[i] &&
468        globals.threads[i]->message_loop() == cur_message_loop) {
469      *identifier = globals.threads[i]->identifier_;
470      return true;
471    }
472  }
473
474  return false;
475}
476
477// static
478scoped_refptr<base::MessageLoopProxy>
479BrowserThread::GetMessageLoopProxyForThread(ID identifier) {
480  return make_scoped_refptr(new BrowserThreadMessageLoopProxy(identifier));
481}
482
483// static
484base::MessageLoop* BrowserThread::UnsafeGetMessageLoopForThread(ID identifier) {
485  if (g_globals == NULL)
486    return NULL;
487
488  BrowserThreadGlobals& globals = g_globals.Get();
489  base::AutoLock lock(globals.lock);
490  base::Thread* thread = globals.threads[identifier];
491  DCHECK(thread);
492  base::MessageLoop* loop = thread->message_loop();
493  return loop;
494}
495
496// static
497void BrowserThread::SetDelegate(ID identifier,
498                                BrowserThreadDelegate* delegate) {
499  using base::subtle::AtomicWord;
500  BrowserThreadGlobals& globals = g_globals.Get();
501  AtomicWord* storage = reinterpret_cast<AtomicWord*>(
502      &globals.thread_delegates[identifier]);
503  AtomicWord old_pointer = base::subtle::NoBarrier_AtomicExchange(
504      storage, reinterpret_cast<AtomicWord>(delegate));
505
506  // This catches registration when previously registered.
507  DCHECK(!delegate || !old_pointer);
508}
509
510}  // namespace content
511