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

Unified Diff: base/trace_event/blame_context.cc

Issue 1916843003: Ensure Origin Thread Affinity for BlameContext (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use origin thread affinity Created 4 years, 8 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 | « base/trace_event/blame_context.h ('k') | base/trace_event/blame_context_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/trace_event/blame_context.cc
diff --git a/base/trace_event/blame_context.cc b/base/trace_event/blame_context.cc
index 27d2d2eb7509c2b57f161aa5263c2787cc337a3c..74b4a09b602086f6bb513235c4945460d92e1e58 100644
--- a/base/trace_event/blame_context.cc
+++ b/base/trace_event/blame_context.cc
@@ -5,6 +5,7 @@
#include "base/trace_event/blame_context.h"
#include "base/strings/stringprintf.h"
+#include "base/thread_task_runner_handle.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/trace_event_argument.h"
@@ -24,7 +25,9 @@ BlameContext::BlameContext(const char* category,
id_(id),
parent_scope_(parent_context ? parent_context->scope() : nullptr),
parent_id_(parent_context ? parent_context->id() : 0),
- category_group_enabled_(nullptr) {
+ category_group_enabled_(nullptr),
+ task_runner_(ThreadTaskRunnerHandle::Get()),
+ weak_factory_(this) {
DCHECK(!parent_context || !std::strcmp(name_, parent_context->name()))
<< "Parent blame context must have the same name";
}
@@ -54,6 +57,7 @@ void BlameContext::Leave() {
}
void BlameContext::TakeSnapshot() {
+ DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(WasInitialized());
if (!*category_group_enabled_)
return;
@@ -71,9 +75,18 @@ void BlameContext::TakeSnapshot() {
arg_values, TRACE_EVENT_FLAG_HAS_ID);
}
+// Called by TraceLog from a thread possibly different from the one where this
+// BlameContext instance was constructed. Since any access to TraceLog's list
+// of observers is lock-guarded, we can access members of BlameContext from a
+// different thread via a raw pointer safely without worrying about threading
+// issues or pointer validity.
void BlameContext::OnTraceLogEnabled() {
DCHECK(WasInitialized());
- TakeSnapshot();
+ if (task_runner_->BelongsToCurrentThread())
+ TakeSnapshot();
+ else
+ task_runner_->PostTask(FROM_HERE, Bind(&BlameContext::TakeSnapshot,
Primiano Tucci (use gerrit) 2016/04/26 15:32:15 What happens in the case that BlameContext is dest
Sami 2016/04/26 15:45:26 BlameContext removes itself as an observer in the
+ weak_factory_.GetWeakPtr()));
}
void BlameContext::OnTraceLogDisabled() {}
« no previous file with comments | « base/trace_event/blame_context.h ('k') | base/trace_event/blame_context_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698