| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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 #ifndef CHROME_BROWSER_BROWSER_THREAD_H_ |
| 6 #define CHROME_BROWSER_BROWSER_THREAD_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/lock.h" |
| 10 #include "base/task.h" |
| 11 #include "base/thread.h" |
| 12 |
| 13 namespace base { |
| 14 class MessageLoopProxy; |
| 15 } |
| 16 |
| 17 /////////////////////////////////////////////////////////////////////////////// |
| 18 // ChromeThread |
| 19 // |
| 20 // This class represents a thread that is known by a browser-wide name. For |
| 21 // example, there is one IO thread for the entire browser process, and various |
| 22 // pieces of code find it useful to retrieve a pointer to the IO thread's |
| 23 // Invoke a task by thread ID: |
| 24 // |
| 25 // ChromeThread::PostTask(ChromeThread::IO, FROM_HERE, task); |
| 26 // |
| 27 // The return value is false if the task couldn't be posted because the target |
| 28 // thread doesn't exist. If this could lead to data loss, you need to check the |
| 29 // result and restructure the code to ensure it doesn't occur. |
| 30 // |
| 31 // This class automatically handles the lifetime of different threads. |
| 32 // It's always safe to call PostTask on any thread. If it's not yet created, |
| 33 // the task is deleted. There are no race conditions. If the thread that the |
| 34 // task is posted to is guaranteed to outlive the current thread, then no locks |
| 35 // are used. You should never need to cache pointers to MessageLoops, since |
| 36 // they're not thread safe. |
| 37 class ChromeThread : public base::Thread { |
| 38 public: |
| 39 // An enumeration of the well-known threads. |
| 40 // NOTE: threads must be listed in the order of their life-time, with each |
| 41 // thread outliving every other thread below it. |
| 42 enum ID { |
| 43 // The main thread in the browser. |
| 44 UI, |
| 45 |
| 46 // This is the thread that interacts with the database. |
| 47 DB, |
| 48 |
| 49 // This is the "main" thread for WebKit within the browser process when |
| 50 // NOT in --single-process mode. |
| 51 WEBKIT, |
| 52 |
| 53 // This is the thread that interacts with the file system. |
| 54 FILE, |
| 55 |
| 56 // Used to launch and terminate processes. |
| 57 PROCESS_LAUNCHER, |
| 58 |
| 59 // This is the thread to handle slow HTTP cache operations. |
| 60 CACHE, |
| 61 |
| 62 // This is the thread that processes IPC and network messages. |
| 63 IO, |
| 64 |
| 65 #if defined(USE_X11) |
| 66 // This thread has a second connection to the X server and is used to |
| 67 // process UI requests when routing the request to the UI thread would risk |
| 68 // deadlock. |
| 69 BACKGROUND_X11, |
| 70 #endif |
| 71 |
| 72 // This identifier does not represent a thread. Instead it counts the |
| 73 // number of well-known threads. Insert new well-known threads before this |
| 74 // identifier. |
| 75 ID_COUNT |
| 76 }; |
| 77 |
| 78 // Construct a ChromeThread with the supplied identifier. It is an error |
| 79 // to construct a ChromeThread that already exists. |
| 80 explicit ChromeThread(ID identifier); |
| 81 |
| 82 // Special constructor for the main (UI) thread and unittests. We use a dummy |
| 83 // thread here since the main thread already exists. |
| 84 ChromeThread(ID identifier, MessageLoop* message_loop); |
| 85 |
| 86 virtual ~ChromeThread(); |
| 87 |
| 88 // These are the same methods in message_loop.h, but are guaranteed to either |
| 89 // get posted to the MessageLoop if it's still alive, or be deleted otherwise. |
| 90 // They return true iff the thread existed and the task was posted. Note that |
| 91 // even if the task is posted, there's no guarantee that it will run, since |
| 92 // the target thread may already have a Quit message in its queue. |
| 93 static bool PostTask(ID identifier, |
| 94 const tracked_objects::Location& from_here, |
| 95 Task* task); |
| 96 static bool PostDelayedTask(ID identifier, |
| 97 const tracked_objects::Location& from_here, |
| 98 Task* task, |
| 99 int64 delay_ms); |
| 100 static bool PostNonNestableTask(ID identifier, |
| 101 const tracked_objects::Location& from_here, |
| 102 Task* task); |
| 103 static bool PostNonNestableDelayedTask( |
| 104 ID identifier, |
| 105 const tracked_objects::Location& from_here, |
| 106 Task* task, |
| 107 int64 delay_ms); |
| 108 |
| 109 template <class T> |
| 110 static bool DeleteSoon(ID identifier, |
| 111 const tracked_objects::Location& from_here, |
| 112 T* object) { |
| 113 return PostNonNestableTask( |
| 114 identifier, from_here, new DeleteTask<T>(object)); |
| 115 } |
| 116 |
| 117 template <class T> |
| 118 static bool ReleaseSoon(ID identifier, |
| 119 const tracked_objects::Location& from_here, |
| 120 T* object) { |
| 121 return PostNonNestableTask( |
| 122 identifier, from_here, new ReleaseTask<T>(object)); |
| 123 } |
| 124 |
| 125 // Callable on any thread. Returns whether the given ID corresponds to a well |
| 126 // known thread. |
| 127 static bool IsWellKnownThread(ID identifier); |
| 128 |
| 129 // Callable on any thread. Returns whether you're currently on a particular |
| 130 // thread. |
| 131 static bool CurrentlyOn(ID identifier); |
| 132 |
| 133 // Callable on any thread. Returns whether the threads message loop is valid. |
| 134 // If this returns false it means the thread is in the process of shutting |
| 135 // down. |
| 136 static bool IsMessageLoopValid(ID identifier); |
| 137 |
| 138 // If the current message loop is one of the known threads, returns true and |
| 139 // sets identifier to its ID. Otherwise returns false. |
| 140 static bool GetCurrentThreadIdentifier(ID* identifier); |
| 141 |
| 142 // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime |
| 143 // of the thread. |
| 144 static scoped_refptr<base::MessageLoopProxy> GetMessageLoopProxyForThread( |
| 145 ID identifier); |
| 146 |
| 147 // Use these templates in conjuction with RefCountedThreadSafe when you want |
| 148 // to ensure that an object is deleted on a specific thread. This is needed |
| 149 // when an object can hop between threads (i.e. IO -> FILE -> IO), and thread |
| 150 // switching delays can mean that the final IO tasks executes before the FILE |
| 151 // task's stack unwinds. This would lead to the object destructing on the |
| 152 // FILE thread, which often is not what you want (i.e. to unregister from |
| 153 // NotificationService, to notify other objects on the creating thread etc). |
| 154 template<ID thread> |
| 155 struct DeleteOnThread { |
| 156 template<typename T> |
| 157 static void Destruct(T* x) { |
| 158 if (CurrentlyOn(thread)) { |
| 159 delete x; |
| 160 } else { |
| 161 DeleteSoon(thread, FROM_HERE, x); |
| 162 } |
| 163 } |
| 164 }; |
| 165 |
| 166 // Sample usage: |
| 167 // class Foo |
| 168 // : public base::RefCountedThreadSafe< |
| 169 // Foo, ChromeThread::DeleteOnIOThread> { |
| 170 // |
| 171 // ... |
| 172 // private: |
| 173 // friend struct ChromeThread::DeleteOnThread<ChromeThread::IO>; |
| 174 // friend class DeleteTask<Foo>; |
| 175 // |
| 176 // ~Foo(); |
| 177 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; |
| 178 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; |
| 179 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; |
| 180 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; |
| 181 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { }; |
| 182 |
| 183 private: |
| 184 // Common initialization code for the constructors. |
| 185 void Initialize(); |
| 186 |
| 187 static bool PostTaskHelper( |
| 188 ID identifier, |
| 189 const tracked_objects::Location& from_here, |
| 190 Task* task, |
| 191 int64 delay_ms, |
| 192 bool nestable); |
| 193 |
| 194 // The identifier of this thread. Only one thread can exist with a given |
| 195 // identifier at a given time. |
| 196 ID identifier_; |
| 197 |
| 198 // This lock protects |chrome_threads_|. Do not read or modify that array |
| 199 // without holding this lock. Do not block while holding this lock. |
| 200 static Lock lock_; |
| 201 |
| 202 // An array of the ChromeThread objects. This array is protected by |lock_|. |
| 203 // The threads are not owned by this array. Typically, the threads are owned |
| 204 // on the UI thread by the g_browser_process object. ChromeThreads remove |
| 205 // themselves from this array upon destruction. |
| 206 static ChromeThread* chrome_threads_[ID_COUNT]; |
| 207 }; |
| 208 |
| 209 typedef ChromeThread BrowserThread; |
| 210 |
| 211 #endif // CHROME_BROWSER_CHROME_THREAD_H_ |
| OLD | NEW |