diff options
author | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-26 05:53:57 +0000 |
---|---|---|
committer | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-26 05:53:57 +0000 |
commit | 4d9bdfafcd1393385860bc9fe947e0c07719c0f4 (patch) | |
tree | 67c91c3fa4c658352995e22b894535e60b04f282 /base/thread.h | |
parent | 3c17b9c00d5fc28f5523ab60b0df502dd7c2a596 (diff) | |
download | chromium_src-4d9bdfafcd1393385860bc9fe947e0c07719c0f4.zip chromium_src-4d9bdfafcd1393385860bc9fe947e0c07719c0f4.tar.gz chromium_src-4d9bdfafcd1393385860bc9fe947e0c07719c0f4.tar.bz2 |
Allow consumers of MessageLoop to specify the type of MessageLoop they want.
This CL introduces a Type enum to MessageLoop, and I also created subclasses of MessageLoop corresponding to the non-default types: MessageLoopForIO and MessageLoopForUI.
I moved all of the platform-specific MessageLoop APIs onto either MessageLoopForIO or MessageLoopForUI. MessageLoopForIO gets the Watcher API, and MessageLoopForUI gets the Observer and Dispatcher APIs. Under the hood, both are implemented in terms of MessagePumpWin, but that will change in a future CL.
The Thread class is changed to allow the consumer to specify the Type of MessageLoop they want to have setup on the created thread.
I re-organized message_loop_unittest.cc and timer_unittest.cc so that I could exercise all (or most) of the tests against each type of MessageLoop.
Note: I know that "explicit MessageLoop(Type type = TYPE_DEFAULT);" is in violation to the style-guide's restriction against default arguments. I'm working on finding a decent solution to that problem. Please ignore this issue for now.
The corresponding chrome/ changes are coming in a separate CL due to Reitveld data size limitations.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1362 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/thread.h')
-rw-r--r-- | base/thread.h | 49 |
1 files changed, 32 insertions, 17 deletions
diff --git a/base/thread.h b/base/thread.h index 1b3ba14..619d2b7 100644 --- a/base/thread.h +++ b/base/thread.h @@ -7,14 +7,11 @@ #include <string> +#include "base/message_loop.h" #include "base/platform_thread.h" #include "base/thread_local_storage.h" -class MessageLoop; - namespace base { -class WaitableEvent; -} // A simple thread abstraction that establishes a MessageLoop on a new thread. // The consumer uses the MessageLoop of the thread to cause code to execute on @@ -23,12 +20,27 @@ class WaitableEvent; // before the thread is terminated. class Thread : PlatformThread::Delegate { public: + struct Options { + // Specifies the type of message loop that will be allocated on the thread. + MessageLoop::Type message_loop_type; + + // Specifies the maximum stack size that the thread is allowed to use. + // This does not necessarily correspond to the thread's initial stack size. + // A value of 0 indicates that the default maximum should be used. + size_t stack_size; + + Options() : message_loop_type(MessageLoop::TYPE_DEFAULT), stack_size(0) {} + }; + // Constructor. // name is a display string to identify the thread. explicit Thread(const char *name); // Destroys the thread, stopping it if necessary. // + // NOTE: If you are subclassing from Thread, and you wish for your CleanUp + // method to be called, then you need to call Stop() from your destructor. + // virtual ~Thread(); // Starts the thread. Returns true if the thread was successfully started; @@ -41,13 +53,12 @@ class Thread : PlatformThread::Delegate { bool Start(); // Starts the thread. Behaves exactly like Start in addition to allow to - // override the default process stack size. This is not the initial stack size - // but the maximum stack size that thread is allowed to use. + // override the default options. // // Note: This function can't be called on Windows with the loader lock held; // i.e. during a DllMain, global object construction or destruction, atexit() // callback. - bool StartWithStackSize(size_t stack_size); + bool StartWithOptions(const Options& options); // Signals the thread to exit and returns once the thread has exited. After // this method returns, the Thread object is completely reset and may be used @@ -93,10 +104,10 @@ class Thread : PlatformThread::Delegate { protected: // Called just prior to starting the message loop - virtual void Init() { } + virtual void Init() {} // Called just after the message loop ends - virtual void CleanUp() { } + virtual void CleanUp() {} static void SetThreadWasQuitProperly(bool flag); static bool GetThreadWasQuitProperly(); @@ -105,25 +116,27 @@ class Thread : PlatformThread::Delegate { // PlatformThread::Delegate methods: virtual void ThreadMain(); + // We piggy-back on the startup_data_ member to know if we successfully + // started the thread. This way we know that we need to call Join. + bool thread_was_started() const { return startup_data_ != NULL; } + + // Used to pass data to ThreadMain. + struct StartupData; + StartupData* startup_data_; + // The thread's handle. PlatformThreadHandle thread_; - // The thread's ID. Used for debugging purposes. - int thread_id_; - // The thread's message loop. Valid only while the thread is alive. Set // by the created thread. MessageLoop* message_loop_; - // Used to synchronize thread startup. - base::WaitableEvent* startup_event_; + // Our thread's ID. Used for debugging purposes. + int thread_id_; // The name of the thread. Used for debugging purposes. std::string name_; - // This flag indicates if we created a thread that needs to be joined. - bool thread_created_; - static TLSSlot tls_index_; friend class ThreadQuitTask; @@ -131,5 +144,7 @@ class Thread : PlatformThread::Delegate { DISALLOW_COPY_AND_ASSIGN(Thread); }; +} // namespace base + #endif // BASE_THREAD_H_ |