ipc/chromium/src/base/thread.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial