OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chrome_thread.h" | 5 #include "chrome/browser/chrome_thread.h" |
6 | 6 |
7 // Friendly names for the well-known threads. | 7 // Friendly names for the well-known threads. |
8 static const char* chrome_thread_names[ChromeThread::ID_COUNT] = { | 8 static const char* chrome_thread_names[ChromeThread::ID_COUNT] = { |
| 9 "", // UI (name assembled in browser_main.cc). |
9 "Chrome_IOThread", // IO | 10 "Chrome_IOThread", // IO |
10 "Chrome_FileThread", // FILE | 11 "Chrome_FileThread", // FILE |
11 "Chrome_DBThread", // DB | 12 "Chrome_DBThread", // DB |
12 "Chrome_WebKitThread", // WEBKIT | 13 "Chrome_WebKitThread", // WEBKIT |
13 "Chrome_HistoryThread", // HISTORY | 14 "Chrome_HistoryThread", // HISTORY |
14 #if defined(OS_LINUX) | 15 #if defined(OS_LINUX) |
15 "Chrome_Background_X11Thread", // BACKGROUND_X11 | 16 "Chrome_Background_X11Thread", // BACKGROUND_X11 |
16 #endif | 17 #endif |
17 }; | 18 }; |
18 | 19 |
19 Lock ChromeThread::lock_; | 20 Lock ChromeThread::lock_; |
20 | 21 |
21 ChromeThread* ChromeThread::chrome_threads_[ID_COUNT] = { | 22 ChromeThread* ChromeThread::chrome_threads_[ID_COUNT] = { |
| 23 NULL, // UI |
22 NULL, // IO | 24 NULL, // IO |
23 NULL, // FILE | 25 NULL, // FILE |
24 NULL, // DB | 26 NULL, // DB |
25 NULL, // WEBKIT | 27 NULL, // WEBKIT |
26 NULL, // HISTORY | 28 NULL, // HISTORY |
27 #if defined(OS_LINUX) | 29 #if defined(OS_LINUX) |
28 NULL, // BACKGROUND_X11 | 30 NULL, // BACKGROUND_X11 |
29 #endif | 31 #endif |
30 }; | 32 }; |
31 | 33 |
32 ChromeThread::ChromeThread(ChromeThread::ID identifier) | 34 ChromeThread::ChromeThread(ChromeThread::ID identifier) |
33 : Thread(chrome_thread_names[identifier]), | 35 : Thread(chrome_thread_names[identifier]), |
34 identifier_(identifier) { | 36 identifier_(identifier) { |
| 37 Initialize(); |
| 38 } |
| 39 |
| 40 ChromeThread::ChromeThread() |
| 41 : Thread(MessageLoop::current()->thread_name().c_str()), |
| 42 identifier_(UI) { |
| 43 set_message_loop(MessageLoop::current()); |
| 44 Initialize(); |
| 45 } |
| 46 |
| 47 void ChromeThread::Initialize() { |
35 AutoLock lock(lock_); | 48 AutoLock lock(lock_); |
36 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 49 DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT); |
37 DCHECK(chrome_threads_[identifier] == NULL); | 50 DCHECK(chrome_threads_[identifier_] == NULL); |
38 chrome_threads_[identifier_] = this; | 51 chrome_threads_[identifier_] = this; |
39 } | 52 } |
40 | 53 |
41 ChromeThread::~ChromeThread() { | 54 ChromeThread::~ChromeThread() { |
42 AutoLock lock(lock_); | 55 AutoLock lock(lock_); |
43 chrome_threads_[identifier_] = NULL; | 56 chrome_threads_[identifier_] = NULL; |
44 } | 57 } |
45 | 58 |
46 // static | 59 // static |
47 MessageLoop* ChromeThread::GetMessageLoop(ID identifier) { | 60 MessageLoop* ChromeThread::GetMessageLoop(ID identifier) { |
48 AutoLock lock(lock_); | 61 AutoLock lock(lock_); |
49 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 62 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
50 | 63 |
51 if (chrome_threads_[identifier]) | 64 if (chrome_threads_[identifier]) |
52 return chrome_threads_[identifier]->message_loop(); | 65 return chrome_threads_[identifier]->message_loop(); |
53 | 66 |
54 return NULL; | 67 return NULL; |
55 } | 68 } |
56 | 69 |
57 // static | 70 // static |
58 bool ChromeThread::CurrentlyOn(ID identifier) { | 71 bool ChromeThread::CurrentlyOn(ID identifier) { |
59 // MessageLoop::current() will return NULL if none is running. This is often | 72 // MessageLoop::current() will return NULL if none is running. This is often |
60 // true when running under unit tests. This behavior actually works out | 73 // true when running under unit tests. This behavior actually works out |
61 // pretty convienently (as is mentioned in the header file comment), but it's | 74 // pretty convienently (as is mentioned in the header file comment), but it's |
62 // worth noting here. | 75 // worth noting here. |
63 MessageLoop* message_loop = GetMessageLoop(identifier); | 76 MessageLoop* message_loop = GetMessageLoop(identifier); |
64 return MessageLoop::current() == message_loop; | 77 return MessageLoop::current() == message_loop; |
65 } | 78 } |
| 79 |
OLD | NEW |