license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 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 |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 5 | #ifndef BASE_THREAD_H_ |
| 6 | #define BASE_THREAD_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 8 | #include <string> |
| 9 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 10 | #include "base/platform_thread.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 11 | #include "base/thread_local_storage.h" |
| 12 | |
| 13 | class MessageLoop; |
| 14 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 15 | namespace base { |
| 16 | class WaitableEvent; |
| 17 | } |
avi@google.com | 4be2f9b | 2008-08-08 05:12:28 +0900 | [diff] [blame] | 18 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 19 | // A simple thread abstraction that establishes a MessageLoop on a new thread. |
| 20 | // The consumer uses the MessageLoop of the thread to cause code to execute on |
| 21 | // the thread. When this object is destroyed the thread is terminated. All |
| 22 | // pending tasks queued on the thread's message loop will run to completion |
| 23 | // before the thread is terminated. |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 24 | class Thread : PlatformThread::Delegate { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 25 | public: |
| 26 | // Constructor. |
| 27 | // name is a display string to identify the thread. |
| 28 | explicit Thread(const char *name); |
| 29 | |
| 30 | // Destroys the thread, stopping it if necessary. |
| 31 | // |
| 32 | virtual ~Thread(); |
| 33 | |
| 34 | // Starts the thread. Returns true if the thread was successfully started; |
| 35 | // otherwise, returns false. Upon successful return, the message_loop() |
| 36 | // getter will return non-null. |
| 37 | // |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 38 | // Note: This function can't be called on Windows with the loader lock held; |
| 39 | // i.e. during a DllMain, global object construction or destruction, atexit() |
| 40 | // callback. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 41 | bool Start(); |
| 42 | |
| 43 | // Starts the thread. Behaves exactly like Start in addition to allow to |
| 44 | // override the default process stack size. This is not the initial stack size |
| 45 | // but the maximum stack size that thread is allowed to use. |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 46 | // |
| 47 | // Note: This function can't be called on Windows with the loader lock held; |
| 48 | // i.e. during a DllMain, global object construction or destruction, atexit() |
| 49 | // callback. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 50 | bool StartWithStackSize(size_t stack_size); |
| 51 | |
| 52 | // Signals the thread to exit and returns once the thread has exited. After |
| 53 | // this method returns, the Thread object is completely reset and may be used |
| 54 | // as if it were newly constructed (i.e., Start may be called again). |
| 55 | // |
| 56 | // Stop may be called multiple times and is simply ignored if the thread is |
| 57 | // already stopped. |
| 58 | // |
| 59 | // NOTE: This method is optional. It is not strictly necessary to call this |
| 60 | // method as the Thread's destructor will take care of stopping the thread if |
| 61 | // necessary. |
| 62 | // |
| 63 | void Stop(); |
| 64 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 65 | // Signals the thread to exit in the near future. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 66 | // |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 67 | // WARNING: This function is not meant to be commonly used. Use at your own |
maruel@google.com | 3737729 | 2008-08-14 00:40:45 +0900 | [diff] [blame] | 68 | // risk. Calling this function will cause message_loop() to become invalid in |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 69 | // the near future. This function was created to workaround a specific |
| 70 | // deadlock on Windows with printer worker thread. In any other case, Stop() |
| 71 | // should be used. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 72 | // |
maruel@google.com | 3737729 | 2008-08-14 00:40:45 +0900 | [diff] [blame] | 73 | // StopSoon should not be called multiple times as it is risky to do so. It |
| 74 | // could cause a timing issue in message_loop() access. Call Stop() to reset |
| 75 | // the thread object once it is known that the thread has quit. |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 76 | void StopSoon(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 77 | |
| 78 | // Returns the message loop for this thread. Use the MessageLoop's |
avi@google.com | 4be2f9b | 2008-08-08 05:12:28 +0900 | [diff] [blame] | 79 | // PostTask methods to execute code on the thread. This only returns |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 80 | // non-null after a successful call to Start. After Stop has been called, |
| 81 | // this will return NULL. |
| 82 | // |
| 83 | // NOTE: You must not call this MessageLoop's Quit method directly. Use |
| 84 | // the Thread's Stop method instead. |
| 85 | // |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 86 | MessageLoop* message_loop() const { return message_loop_; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 87 | |
| 88 | // Set the name of this thread (for display in debugger too). |
| 89 | const std::string &thread_name() { return name_; } |
| 90 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 91 | // The native thread handle. |
| 92 | PlatformThreadHandle thread_handle() { return thread_; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 93 | |
| 94 | protected: |
| 95 | // Called just prior to starting the message loop |
| 96 | virtual void Init() { } |
| 97 | |
| 98 | // Called just after the message loop ends |
| 99 | virtual void CleanUp() { } |
| 100 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 101 | static void SetThreadWasQuitProperly(bool flag); |
| 102 | static bool GetThreadWasQuitProperly(); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 103 | |
| 104 | private: |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 105 | // PlatformThread::Delegate methods: |
| 106 | virtual void ThreadMain(); |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 107 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 108 | // The thread's handle. |
| 109 | PlatformThreadHandle thread_; |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 110 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 111 | // The thread's ID. Used for debugging purposes. |
| 112 | int thread_id_; |
| 113 | |
| 114 | // The thread's message loop. Valid only while the thread is alive. Set |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 115 | // by the created thread. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 116 | MessageLoop* message_loop_; |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 117 | |
darin@google.com | c18d7ae | 2008-08-21 18:46:32 +0900 | [diff] [blame] | 118 | // Used to synchronize thread startup. |
| 119 | base::WaitableEvent* startup_event_; |
| 120 | |
| 121 | // The name of the thread. Used for debugging purposes. |
| 122 | std::string name_; |
| 123 | |
| 124 | // This flag indicates if we created a thread that needs to be joined. |
| 125 | bool thread_created_; |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 126 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 127 | static TLSSlot tls_index_; |
| 128 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 129 | friend class ThreadQuitTask; |
| 130 | |
| 131 | DISALLOW_COPY_AND_ASSIGN(Thread); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 132 | }; |
| 133 | |
maruel@google.com | f6e3db8 | 2008-08-13 03:37:35 +0900 | [diff] [blame] | 134 | #endif // BASE_THREAD_H_ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 135 | |