ipc/chromium/src/base/thread.cc

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 #include "base/thread.h"
     7 #include "base/lazy_instance.h"
     8 #include "base/string_util.h"
     9 #include "base/thread_local.h"
    10 #include "base/waitable_event.h"
    11 #include "GeckoProfiler.h"
    12 #include "mozilla/IOInterposer.h"
    14 #ifdef MOZ_TASK_TRACER
    15 #include "GeckoTaskTracer.h"
    16 #endif
    18 namespace base {
    20 // This task is used to trigger the message loop to exit.
    21 class ThreadQuitTask : public Task {
    22  public:
    23   virtual void Run() {
    24     MessageLoop::current()->Quit();
    25     Thread::SetThreadWasQuitProperly(true);
    26   }
    27 };
    29 // Used to pass data to ThreadMain.  This structure is allocated on the stack
    30 // from within StartWithOptions.
    31 struct Thread::StartupData {
    32   // We get away with a const reference here because of how we are allocated.
    33   const Thread::Options& options;
    35   // Used to synchronize thread startup.
    36   WaitableEvent event;
    38   explicit StartupData(const Options& opt)
    39       : options(opt),
    40         event(false, false) {}
    41 };
    43 Thread::Thread(const char *name)
    44     : startup_data_(NULL),
    45       thread_(0),
    46       message_loop_(NULL),
    47       thread_id_(0),
    48       name_(name) {
    49 }
    51 Thread::~Thread() {
    52   Stop();
    53 }
    55 namespace {
    57 // We use this thread-local variable to record whether or not a thread exited
    58 // because its Stop method was called.  This allows us to catch cases where
    59 // MessageLoop::Quit() is called directly, which is unexpected when using a
    60 // Thread to setup and run a MessageLoop.
    61 base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool(
    62     base::LINKER_INITIALIZED);
    64 }  // namespace
    66 void Thread::SetThreadWasQuitProperly(bool flag) {
    67   lazy_tls_bool.Pointer()->Set(flag);
    68 }
    70 bool Thread::GetThreadWasQuitProperly() {
    71   bool quit_properly = true;
    72 #ifndef NDEBUG
    73   quit_properly = lazy_tls_bool.Pointer()->Get();
    74 #endif
    75   return quit_properly;
    76 }
    78 bool Thread::Start() {
    79   return StartWithOptions(Options());
    80 }
    82 bool Thread::StartWithOptions(const Options& options) {
    83   DCHECK(!message_loop_);
    85   SetThreadWasQuitProperly(false);
    87   StartupData startup_data(options);
    88   startup_data_ = &startup_data;
    90   if (!PlatformThread::Create(options.stack_size, this, &thread_)) {
    91     DLOG(ERROR) << "failed to create thread";
    92     startup_data_ = NULL;  // Record that we failed to start.
    93     return false;
    94   }
    96   // Wait for the thread to start and initialize message_loop_
    97   startup_data.event.Wait();
    99   DCHECK(message_loop_);
   100   return true;
   101 }
   103 void Thread::Stop() {
   104   if (!thread_was_started())
   105     return;
   107   // We should only be called on the same thread that started us.
   108   DCHECK_NE(thread_id_, PlatformThread::CurrentId());
   110   // StopSoon may have already been called.
   111   if (message_loop_)
   112     message_loop_->PostTask(FROM_HERE, new ThreadQuitTask());
   114   // Wait for the thread to exit.  It should already have terminated but make
   115   // sure this assumption is valid.
   116   //
   117   // TODO(darin): Unfortunately, we need to keep message_loop_ around until
   118   // the thread exits.  Some consumers are abusing the API.  Make them stop.
   119   //
   120   PlatformThread::Join(thread_);
   122   // The thread can't receive messages anymore.
   123   message_loop_ = NULL;
   125   // The thread no longer needs to be joined.
   126   startup_data_ = NULL;
   127 }
   129 void Thread::StopSoon() {
   130   if (!message_loop_)
   131     return;
   133   // We should only be called on the same thread that started us.
   134   DCHECK_NE(thread_id_, PlatformThread::CurrentId());
   136   // We had better have a message loop at this point!  If we do not, then it
   137   // most likely means that the thread terminated unexpectedly, probably due
   138   // to someone calling Quit() on our message loop directly.
   139   DCHECK(message_loop_);
   141   message_loop_->PostTask(FROM_HERE, new ThreadQuitTask());
   142 }
   144 void Thread::ThreadMain() {
   145   char aLocal;
   146   profiler_register_thread(name_.c_str(), &aLocal);
   147   mozilla::IOInterposer::RegisterCurrentThread();
   149   // The message loop for this thread.
   150   MessageLoop message_loop(startup_data_->options.message_loop_type);
   152   // Complete the initialization of our Thread object.
   153   thread_id_ = PlatformThread::CurrentId();
   154   PlatformThread::SetName(name_.c_str());
   155   message_loop.set_thread_name(name_);
   156   message_loop.set_hang_timeouts(startup_data_->options.transient_hang_timeout,
   157                                  startup_data_->options.permanent_hang_timeout);
   158   message_loop_ = &message_loop;
   160   // Let the thread do extra initialization.
   161   // Let's do this before signaling we are started.
   162   Init();
   164   startup_data_->event.Signal();
   165   // startup_data_ can't be touched anymore since the starting thread is now
   166   // unlocked.
   168   message_loop.Run();
   170   // Let the thread do extra cleanup.
   171   CleanUp();
   173   // Assert that MessageLoop::Quit was called by ThreadQuitTask.
   174   DCHECK(GetThreadWasQuitProperly());
   176   mozilla::IOInterposer::UnregisterCurrentThread();
   177   profiler_unregister_thread();
   179 #ifdef MOZ_TASK_TRACER
   180   mozilla::tasktracer::FreeTraceInfo();
   181 #endif
   183   // We can't receive messages anymore.
   184   message_loop_ = NULL;
   185   thread_id_ = 0;
   186 }
   188 }  // namespace base

mercurial