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

Side by Side Diff: base/pending_task.cc

Issue 2374193002: Make base::PendingTask support ConvertableToTraceFormat (Closed)
Patch Set: JsonWriter::Write requires a temp var Created 4 years, 2 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/pending_task.h" 5 #include "base/pending_task.h"
6 6
7 #include "base/json/json_writer.h"
8 #include "base/logging.h"
9 #include "base/memory/ptr_util.h"
10 #include "base/trace_event/trace_event.h"
7 #include "base/tracked_objects.h" 11 #include "base/tracked_objects.h"
12 #include "base/values.h"
8 13
9 namespace base { 14 namespace base {
10 15
16 PendingTask::TracingInfo::TracingInfo(
17 const tracked_objects::Location& posted_from)
18 : posted_from_(posted_from) {
19 #if DCHECK_IS_ON()
20 // TracingInfo should only be obtained when tracing is enabled or it will
21 // generate unnecessary copies.
22 bool tracing_enabled;
caseq 2016/09/29 22:46:36 considering this is hot code, you really want to k
gab 2016/10/03 15:18:57 This is only hot code if both (1) dchecks are enab
23 TRACE_EVENT_CATEGORY_GROUP_ENABLED("toplevel", &tracing_enabled);
24 DCHECK(tracing_enabled);
25 #endif // DCHECK_IS_ON()
26 }
27
28 PendingTask::TracingInfo::~TracingInfo() = default;
29
30 void PendingTask::TracingInfo::AppendAsTraceFormat(std::string* out) const {
31 DictionaryValue dict;
32 dict.SetString("src_file", posted_from_.file_name());
33 dict.SetString("src_func", posted_from_.function_name());
34 std::string tmp;
35 JSONWriter::Write(dict, &tmp);
36 out->append(tmp);
37 }
38
11 PendingTask::PendingTask(const tracked_objects::Location& posted_from, 39 PendingTask::PendingTask(const tracked_objects::Location& posted_from,
12 base::Closure task) 40 base::Closure task)
13 : base::TrackingInfo(posted_from, TimeTicks()), 41 : base::TrackingInfo(posted_from, TimeTicks()),
14 task(std::move(task)), 42 task(std::move(task)),
15 posted_from(posted_from), 43 posted_from(posted_from),
16 sequence_num(0), 44 sequence_num(0),
17 nestable(true), 45 nestable(true),
18 is_high_res(false) { 46 is_high_res(false) {
19 } 47 }
20 48
21 PendingTask::PendingTask(const tracked_objects::Location& posted_from, 49 PendingTask::PendingTask(const tracked_objects::Location& posted_from,
22 base::Closure task, 50 base::Closure task,
23 TimeTicks delayed_run_time, 51 TimeTicks delayed_run_time,
24 bool nestable) 52 bool nestable)
25 : base::TrackingInfo(posted_from, delayed_run_time), 53 : base::TrackingInfo(posted_from, delayed_run_time),
26 task(std::move(task)), 54 task(std::move(task)),
27 posted_from(posted_from), 55 posted_from(posted_from),
28 sequence_num(0), 56 sequence_num(0),
29 nestable(nestable), 57 nestable(nestable),
30 is_high_res(false) { 58 is_high_res(false) {
31 } 59 }
32 60
33 PendingTask::PendingTask(PendingTask&& other) = default; 61 PendingTask::PendingTask(PendingTask&& other) = default;
34 62
35 PendingTask::~PendingTask() { 63 PendingTask::~PendingTask() = default;
36 }
37 64
38 PendingTask& PendingTask::operator=(PendingTask&& other) = default; 65 PendingTask& PendingTask::operator=(PendingTask&& other) = default;
39 66
40 bool PendingTask::operator<(const PendingTask& other) const { 67 bool PendingTask::operator<(const PendingTask& other) const {
41 // Since the top of a priority queue is defined as the "greatest" element, we 68 // Since the top of a priority queue is defined as the "greatest" element, we
42 // need to invert the comparison here. We want the smaller time to be at the 69 // need to invert the comparison here. We want the smaller time to be at the
43 // top of the heap. 70 // top of the heap.
44 71
45 if (delayed_run_time < other.delayed_run_time) 72 if (delayed_run_time < other.delayed_run_time)
46 return false; 73 return false;
47 74
48 if (delayed_run_time > other.delayed_run_time) 75 if (delayed_run_time > other.delayed_run_time)
49 return true; 76 return true;
50 77
51 // If the times happen to match, then we use the sequence number to decide. 78 // If the times happen to match, then we use the sequence number to decide.
52 // Compare the difference to support integer roll-over. 79 // Compare the difference to support integer roll-over.
53 return (sequence_num - other.sequence_num) > 0; 80 return (sequence_num - other.sequence_num) > 0;
54 } 81 }
55 82
83 std::unique_ptr<trace_event::ConvertableToTraceFormat>
84 PendingTask::GetTracingInfo() const {
85 return MakeUnique<TracingInfo>(posted_from);
86 }
87
56 } // namespace base 88 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698