summaryrefslogtreecommitdiffstats
path: root/chromium/base/threading/thread.cc
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@theqtcompany.com>2015-10-13 13:24:50 +0200
committerAllan Sandfeld Jensen <allan.jensen@theqtcompany.com>2015-10-14 10:57:25 +0000
commitaf3d4809763ef308f08ced947a73b624729ac7ea (patch)
tree4402b911e30383f6c6dace1e8cf3b8e85355db3a /chromium/base/threading/thread.cc
parent0e8ff63a407fe323e215bb1a2c423c09a4747c8a (diff)
BASELINE: Update Chromium to 47.0.2526.14
Also adding in sources needed for spellchecking. Change-Id: Idd44170fa1616f26315188970a8d5ba7d472b18a Reviewed-by: Michael BrĂ¼ning <michael.bruning@theqtcompany.com>
Diffstat (limited to 'chromium/base/threading/thread.cc')
-rw-r--r--chromium/base/threading/thread.cc61
1 files changed, 32 insertions, 29 deletions
diff --git a/chromium/base/threading/thread.cc b/chromium/base/threading/thread.cc
index 7bff24232e2..4b517a1ef26 100644
--- a/chromium/base/threading/thread.cc
+++ b/chromium/base/threading/thread.cc
@@ -62,9 +62,12 @@ Thread::Thread(const std::string& name)
stopping_(false),
running_(false),
thread_(0),
+ id_(kInvalidThreadId),
+ id_event_(true, false),
message_loop_(nullptr),
message_loop_timer_slack_(TIMER_SLACK_NONE),
- name_(name) {
+ name_(name),
+ start_event_(false, false) {
}
Thread::~Thread() {
@@ -87,6 +90,10 @@ bool Thread::StartWithOptions(const Options& options) {
(options.message_loop_type == MessageLoop::TYPE_UI));
#endif
+ // Reset |id_| here to support restarting the thread.
+ id_event_.Reset();
+ id_ = kInvalidThreadId;
+
SetThreadWasQuitProperly(false);
MessageLoop::Type type = options.message_loop_type;
@@ -97,23 +104,16 @@ bool Thread::StartWithOptions(const Options& options) {
scoped_ptr<MessageLoop> message_loop = MessageLoop::CreateUnbound(
type, options.message_pump_factory);
message_loop_ = message_loop.get();
- start_event_.reset(new WaitableEvent(false, false));
+ start_event_.Reset();
// Hold the thread_lock_ while starting a new thread, so that we can make sure
// that thread_ is populated before the newly created thread accesses it.
{
AutoLock lock(thread_lock_);
- bool created;
- if (options.priority == ThreadPriority::NORMAL) {
- created = PlatformThread::Create(options.stack_size, this, &thread_);
- } else {
- created = PlatformThread::CreateWithPriority(options.stack_size, this,
- &thread_, options.priority);
- }
- if (!created) {
+ if (!PlatformThread::CreateWithPriority(options.stack_size, this, &thread_,
+ options.priority)) {
DLOG(ERROR) << "failed to create thread";
message_loop_ = nullptr;
- start_event_.reset();
return false;
}
}
@@ -134,16 +134,17 @@ bool Thread::StartAndWaitForTesting() {
return true;
}
-bool Thread::WaitUntilThreadStarted() {
- if (!start_event_)
+bool Thread::WaitUntilThreadStarted() const {
+ if (!message_loop_)
return false;
base::ThreadRestrictions::ScopedAllowWait allow_wait;
- start_event_->Wait();
+ start_event_.Wait();
return true;
}
void Thread::Stop() {
- if (!start_event_)
+ AutoLock lock(thread_lock_);
+ if (thread_.is_null())
return;
StopSoon();
@@ -154,20 +155,18 @@ void Thread::Stop() {
// the thread exits. Some consumers are abusing the API. Make them stop.
//
PlatformThread::Join(thread_);
+ thread_ = base::PlatformThreadHandle();
- // The thread should NULL message_loop_ on exit.
+ // The thread should nullify message_loop_ on exit.
DCHECK(!message_loop_);
- // The thread no longer needs to be joined.
- start_event_.reset();
-
stopping_ = false;
}
void Thread::StopSoon() {
// We should only be called on the same thread that started us.
- DCHECK_NE(thread_id(), PlatformThread::CurrentId());
+ DCHECK_NE(GetThreadId(), PlatformThread::CurrentId());
if (stopping_ || !message_loop_)
return;
@@ -176,9 +175,11 @@ void Thread::StopSoon() {
task_runner()->PostTask(FROM_HERE, base::Bind(&ThreadQuitHelper));
}
-PlatformThreadId Thread::thread_id() const {
- AutoLock lock(thread_lock_);
- return thread_.id();
+PlatformThreadId Thread::GetThreadId() const {
+ // If the thread is created but not started yet, wait for |id_| being ready.
+ base::ThreadRestrictions::ScopedAllowWait allow_wait;
+ id_event_.Wait();
+ return id_;
}
bool Thread::IsRunning() const {
@@ -211,6 +212,12 @@ bool Thread::GetThreadWasQuitProperly() {
}
void Thread::ThreadMain() {
+ // First, make GetThreadId() available to avoid deadlocks. It could be called
+ // any place in the following thread initialization code.
+ id_ = PlatformThread::CurrentId();
+ DCHECK_NE(kInvalidThreadId, id_);
+ id_event_.Signal();
+
// Complete the initialization of our Thread object.
PlatformThread::SetName(name_.c_str());
ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector.
@@ -231,10 +238,6 @@ void Thread::ThreadMain() {
}
#endif
- // Make sure the thread_id() returns current thread.
- // (This internally acquires lock against PlatformThread::Create)
- DCHECK_EQ(thread_id(), PlatformThread::CurrentId());
-
// Let the thread do extra initialization.
Init();
@@ -243,7 +246,7 @@ void Thread::ThreadMain() {
running_ = true;
}
- start_event_->Signal();
+ start_event_.Signal();
Run(message_loop_);
@@ -264,7 +267,7 @@ void Thread::ThreadMain() {
// We can't receive messages anymore.
// (The message loop is destructed at the end of this block)
- message_loop_ = NULL;
+ message_loop_ = nullptr;
}
} // namespace base