OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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/task.h" | 9 #include "base/task.h" |
10 #include "base/thread.h" | 10 #include "base/thread.h" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 // When running under unit-tests, this will return true if you're on the | 118 // When running under unit-tests, this will return true if you're on the |
119 // main thread and the thread ID you pass in isn't running. This is | 119 // main thread and the thread ID you pass in isn't running. This is |
120 // normally the correct behavior because you want to ignore these asserts | 120 // normally the correct behavior because you want to ignore these asserts |
121 // unless you've specifically spun up the threads, but be mindful of it. | 121 // unless you've specifically spun up the threads, but be mindful of it. |
122 static bool CurrentlyOn(ID identifier); | 122 static bool CurrentlyOn(ID identifier); |
123 | 123 |
124 // If the current message loop is one of the known threads, returns true and | 124 // If the current message loop is one of the known threads, returns true and |
125 // sets identifier to its ID. Otherwise returns false. | 125 // sets identifier to its ID. Otherwise returns false. |
126 static bool GetCurrentThreadIdentifier(ID* identifier); | 126 static bool GetCurrentThreadIdentifier(ID* identifier); |
127 | 127 |
| 128 // Use these templates in conjuction with RefCountedThreadSafe when you want |
| 129 // to ensure that an object is deleted on a specific thread. This is needed |
| 130 // when an object can hop between threads (i.e. IO -> FILE -> IO), and thread |
| 131 // switching delays can mean that the final IO tasks executes before the FILE |
| 132 // task's stack unwinds. This would lead to the object destructing on the |
| 133 // FILE thread, which often is not what you want (i.e. to unregister from |
| 134 // NotificationService, to notify other objects on the creating thread etc). |
| 135 template<ID thread> |
| 136 struct DeleteOnThread { |
| 137 template<typename T> |
| 138 static void Destruct(T* x) { |
| 139 if (CurrentlyOn(thread)) { |
| 140 delete x; |
| 141 } else { |
| 142 DeleteSoon(thread, FROM_HERE, x); |
| 143 } |
| 144 } |
| 145 }; |
| 146 |
| 147 // Sample usage: |
| 148 // class Foo |
| 149 // : public base::RefCountedThreadSafe< |
| 150 // Foo, ChromeThread::DeleteOnIOThread> { |
| 151 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; |
| 152 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; |
| 153 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; |
| 154 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; |
| 155 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { }; |
| 156 |
128 private: | 157 private: |
129 // Common initialization code for the constructors. | 158 // Common initialization code for the constructors. |
130 void Initialize(); | 159 void Initialize(); |
131 | 160 |
132 static bool PostTaskHelper( | 161 static bool PostTaskHelper( |
133 ID identifier, | 162 ID identifier, |
134 const tracked_objects::Location& from_here, | 163 const tracked_objects::Location& from_here, |
135 Task* task, | 164 Task* task, |
136 int64 delay_ms, | 165 int64 delay_ms, |
137 bool nestable); | 166 bool nestable); |
138 | 167 |
139 // The identifier of this thread. Only one thread can exist with a given | 168 // The identifier of this thread. Only one thread can exist with a given |
140 // identifier at a given time. | 169 // identifier at a given time. |
141 ID identifier_; | 170 ID identifier_; |
142 | 171 |
143 // This lock protects |chrome_threads_|. Do not read or modify that array | 172 // This lock protects |chrome_threads_|. Do not read or modify that array |
144 // without holding this lock. Do not block while holding this lock. | 173 // without holding this lock. Do not block while holding this lock. |
145 static Lock lock_; | 174 static Lock lock_; |
146 | 175 |
147 // An array of the ChromeThread objects. This array is protected by |lock_|. | 176 // An array of the ChromeThread objects. This array is protected by |lock_|. |
148 // The threads are not owned by this array. Typically, the threads are owned | 177 // The threads are not owned by this array. Typically, the threads are owned |
149 // on the UI thread by the g_browser_process object. ChromeThreads remove | 178 // on the UI thread by the g_browser_process object. ChromeThreads remove |
150 // themselves from this array upon destruction. | 179 // themselves from this array upon destruction. |
151 static ChromeThread* chrome_threads_[ID_COUNT]; | 180 static ChromeThread* chrome_threads_[ID_COUNT]; |
152 }; | 181 }; |
153 | 182 |
154 #endif // #ifndef CHROME_BROWSER_CHROME_THREAD_H_ | 183 #endif // #ifndef CHROME_BROWSER_CHROME_THREAD_H_ |
OLD | NEW |