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

Unified Diff: base/trace_event/category_registry.h

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
« no previous file with comments | « no previous file | base/trace_event/category_registry.cc » ('j') | base/trace_event/trace_log.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/trace_event/category_registry.h
diff --git a/base/trace_event/category_registry.h b/base/trace_event/category_registry.h
index da998993c4e1bb5b5629fc8e29e1491b233248e5..80e7cb2ef504918ec70b46e5852b267232c48590 100644
--- a/base/trace_event/category_registry.h
+++ b/base/trace_event/category_registry.h
@@ -18,10 +18,12 @@ struct TraceCategory;
class TraceCategoryTest;
class TraceLog;
-// Keeps track of the state of all tracing categories. The reason why this
-// is a fully static class with global state is to allow to statically define
-// known categories as global linker-initialized structs, without requiring
-// static initializers.
+// Allows fast and thread-safe acces to the state of all tracing categories.
Lei Zhang 2016/11/21 19:11:13 (Maybe later since it's already in CQ) typo: acces
+// All the methods in this class can be concurrently called on multiple threads,
+// unless otherwise noted (e.g., GetOrCreateCategoryLocked).
+// The reason why this is a fully static class with global state is to allow to
+// statically define known categories as global linker-initialized structs,
+// without requiring static initializers.
class BASE_EXPORT CategoryRegistry {
public:
// Allows for-each iterations over a slice of the categories array.
@@ -52,11 +54,18 @@ class BASE_EXPORT CategoryRegistry {
static const TraceCategory* GetCategoryByStatePtr(
const uint8_t* category_state);
+ // Returns a category from its name or nullptr if not found.
+ // The output |category| argument is an undefinitely lived pointer to the
+ // TraceCategory owned by the registry. TRACE_EVENTx macros will cache this
+ // pointer and use it for checks in their fast-paths.
+ static TraceCategory* GetCategoryByName(const char* category_name);
+
static bool IsBuiltinCategory(const TraceCategory*);
private:
friend class TraceCategoryTest;
friend class TraceLog;
+ using CategoryInitializerFn = void (*)(TraceCategory*);
// Only for debugging/testing purposes, is a no-op on release builds.
static void Initialize();
@@ -64,13 +73,14 @@ class BASE_EXPORT CategoryRegistry {
// Resets the state of all categories, to clear up the state between tests.
static void ResetForTesting();
- // The output |category| argument is an undefinitely lived pointer to the
- // TraceCategory owned by the registry. TRACE_EVENTx macros will cache this
- // pointer and use it for checks in their fast-paths.
- // Returns false if the category was already present, true if the category
- // has just been added and hence requires initialization.
- static bool GetOrCreateCategoryByName(const char* category_name,
- TraceCategory** category);
+ // Used to get/create a category in the slow-path. If the category exists
+ // already, this has the same effect of GetCategoryByName and returns false.
+ // If not, a new category is created and the CategoryInitializerFn is invoked
+ // before retuning true. The caller must guarantee serialization: either call
+ // this method from a single thread or hold a lock when calling this.
+ static bool GetOrCreateCategoryLocked(const char* category_name,
+ CategoryInitializerFn,
+ TraceCategory**);
// Allows to iterate over the valid categories in a for-each loop.
// This includes builtin categories such as __metadata.
« no previous file with comments | « no previous file | base/trace_event/category_registry.cc » ('j') | base/trace_event/trace_log.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698