| 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 #ifndef CHROME_BROWSER_CHROME_THREAD_H__ | 5 #ifndef CHROME_BROWSER_CHROME_THREAD_H__ |
| 6 #define CHROME_BROWSER_CHROME_THREAD_H__ | 6 #define CHROME_BROWSER_CHROME_THREAD_H__ |
| 7 | 7 |
| 8 #include "base/lock.h" | 8 #include "base/lock.h" |
| 9 #include "base/thread.h" | 9 #include "base/thread.h" |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // thread via the g_browser_process object, e.g. g_browser_process->io_thread(); | 22 // thread via the g_browser_process object, e.g. g_browser_process->io_thread(); |
| 23 // | 23 // |
| 24 // Code that runs on a thread other than the UI thread must take extra care in | 24 // Code that runs on a thread other than the UI thread must take extra care in |
| 25 // handling pointers to threads because many of the well-known threads are owned | 25 // handling pointers to threads because many of the well-known threads are owned |
| 26 // by the UI thread and can be deallocated without notice. | 26 // by the UI thread and can be deallocated without notice. |
| 27 // | 27 // |
| 28 class ChromeThread : public base::Thread { | 28 class ChromeThread : public base::Thread { |
| 29 public: | 29 public: |
| 30 // An enumeration of the well-known threads. | 30 // An enumeration of the well-known threads. |
| 31 enum ID { | 31 enum ID { |
| 32 // The main thread in the browser. |
| 33 UI, |
| 34 |
| 32 // This is the thread that processes IPC and network messages. | 35 // This is the thread that processes IPC and network messages. |
| 33 IO, | 36 IO, |
| 34 | 37 |
| 35 // This is the thread that interacts with the file system. | 38 // This is the thread that interacts with the file system. |
| 36 FILE, | 39 FILE, |
| 37 | 40 |
| 38 // This is the thread that interacts with the database. | 41 // This is the thread that interacts with the database. |
| 39 DB, | 42 DB, |
| 40 | 43 |
| 41 // This is the "main" thread for WebKit within the browser process when | 44 // This is the "main" thread for WebKit within the browser process when |
| (...skipping 12 matching lines...) Expand all Loading... |
| 54 | 57 |
| 55 // This identifier does not represent a thread. Instead it counts the | 58 // This identifier does not represent a thread. Instead it counts the |
| 56 // number of well-known threads. Insert new well-known threads before this | 59 // number of well-known threads. Insert new well-known threads before this |
| 57 // identifier. | 60 // identifier. |
| 58 ID_COUNT | 61 ID_COUNT |
| 59 }; | 62 }; |
| 60 | 63 |
| 61 // Construct a ChromeThread with the supplied identifier. It is an error | 64 // Construct a ChromeThread with the supplied identifier. It is an error |
| 62 // to construct a ChromeThread that already exists. | 65 // to construct a ChromeThread that already exists. |
| 63 explicit ChromeThread(ID identifier); | 66 explicit ChromeThread(ID identifier); |
| 67 |
| 68 // Special constructor for the main (UI) thread. We use a dummy thread here |
| 69 // since the main thread already exists. |
| 70 ChromeThread(); |
| 71 |
| 64 virtual ~ChromeThread(); | 72 virtual ~ChromeThread(); |
| 65 | 73 |
| 66 // Callable on any thread, this helper function returns a pointer to the | 74 // Callable on any thread, this helper function returns a pointer to the |
| 67 // thread's MessageLoop. | 75 // thread's MessageLoop. |
| 68 // | 76 // |
| 69 // WARNING: | 77 // WARNING: |
| 70 // Nothing in this class prevents the MessageLoop object returned from this | 78 // Nothing in this class prevents the MessageLoop object returned from this |
| 71 // function from being destroyed on another thread. Use with care. | 79 // function from being destroyed on another thread. Use with care. |
| 72 // | 80 // |
| 73 static MessageLoop* GetMessageLoop(ID identifier); | 81 static MessageLoop* GetMessageLoop(ID identifier); |
| 74 | 82 |
| 75 // Callable on any thread. Returns whether you're currently on a particular | 83 // Callable on any thread. Returns whether you're currently on a particular |
| 76 // thread. | 84 // thread. |
| 77 // | 85 // |
| 78 // WARNING: | 86 // WARNING: |
| 79 // When running under unit-tests, this will return true if you're on the | 87 // When running under unit-tests, this will return true if you're on the |
| 80 // main thread and the thread ID you pass in isn't running. This is | 88 // main thread and the thread ID you pass in isn't running. This is |
| 81 // normally the correct behavior because you want to ignore these asserts | 89 // normally the correct behavior because you want to ignore these asserts |
| 82 // unless you've specifically spun up the threads, but be mindful of it. | 90 // unless you've specifically spun up the threads, but be mindful of it. |
| 83 static bool CurrentlyOn(ID identifier); | 91 static bool CurrentlyOn(ID identifier); |
| 84 | 92 |
| 85 private: | 93 private: |
| 94 // Common initialization code for the constructors. |
| 95 void Initialize(); |
| 96 |
| 86 // The identifier of this thread. Only one thread can exist with a given | 97 // The identifier of this thread. Only one thread can exist with a given |
| 87 // identifier at a given time. | 98 // identifier at a given time. |
| 88 ID identifier_; | 99 ID identifier_; |
| 89 | 100 |
| 90 // This lock protects |chrome_threads_|. Do not read or modify that array | 101 // This lock protects |chrome_threads_|. Do not read or modify that array |
| 91 // without holding this lock. Do not block while holding this lock. | 102 // without holding this lock. Do not block while holding this lock. |
| 92 static Lock lock_; | 103 static Lock lock_; |
| 93 | 104 |
| 94 // An array of the ChromeThread objects. This array is protected by |lock_|. | 105 // An array of the ChromeThread objects. This array is protected by |lock_|. |
| 95 // The threads are not owned by this array. Typically, the threads are owned | 106 // The threads are not owned by this array. Typically, the threads are owned |
| 96 // on the UI thread by the g_browser_process object. ChromeThreads remove | 107 // on the UI thread by the g_browser_process object. ChromeThreads remove |
| 97 // themselves from this array upon destruction. | 108 // themselves from this array upon destruction. |
| 98 static ChromeThread* chrome_threads_[ID_COUNT]; | 109 static ChromeThread* chrome_threads_[ID_COUNT]; |
| 99 }; | 110 }; |
| 100 | 111 |
| 101 #endif // #ifndef CHROME_BROWSER_CHROME_THREAD_H__ | 112 #endif // #ifndef CHROME_BROWSER_CHROME_THREAD_H__ |
| OLD | NEW |