1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/thread.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,174 @@ 1.4 +// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef BASE_THREAD_H_ 1.9 +#define BASE_THREAD_H_ 1.10 + 1.11 +#include <stdint.h> 1.12 +#include <string> 1.13 + 1.14 +#include "base/message_loop.h" 1.15 +#include "base/platform_thread.h" 1.16 + 1.17 +namespace base { 1.18 + 1.19 +// A simple thread abstraction that establishes a MessageLoop on a new thread. 1.20 +// The consumer uses the MessageLoop of the thread to cause code to execute on 1.21 +// the thread. When this object is destroyed the thread is terminated. All 1.22 +// pending tasks queued on the thread's message loop will run to completion 1.23 +// before the thread is terminated. 1.24 +class Thread : PlatformThread::Delegate { 1.25 + public: 1.26 + struct Options { 1.27 + // Specifies the type of message loop that will be allocated on the thread. 1.28 + MessageLoop::Type message_loop_type; 1.29 + 1.30 + // Specifies the maximum stack size that the thread is allowed to use. 1.31 + // This does not necessarily correspond to the thread's initial stack size. 1.32 + // A value of 0 indicates that the default maximum should be used. 1.33 + size_t stack_size; 1.34 + 1.35 + // Specifies the transient and permanent hang timeouts for background hang 1.36 + // monitoring. A value of 0 indicates there is no timeout. 1.37 + uint32_t transient_hang_timeout; 1.38 + uint32_t permanent_hang_timeout; 1.39 + 1.40 + Options() 1.41 + : message_loop_type(MessageLoop::TYPE_DEFAULT) 1.42 + , stack_size(0) 1.43 + , transient_hang_timeout(0) 1.44 + , permanent_hang_timeout(0) {} 1.45 + Options(MessageLoop::Type type, size_t size) 1.46 + : message_loop_type(type) 1.47 + , stack_size(size) 1.48 + , transient_hang_timeout(0) 1.49 + , permanent_hang_timeout(0) {} 1.50 + }; 1.51 + 1.52 + // Constructor. 1.53 + // name is a display string to identify the thread. 1.54 + explicit Thread(const char *name); 1.55 + 1.56 + // Destroys the thread, stopping it if necessary. 1.57 + // 1.58 + // NOTE: If you are subclassing from Thread, and you wish for your CleanUp 1.59 + // method to be called, then you need to call Stop() from your destructor. 1.60 + // 1.61 + virtual ~Thread(); 1.62 + 1.63 + // Starts the thread. Returns true if the thread was successfully started; 1.64 + // otherwise, returns false. Upon successful return, the message_loop() 1.65 + // getter will return non-null. 1.66 + // 1.67 + // Note: This function can't be called on Windows with the loader lock held; 1.68 + // i.e. during a DllMain, global object construction or destruction, atexit() 1.69 + // callback. 1.70 + bool Start(); 1.71 + 1.72 + // Starts the thread. Behaves exactly like Start in addition to allow to 1.73 + // override the default options. 1.74 + // 1.75 + // Note: This function can't be called on Windows with the loader lock held; 1.76 + // i.e. during a DllMain, global object construction or destruction, atexit() 1.77 + // callback. 1.78 + bool StartWithOptions(const Options& options); 1.79 + 1.80 + // Signals the thread to exit and returns once the thread has exited. After 1.81 + // this method returns, the Thread object is completely reset and may be used 1.82 + // as if it were newly constructed (i.e., Start may be called again). 1.83 + // 1.84 + // Stop may be called multiple times and is simply ignored if the thread is 1.85 + // already stopped. 1.86 + // 1.87 + // NOTE: This method is optional. It is not strictly necessary to call this 1.88 + // method as the Thread's destructor will take care of stopping the thread if 1.89 + // necessary. 1.90 + // 1.91 + void Stop(); 1.92 + 1.93 + // Signals the thread to exit in the near future. 1.94 + // 1.95 + // WARNING: This function is not meant to be commonly used. Use at your own 1.96 + // risk. Calling this function will cause message_loop() to become invalid in 1.97 + // the near future. This function was created to workaround a specific 1.98 + // deadlock on Windows with printer worker thread. In any other case, Stop() 1.99 + // should be used. 1.100 + // 1.101 + // StopSoon should not be called multiple times as it is risky to do so. It 1.102 + // could cause a timing issue in message_loop() access. Call Stop() to reset 1.103 + // the thread object once it is known that the thread has quit. 1.104 + void StopSoon(); 1.105 + 1.106 + // Returns the message loop for this thread. Use the MessageLoop's 1.107 + // PostTask methods to execute code on the thread. This only returns 1.108 + // non-null after a successful call to Start. After Stop has been called, 1.109 + // this will return NULL. 1.110 + // 1.111 + // NOTE: You must not call this MessageLoop's Quit method directly. Use 1.112 + // the Thread's Stop method instead. 1.113 + // 1.114 + MessageLoop* message_loop() const { return message_loop_; } 1.115 + 1.116 + // Set the name of this thread (for display in debugger too). 1.117 + const std::string &thread_name() { return name_; } 1.118 + 1.119 + // The native thread handle. 1.120 + PlatformThreadHandle thread_handle() { return thread_; } 1.121 + 1.122 + // The thread ID. 1.123 + PlatformThreadId thread_id() const { return thread_id_; } 1.124 + 1.125 + // Reset thread ID as current thread. 1.126 + PlatformThreadId reset_thread_id() { 1.127 + thread_id_ = PlatformThread::CurrentId(); 1.128 + return thread_id_; 1.129 + } 1.130 + 1.131 + // Returns true if the thread has been started, and not yet stopped. 1.132 + // When a thread is running, the thread_id_ is non-zero. 1.133 + bool IsRunning() const { return thread_id_ != 0; } 1.134 + 1.135 + protected: 1.136 + // Called just prior to starting the message loop 1.137 + virtual void Init() {} 1.138 + 1.139 + // Called just after the message loop ends 1.140 + virtual void CleanUp() {} 1.141 + 1.142 + static void SetThreadWasQuitProperly(bool flag); 1.143 + static bool GetThreadWasQuitProperly(); 1.144 + 1.145 + private: 1.146 + // PlatformThread::Delegate methods: 1.147 + virtual void ThreadMain(); 1.148 + 1.149 + // We piggy-back on the startup_data_ member to know if we successfully 1.150 + // started the thread. This way we know that we need to call Join. 1.151 + bool thread_was_started() const { return startup_data_ != NULL; } 1.152 + 1.153 + // Used to pass data to ThreadMain. 1.154 + struct StartupData; 1.155 + StartupData* startup_data_; 1.156 + 1.157 + // The thread's handle. 1.158 + PlatformThreadHandle thread_; 1.159 + 1.160 + // The thread's message loop. Valid only while the thread is alive. Set 1.161 + // by the created thread. 1.162 + MessageLoop* message_loop_; 1.163 + 1.164 + // Our thread's ID. 1.165 + PlatformThreadId thread_id_; 1.166 + 1.167 + // The name of the thread. Used for debugging purposes. 1.168 + std::string name_; 1.169 + 1.170 + friend class ThreadQuitTask; 1.171 + 1.172 + DISALLOW_COPY_AND_ASSIGN(Thread); 1.173 +}; 1.174 + 1.175 +} // namespace base 1.176 + 1.177 +#endif // BASE_THREAD_H_