OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkAtomics.h" | |
8 #include "SkEventTracer.h" | 9 #include "SkEventTracer.h" |
9 #include "SkOnce.h" | 10 #include "SkLazyPtr.h" |
10 | 11 |
11 class SkDefaultEventTracer: public SkEventTracer { | 12 class SkDefaultEventTracer : public SkEventTracer { |
12 virtual SkEventTracer::Handle | 13 virtual SkEventTracer::Handle |
13 addTraceEvent(char phase, | 14 addTraceEvent(char phase, |
14 const uint8_t* categoryEnabledFlag, | 15 const uint8_t* categoryEnabledFlag, |
15 const char* name, | 16 const char* name, |
16 uint64_t id, | 17 uint64_t id, |
17 int numArgs, | 18 int numArgs, |
18 const char** argNames, | 19 const char** argNames, |
19 const uint8_t* argTypes, | 20 const uint8_t* argTypes, |
20 const uint64_t* argValues, | 21 const uint64_t* argValues, |
21 uint8_t flags) override { return 0; } | 22 uint8_t flags) override { return 0; } |
22 | 23 |
23 virtual void | 24 virtual void |
24 updateTraceEventDuration(const uint8_t* categoryEnabledFlag, | 25 updateTraceEventDuration(const uint8_t* categoryEnabledFlag, |
25 const char* name, | 26 const char* name, |
26 SkEventTracer::Handle handle) override {}; | 27 SkEventTracer::Handle handle) override {}; |
27 | 28 |
28 const uint8_t* getCategoryGroupEnabled(const char* name) override { | 29 const uint8_t* getCategoryGroupEnabled(const char* name) override { |
29 static uint8_t no = 0; | 30 static uint8_t no = 0; |
30 return &no; | 31 return &no; |
31 }; | 32 }; |
32 virtual const char* getCategoryGroupName( | 33 virtual const char* getCategoryGroupName( |
33 const uint8_t* categoryEnabledFlag) override { | 34 const uint8_t* categoryEnabledFlag) override { |
34 static const char* dummy = "dummy"; | 35 static const char* dummy = "dummy"; |
35 return dummy; | 36 return dummy; |
36 }; | 37 }; |
37 }; | 38 }; |
38 | 39 |
39 SkEventTracer* SkEventTracer::gInstance; | 40 // We prefer gUserTracer if it's been set, otherwise we fall back on gDefaultTra cer. |
41 static SkEventTracer* gUserTracer = nullptr; | |
42 SK_DECLARE_STATIC_LAZY_PTR(SkDefaultEventTracer, gDefaultTracer); | |
40 | 43 |
41 static void cleanup_tracer() { | 44 // We can use relaxed memory order for gUserTracer loads and stores. |
42 // calling SetInstance will delete the existing instance. | 45 // It's not guarding anything but itself. |
43 SkEventTracer::SetInstance(NULL); | 46 |
47 void SkEventTracer::SetInstance(SkEventTracer* tracer) { | |
48 SkASSERT(nullptr == sk_atomic_load(&gUserTracer, sk_memory_order_relaxed)); | |
49 sk_atomic_store(&gUserTracer, tracer, sk_memory_order_relaxed); | |
50 // An atomic load during process shutdown is probably overkill, but safe ove rkill. | |
51 atexit([](){ SkDELETE(sk_atomic_load(&gUserTracer, sk_memory_order_relaxed)) ; }); | |
44 } | 52 } |
45 | 53 |
46 static void intialize_default_tracer(SkEventTracer* current_instance) { | 54 SkEventTracer* SkEventTracer::GetInstance() { |
47 if (NULL == current_instance) { | 55 if (SkEventTracer* tracer = sk_atomic_load(&gUserTracer, sk_memory_order_rel axed)) { |
Alexander Potapenko
2015/04/21 15:55:38
Since you're dereferincing this pointer, you need
| |
48 SkEventTracer::SetInstance(SkNEW(SkDefaultEventTracer)); | 56 return tracer; |
49 } | 57 } |
50 atexit(cleanup_tracer); | 58 return gDefaultTracer.get(); |
51 } | 59 } |
52 | |
53 | |
54 SK_DECLARE_STATIC_ONCE(once); | |
55 SkEventTracer* SkEventTracer::GetInstance() { | |
56 SkOnce(&once, intialize_default_tracer, SkEventTracer::gInstance); | |
57 SkASSERT(SkEventTracer::gInstance); | |
58 return SkEventTracer::gInstance; | |
59 } | |
OLD | NEW |