Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1074)

Unified Diff: content/browser/browser_thread_impl.cc

Issue 1989723003: Use a reader-writer lock to protect the threads table in BrowserThreadImpl. Base URL: https://chromium.googlesource.com/chromium/src.git@rw-lock-incoming-task-queue
Patch Set: Rebase Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/browser_thread_impl.cc
diff --git a/content/browser/browser_thread_impl.cc b/content/browser/browser_thread_impl.cc
index 90b581882fb7bfb0fc35c332215e1a677b364f6b..6ff498e96d41a6a1f171fb91d807407a2e05e14a 100644
--- a/content/browser/browser_thread_impl.cc
+++ b/content/browser/browser_thread_impl.cc
@@ -15,6 +15,7 @@
#include "base/macros.h"
#include "base/profiler/scoped_tracker.h"
#include "base/single_thread_task_runner.h"
+#include "base/synchronization/read_write_lock.h"
#include "base/threading/sequenced_worker_pool.h"
#include "build/build_config.h"
#include "content/public/browser/browser_thread_delegate.h"
@@ -99,7 +100,7 @@ struct BrowserThreadGlobals {
// This lock protects |threads|. Do not read or modify that array
// without holding this lock. Do not block while holding this lock.
- base::Lock lock;
+ base::ReadWriteLock lock;
// This array is protected by |lock|. The threads are not owned by this
// array. Typically, the threads are owned on the UI thread by
@@ -276,14 +277,14 @@ void BrowserThreadImpl::CleanUp() {
// However, the message loop will soon be destructed without any locking. So
// to prevent a race with accessing the message loop in PostTaskHelper(),
// remove this thread from the global array now.
- base::AutoLock lock(globals.lock);
+ base::AutoWriteLock lock(globals.lock);
globals.threads[identifier_] = NULL;
}
void BrowserThreadImpl::Initialize() {
BrowserThreadGlobals& globals = g_globals.Get();
- base::AutoLock lock(globals.lock);
+ base::AutoWriteLock lock(globals.lock);
DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT);
DCHECK(globals.threads[identifier_] == NULL);
globals.threads[identifier_] = this;
@@ -296,7 +297,7 @@ BrowserThreadImpl::~BrowserThreadImpl() {
Stop();
BrowserThreadGlobals& globals = g_globals.Get();
- base::AutoLock lock(globals.lock);
+ base::AutoWriteLock lock(globals.lock);
globals.threads[identifier_] = NULL;
#ifndef NDEBUG
// Double check that the threads are ordered correctly in the enumeration.
@@ -312,7 +313,7 @@ bool BrowserThreadImpl::StartWithOptions(const Options& options) {
// starting, as the new thread can asynchronously start touching the
// table (and other thread's message_loop).
BrowserThreadGlobals& globals = g_globals.Get();
- base::AutoLock lock(globals.lock);
+ base::AutoWriteLock lock(globals.lock);
return Thread::StartWithOptions(options);
}
@@ -337,7 +338,7 @@ bool BrowserThreadImpl::PostTaskHelper(
BrowserThreadGlobals& globals = g_globals.Get();
if (!target_thread_outlives_current)
- globals.lock.Acquire();
+ globals.lock.ReadAcquire();
base::MessageLoop* message_loop =
globals.threads[identifier] ? globals.threads[identifier]->message_loop()
@@ -352,7 +353,7 @@ bool BrowserThreadImpl::PostTaskHelper(
}
if (!target_thread_outlives_current)
- globals.lock.Release();
+ globals.lock.ReadRelease();
return !!message_loop;
}
@@ -402,7 +403,7 @@ bool BrowserThread::IsThreadInitialized(ID identifier) {
return false;
BrowserThreadGlobals& globals = g_globals.Get();
- base::AutoLock lock(globals.lock);
+ base::AutoReadLock lock(globals.lock);
DCHECK(identifier >= 0 && identifier < ID_COUNT);
return globals.threads[identifier] != NULL;
}
@@ -410,7 +411,7 @@ bool BrowserThread::IsThreadInitialized(ID identifier) {
// static
bool BrowserThread::CurrentlyOn(ID identifier) {
BrowserThreadGlobals& globals = g_globals.Get();
- base::AutoLock lock(globals.lock);
+ base::AutoReadLock lock(globals.lock);
DCHECK(identifier >= 0 && identifier < ID_COUNT);
return globals.threads[identifier] &&
globals.threads[identifier]->message_loop() ==
@@ -449,7 +450,7 @@ bool BrowserThread::IsMessageLoopValid(ID identifier) {
return false;
BrowserThreadGlobals& globals = g_globals.Get();
- base::AutoLock lock(globals.lock);
+ base::AutoReadLock lock(globals.lock);
DCHECK(identifier >= 0 && identifier < ID_COUNT);
return globals.threads[identifier] &&
globals.threads[identifier]->message_loop();
@@ -513,7 +514,7 @@ bool BrowserThread::GetCurrentThreadIdentifier(ID* identifier) {
// real work on canary and local dev builds, so the cost of having this here
// should be minimal.
tracked_objects::ScopedTracker tracking_profile(FROM_HERE);
- base::AutoLock lock(globals.lock);
+ base::AutoReadLock lock(globals.lock);
for (int i = 0; i < ID_COUNT; ++i) {
if (globals.threads[i] &&
globals.threads[i]->message_loop() == cur_message_loop) {
@@ -537,7 +538,7 @@ base::MessageLoop* BrowserThread::UnsafeGetMessageLoopForThread(ID identifier) {
return NULL;
BrowserThreadGlobals& globals = g_globals.Get();
- base::AutoLock lock(globals.lock);
+ base::AutoReadLock lock(globals.lock);
base::Thread* thread = globals.threads[identifier];
DCHECK(thread);
base::MessageLoop* loop = thread->message_loop();
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698