Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_THREAD_H_ |
michael@0 | 6 | #define BASE_THREAD_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include <stdint.h> |
michael@0 | 9 | #include <string> |
michael@0 | 10 | |
michael@0 | 11 | #include "base/message_loop.h" |
michael@0 | 12 | #include "base/platform_thread.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace base { |
michael@0 | 15 | |
michael@0 | 16 | // A simple thread abstraction that establishes a MessageLoop on a new thread. |
michael@0 | 17 | // The consumer uses the MessageLoop of the thread to cause code to execute on |
michael@0 | 18 | // the thread. When this object is destroyed the thread is terminated. All |
michael@0 | 19 | // pending tasks queued on the thread's message loop will run to completion |
michael@0 | 20 | // before the thread is terminated. |
michael@0 | 21 | class Thread : PlatformThread::Delegate { |
michael@0 | 22 | public: |
michael@0 | 23 | struct Options { |
michael@0 | 24 | // Specifies the type of message loop that will be allocated on the thread. |
michael@0 | 25 | MessageLoop::Type message_loop_type; |
michael@0 | 26 | |
michael@0 | 27 | // Specifies the maximum stack size that the thread is allowed to use. |
michael@0 | 28 | // This does not necessarily correspond to the thread's initial stack size. |
michael@0 | 29 | // A value of 0 indicates that the default maximum should be used. |
michael@0 | 30 | size_t stack_size; |
michael@0 | 31 | |
michael@0 | 32 | // Specifies the transient and permanent hang timeouts for background hang |
michael@0 | 33 | // monitoring. A value of 0 indicates there is no timeout. |
michael@0 | 34 | uint32_t transient_hang_timeout; |
michael@0 | 35 | uint32_t permanent_hang_timeout; |
michael@0 | 36 | |
michael@0 | 37 | Options() |
michael@0 | 38 | : message_loop_type(MessageLoop::TYPE_DEFAULT) |
michael@0 | 39 | , stack_size(0) |
michael@0 | 40 | , transient_hang_timeout(0) |
michael@0 | 41 | , permanent_hang_timeout(0) {} |
michael@0 | 42 | Options(MessageLoop::Type type, size_t size) |
michael@0 | 43 | : message_loop_type(type) |
michael@0 | 44 | , stack_size(size) |
michael@0 | 45 | , transient_hang_timeout(0) |
michael@0 | 46 | , permanent_hang_timeout(0) {} |
michael@0 | 47 | }; |
michael@0 | 48 | |
michael@0 | 49 | // Constructor. |
michael@0 | 50 | // name is a display string to identify the thread. |
michael@0 | 51 | explicit Thread(const char *name); |
michael@0 | 52 | |
michael@0 | 53 | // Destroys the thread, stopping it if necessary. |
michael@0 | 54 | // |
michael@0 | 55 | // NOTE: If you are subclassing from Thread, and you wish for your CleanUp |
michael@0 | 56 | // method to be called, then you need to call Stop() from your destructor. |
michael@0 | 57 | // |
michael@0 | 58 | virtual ~Thread(); |
michael@0 | 59 | |
michael@0 | 60 | // Starts the thread. Returns true if the thread was successfully started; |
michael@0 | 61 | // otherwise, returns false. Upon successful return, the message_loop() |
michael@0 | 62 | // getter will return non-null. |
michael@0 | 63 | // |
michael@0 | 64 | // Note: This function can't be called on Windows with the loader lock held; |
michael@0 | 65 | // i.e. during a DllMain, global object construction or destruction, atexit() |
michael@0 | 66 | // callback. |
michael@0 | 67 | bool Start(); |
michael@0 | 68 | |
michael@0 | 69 | // Starts the thread. Behaves exactly like Start in addition to allow to |
michael@0 | 70 | // override the default options. |
michael@0 | 71 | // |
michael@0 | 72 | // Note: This function can't be called on Windows with the loader lock held; |
michael@0 | 73 | // i.e. during a DllMain, global object construction or destruction, atexit() |
michael@0 | 74 | // callback. |
michael@0 | 75 | bool StartWithOptions(const Options& options); |
michael@0 | 76 | |
michael@0 | 77 | // Signals the thread to exit and returns once the thread has exited. After |
michael@0 | 78 | // this method returns, the Thread object is completely reset and may be used |
michael@0 | 79 | // as if it were newly constructed (i.e., Start may be called again). |
michael@0 | 80 | // |
michael@0 | 81 | // Stop may be called multiple times and is simply ignored if the thread is |
michael@0 | 82 | // already stopped. |
michael@0 | 83 | // |
michael@0 | 84 | // NOTE: This method is optional. It is not strictly necessary to call this |
michael@0 | 85 | // method as the Thread's destructor will take care of stopping the thread if |
michael@0 | 86 | // necessary. |
michael@0 | 87 | // |
michael@0 | 88 | void Stop(); |
michael@0 | 89 | |
michael@0 | 90 | // Signals the thread to exit in the near future. |
michael@0 | 91 | // |
michael@0 | 92 | // WARNING: This function is not meant to be commonly used. Use at your own |
michael@0 | 93 | // risk. Calling this function will cause message_loop() to become invalid in |
michael@0 | 94 | // the near future. This function was created to workaround a specific |
michael@0 | 95 | // deadlock on Windows with printer worker thread. In any other case, Stop() |
michael@0 | 96 | // should be used. |
michael@0 | 97 | // |
michael@0 | 98 | // StopSoon should not be called multiple times as it is risky to do so. It |
michael@0 | 99 | // could cause a timing issue in message_loop() access. Call Stop() to reset |
michael@0 | 100 | // the thread object once it is known that the thread has quit. |
michael@0 | 101 | void StopSoon(); |
michael@0 | 102 | |
michael@0 | 103 | // Returns the message loop for this thread. Use the MessageLoop's |
michael@0 | 104 | // PostTask methods to execute code on the thread. This only returns |
michael@0 | 105 | // non-null after a successful call to Start. After Stop has been called, |
michael@0 | 106 | // this will return NULL. |
michael@0 | 107 | // |
michael@0 | 108 | // NOTE: You must not call this MessageLoop's Quit method directly. Use |
michael@0 | 109 | // the Thread's Stop method instead. |
michael@0 | 110 | // |
michael@0 | 111 | MessageLoop* message_loop() const { return message_loop_; } |
michael@0 | 112 | |
michael@0 | 113 | // Set the name of this thread (for display in debugger too). |
michael@0 | 114 | const std::string &thread_name() { return name_; } |
michael@0 | 115 | |
michael@0 | 116 | // The native thread handle. |
michael@0 | 117 | PlatformThreadHandle thread_handle() { return thread_; } |
michael@0 | 118 | |
michael@0 | 119 | // The thread ID. |
michael@0 | 120 | PlatformThreadId thread_id() const { return thread_id_; } |
michael@0 | 121 | |
michael@0 | 122 | // Reset thread ID as current thread. |
michael@0 | 123 | PlatformThreadId reset_thread_id() { |
michael@0 | 124 | thread_id_ = PlatformThread::CurrentId(); |
michael@0 | 125 | return thread_id_; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | // Returns true if the thread has been started, and not yet stopped. |
michael@0 | 129 | // When a thread is running, the thread_id_ is non-zero. |
michael@0 | 130 | bool IsRunning() const { return thread_id_ != 0; } |
michael@0 | 131 | |
michael@0 | 132 | protected: |
michael@0 | 133 | // Called just prior to starting the message loop |
michael@0 | 134 | virtual void Init() {} |
michael@0 | 135 | |
michael@0 | 136 | // Called just after the message loop ends |
michael@0 | 137 | virtual void CleanUp() {} |
michael@0 | 138 | |
michael@0 | 139 | static void SetThreadWasQuitProperly(bool flag); |
michael@0 | 140 | static bool GetThreadWasQuitProperly(); |
michael@0 | 141 | |
michael@0 | 142 | private: |
michael@0 | 143 | // PlatformThread::Delegate methods: |
michael@0 | 144 | virtual void ThreadMain(); |
michael@0 | 145 | |
michael@0 | 146 | // We piggy-back on the startup_data_ member to know if we successfully |
michael@0 | 147 | // started the thread. This way we know that we need to call Join. |
michael@0 | 148 | bool thread_was_started() const { return startup_data_ != NULL; } |
michael@0 | 149 | |
michael@0 | 150 | // Used to pass data to ThreadMain. |
michael@0 | 151 | struct StartupData; |
michael@0 | 152 | StartupData* startup_data_; |
michael@0 | 153 | |
michael@0 | 154 | // The thread's handle. |
michael@0 | 155 | PlatformThreadHandle thread_; |
michael@0 | 156 | |
michael@0 | 157 | // The thread's message loop. Valid only while the thread is alive. Set |
michael@0 | 158 | // by the created thread. |
michael@0 | 159 | MessageLoop* message_loop_; |
michael@0 | 160 | |
michael@0 | 161 | // Our thread's ID. |
michael@0 | 162 | PlatformThreadId thread_id_; |
michael@0 | 163 | |
michael@0 | 164 | // The name of the thread. Used for debugging purposes. |
michael@0 | 165 | std::string name_; |
michael@0 | 166 | |
michael@0 | 167 | friend class ThreadQuitTask; |
michael@0 | 168 | |
michael@0 | 169 | DISALLOW_COPY_AND_ASSIGN(Thread); |
michael@0 | 170 | }; |
michael@0 | 171 | |
michael@0 | 172 | } // namespace base |
michael@0 | 173 | |
michael@0 | 174 | #endif // BASE_THREAD_H_ |