This source file includes following definitions.
- ThreadQuitHelper
 
- stack_size
 
- stack_size
 
- name_
 
- Start
 
- StartWithOptions
 
- Stop
 
- StopSoon
 
- IsRunning
 
- SetPriority
 
- Run
 
- SetThreadWasQuitProperly
 
- GetThreadWasQuitProperly
 
- ThreadMain
 
#include "base/threading/thread.h"
#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/thread_id_name_manager.h"
#include "base/threading/thread_local.h"
#include "base/threading/thread_restrictions.h"
#include "base/synchronization/waitable_event.h"
#if defined(OS_WIN)
#include "base/win/scoped_com_initializer.h"
#endif
namespace base {
namespace {
base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool =
    LAZY_INSTANCE_INITIALIZER;
}  
void ThreadQuitHelper() {
  MessageLoop::current()->QuitWhenIdle();
  Thread::SetThreadWasQuitProperly(true);
}
struct Thread::StartupData {
  
  const Thread::Options& options;
  
  WaitableEvent event;
  explicit StartupData(const Options& opt)
      : options(opt),
        event(false, false) {}
};
Thread::Options::Options()
    : message_loop_type(MessageLoop::TYPE_DEFAULT),
      stack_size(0) {
}
Thread::Options::Options(MessageLoop::Type type,
                         size_t size)
    : message_loop_type(type),
      stack_size(size) {
}
Thread::Options::~Options() {
}
Thread::Thread(const char* name)
    :
#if defined(OS_WIN)
      com_status_(NONE),
#endif
      started_(false),
      stopping_(false),
      running_(false),
      startup_data_(NULL),
      thread_(0),
      message_loop_(NULL),
      thread_id_(kInvalidThreadId),
      name_(name) {
}
Thread::~Thread() {
  Stop();
}
bool Thread::Start() {
  Options options;
#if defined(OS_WIN)
  if (com_status_ == STA)
    options.message_loop_type = MessageLoop::TYPE_UI;
#endif
  return StartWithOptions(options);
}
bool Thread::StartWithOptions(const Options& options) {
  DCHECK(!message_loop_);
#if defined(OS_WIN)
  DCHECK((com_status_ != STA) ||
      (options.message_loop_type == MessageLoop::TYPE_UI));
#endif
  SetThreadWasQuitProperly(false);
  StartupData startup_data(options);
  startup_data_ = &startup_data;
  if (!PlatformThread::Create(options.stack_size, this, &thread_)) {
    DLOG(ERROR) << "failed to create thread";
    startup_data_ = NULL;
    return false;
  }
  
  base::ThreadRestrictions::ScopedAllowWait allow_wait;
  startup_data.event.Wait();
  
  startup_data_ = NULL;
  started_ = true;
  DCHECK(message_loop_);
  return true;
}
void Thread::Stop() {
  if (!started_)
    return;
  StopSoon();
  
  
  
  
  
  PlatformThread::Join(thread_);
  
  DCHECK(!message_loop_);
  
  started_ = false;
  stopping_ = false;
}
void Thread::StopSoon() {
  
  
  
  DCHECK_NE(ANNOTATE_UNPROTECTED_READ(thread_id_), PlatformThread::CurrentId());
  if (stopping_ || !message_loop_)
    return;
  stopping_ = true;
  message_loop_->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper));
}
bool Thread::IsRunning() const {
  return running_;
}
void Thread::SetPriority(ThreadPriority priority) {
  
  
  DCHECK_NE(thread_id_, kInvalidThreadId);
  PlatformThread::SetThreadPriority(thread_, priority);
}
void Thread::Run(MessageLoop* message_loop) {
  message_loop->Run();
}
void Thread::SetThreadWasQuitProperly(bool flag) {
  lazy_tls_bool.Pointer()->Set(flag);
}
bool Thread::GetThreadWasQuitProperly() {
  bool quit_properly = true;
#ifndef NDEBUG
  quit_properly = lazy_tls_bool.Pointer()->Get();
#endif
  return quit_properly;
}
void Thread::ThreadMain() {
  {
    
    
    scoped_ptr<MessageLoop> message_loop;
    if (!startup_data_->options.message_pump_factory.is_null()) {
      message_loop.reset(
          new MessageLoop(startup_data_->options.message_pump_factory.Run()));
    } else {
      message_loop.reset(
          new MessageLoop(startup_data_->options.message_loop_type));
    }
    
    thread_id_ = PlatformThread::CurrentId();
    PlatformThread::SetName(name_.c_str());
    ANNOTATE_THREAD_NAME(name_.c_str());  
    message_loop->set_thread_name(name_);
    message_loop_ = message_loop.get();
#if defined(OS_WIN)
    scoped_ptr<win::ScopedCOMInitializer> com_initializer;
    if (com_status_ != NONE) {
      com_initializer.reset((com_status_ == STA) ?
          new win::ScopedCOMInitializer() :
          new win::ScopedCOMInitializer(win::ScopedCOMInitializer::kMTA));
    }
#endif
    
    
    Init();
    running_ = true;
    startup_data_->event.Signal();
    
    
    Run(message_loop_);
    running_ = false;
    
    CleanUp();
#if defined(OS_WIN)
    com_initializer.reset();
#endif
    
    DCHECK(GetThreadWasQuitProperly());
    
    message_loop_ = NULL;
  }
}
}