summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chrome_thread.cc
blob: 8cd776765187a536fad5901f5f7975fe80489dd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chrome_thread.h"

// Friendly names for the well-known threads.
static const char* chrome_thread_names[ChromeThread::ID_COUNT] = {
  "",  // UI (name assembled in browser_main.cc).
  "Chrome_IOThread",  // IO
  "Chrome_FileThread",  // FILE
  "Chrome_DBThread",  // DB
  "Chrome_WebKitThread",  // WEBKIT
#if defined(OS_LINUX)
  "Chrome_Background_X11Thread",  // BACKGROUND_X11
#endif
};

Lock ChromeThread::lock_;

ChromeThread* ChromeThread::chrome_threads_[ID_COUNT] = {
  NULL,  // UI
  NULL,  // IO
  NULL,  // FILE
  NULL,  // DB
  NULL,  // WEBKIT
#if defined(OS_LINUX)
  NULL,  // BACKGROUND_X11
#endif
};

ChromeThread::ChromeThread(ChromeThread::ID identifier)
    : Thread(chrome_thread_names[identifier]),
      identifier_(identifier) {
  Initialize();
}

ChromeThread::ChromeThread()
    : Thread(MessageLoop::current()->thread_name().c_str()),
      identifier_(UI) {
  set_message_loop(MessageLoop::current());
  Initialize();
}

void ChromeThread::Initialize() {
  AutoLock lock(lock_);
  DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT);
  DCHECK(chrome_threads_[identifier_] == NULL);
  chrome_threads_[identifier_] = this;
}

ChromeThread::~ChromeThread() {
  AutoLock lock(lock_);
  chrome_threads_[identifier_] = NULL;
}

// static
MessageLoop* ChromeThread::GetMessageLoop(ID identifier) {
  AutoLock lock(lock_);
  DCHECK(identifier >= 0 && identifier < ID_COUNT);

  if (chrome_threads_[identifier])
    return chrome_threads_[identifier]->message_loop();

  return NULL;
}

// static
bool ChromeThread::CurrentlyOn(ID identifier) {
  // MessageLoop::current() will return NULL if none is running.  This is often
  // true when running under unit tests.  This behavior actually works out
  // pretty convienently (as is mentioned in the header file comment), but it's
  // worth noting here.
  MessageLoop* message_loop = GetMessageLoop(identifier);
  return MessageLoop::current() == message_loop;
}