OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef CONTENT_BROWSER_BROWSER_THREAD_H_ | 5 #ifndef CONTENT_BROWSER_BROWSER_THREAD_H_ |
6 #define CONTENT_BROWSER_BROWSER_THREAD_H_ | 6 #define CONTENT_BROWSER_BROWSER_THREAD_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
11 #include "base/task.h" | 11 #include "base/task.h" |
12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "content/common/content_export.h" |
13 | 14 |
14 #if defined(UNIT_TEST) | 15 #if defined(UNIT_TEST) |
15 #include "base/logging.h" | 16 #include "base/logging.h" |
16 #endif // UNIT_TEST | 17 #endif // UNIT_TEST |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 class MessageLoopProxy; | 20 class MessageLoopProxy; |
20 } | 21 } |
21 | 22 |
22 /////////////////////////////////////////////////////////////////////////////// | 23 /////////////////////////////////////////////////////////////////////////////// |
23 // BrowserThread | 24 // BrowserThread |
24 // | 25 // |
25 // This class represents a thread that is known by a browser-wide name. For | 26 // This class represents a thread that is known by a browser-wide name. For |
26 // example, there is one IO thread for the entire browser process, and various | 27 // example, there is one IO thread for the entire browser process, and various |
27 // pieces of code find it useful to retrieve a pointer to the IO thread's | 28 // pieces of code find it useful to retrieve a pointer to the IO thread's |
28 // Invoke a task by thread ID: | 29 // Invoke a task by thread ID: |
29 // | 30 // |
30 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task); | 31 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task); |
31 // | 32 // |
32 // The return value is false if the task couldn't be posted because the target | 33 // The return value is false if the task couldn't be posted because the target |
33 // thread doesn't exist. If this could lead to data loss, you need to check the | 34 // thread doesn't exist. If this could lead to data loss, you need to check the |
34 // result and restructure the code to ensure it doesn't occur. | 35 // result and restructure the code to ensure it doesn't occur. |
35 // | 36 // |
36 // This class automatically handles the lifetime of different threads. | 37 // This class automatically handles the lifetime of different threads. |
37 // It's always safe to call PostTask on any thread. If it's not yet created, | 38 // It's always safe to call PostTask on any thread. If it's not yet created, |
38 // the task is deleted. There are no race conditions. If the thread that the | 39 // the task is deleted. There are no race conditions. If the thread that the |
39 // task is posted to is guaranteed to outlive the current thread, then no locks | 40 // task is posted to is guaranteed to outlive the current thread, then no locks |
40 // are used. You should never need to cache pointers to MessageLoops, since | 41 // are used. You should never need to cache pointers to MessageLoops, since |
41 // they're not thread safe. | 42 // they're not thread safe. |
42 class BrowserThread : public base::Thread { | 43 class CONTENT_EXPORT BrowserThread : public base::Thread { |
43 public: | 44 public: |
44 // An enumeration of the well-known threads. | 45 // An enumeration of the well-known threads. |
45 // NOTE: threads must be listed in the order of their life-time, with each | 46 // NOTE: threads must be listed in the order of their life-time, with each |
46 // thread outliving every other thread below it. | 47 // thread outliving every other thread below it. |
47 enum ID { | 48 enum ID { |
48 // The main thread in the browser. | 49 // The main thread in the browser. |
49 UI, | 50 UI, |
50 | 51 |
51 // This is the thread that interacts with the database. | 52 // This is the thread that interacts with the database. |
52 DB, | 53 DB, |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 static base::Lock lock_; | 242 static base::Lock lock_; |
242 | 243 |
243 // An array of the BrowserThread objects. This array is protected by |lock_|. | 244 // An array of the BrowserThread objects. This array is protected by |lock_|. |
244 // The threads are not owned by this array. Typically, the threads are owned | 245 // The threads are not owned by this array. Typically, the threads are owned |
245 // on the UI thread by the g_browser_process object. BrowserThreads remove | 246 // on the UI thread by the g_browser_process object. BrowserThreads remove |
246 // themselves from this array upon destruction. | 247 // themselves from this array upon destruction. |
247 static BrowserThread* browser_threads_[ID_COUNT]; | 248 static BrowserThread* browser_threads_[ID_COUNT]; |
248 }; | 249 }; |
249 | 250 |
250 #endif // CONTENT_BROWSER_BROWSER_THREAD_H_ | 251 #endif // CONTENT_BROWSER_BROWSER_THREAD_H_ |
OLD | NEW |