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

Unified Diff: base/trace_event/trace_log.cc

Issue 2495173002: tracing: fix races in CategoryRegistry (Closed)
Patch Set: . Created 4 years, 1 month 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
Index: base/trace_event/trace_log.cc
diff --git a/base/trace_event/trace_log.cc b/base/trace_event/trace_log.cc
index f76393cf230a3e97772d3cee734ffac79b0ba5fc..4b21437c66114667b725e3dcfb1b70d7ea18f84b 100644
--- a/base/trace_event/trace_log.cc
+++ b/base/trace_event/trace_log.cc
@@ -493,11 +493,18 @@ const unsigned char* TraceLog::GetCategoryGroupEnabled(
DCHECK(!CategoryRegistry::kCategoryAlreadyShutdown->is_enabled());
return CategoryRegistry::kCategoryAlreadyShutdown->state_ptr();
}
- TraceCategory* category = nullptr;
- bool is_new_category =
- CategoryRegistry::GetOrCreateCategoryByName(category_group, &category);
Primiano Tucci (use gerrit) 2016/11/16 16:46:54 Essentially this was the core of the problem. The
- if (is_new_category)
- tracelog->UpdateCategoryState(category);
+ TraceCategory* category = CategoryRegistry::GetCategoryByName(category_group);
+ if (!category) {
+ // Slow path: in the case of a new category we have to repeat the check
+ // holding the lock, as multiple threads might have reached this point
+ // at the same time.
+ auto category_initializer = [](TraceCategory* category) {
+ TraceLog::GetInstance()->UpdateCategoryState(category);
+ };
+ AutoLock lock(tracelog->lock_);
+ CategoryRegistry::GetOrCreateCategoryLocked(
+ category_group, category_initializer, &category);
+ }
DCHECK(category->state_ptr());
return category->state_ptr();
}
« base/trace_event/category_registry.h ('K') | « base/trace_event/trace_category_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698