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

Side by Side Diff: base/timer/timer.cc

Issue 2958003002: Expose Timer::AbandonAndStop, so it can safely be destroyed on another thread. (Closed)
Patch Set: Removed AbandonAndStop and made Stop abandon. Created 3 years, 5 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
« no previous file with comments | « base/timer/timer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/timer/timer.h" 5 #include "base/timer/timer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 14 matching lines...) Expand all
25 public: 25 public:
26 explicit BaseTimerTaskInternal(Timer* timer) 26 explicit BaseTimerTaskInternal(Timer* timer)
27 : timer_(timer) { 27 : timer_(timer) {
28 } 28 }
29 29
30 ~BaseTimerTaskInternal() { 30 ~BaseTimerTaskInternal() {
31 // This task may be getting cleared because the task runner has been 31 // This task may be getting cleared because the task runner has been
32 // destructed. If so, don't leave Timer with a dangling pointer 32 // destructed. If so, don't leave Timer with a dangling pointer
33 // to this. 33 // to this.
34 if (timer_) 34 if (timer_)
35 timer_->AbandonAndStop(); 35 timer_->Stop();
36 } 36 }
37 37
38 void Run() { 38 void Run() {
39 // |timer_| is nullptr if we were abandoned. 39 // |timer_| is nullptr if we were abandoned.
40 if (!timer_) 40 if (!timer_)
41 return; 41 return;
42 42
43 // |this| will be deleted by the task runner, so Timer needs to forget us: 43 // |this| will be deleted by the task runner, so Timer needs to forget us:
44 timer_->scheduled_task_ = nullptr; 44 timer_->scheduled_task_ = nullptr;
45 45
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 is_repeating_(is_repeating), 93 is_repeating_(is_repeating),
94 retain_user_task_(true), 94 retain_user_task_(true),
95 tick_clock_(tick_clock), 95 tick_clock_(tick_clock),
96 is_running_(false) { 96 is_running_(false) {
97 // See comment in other constructor. 97 // See comment in other constructor.
98 origin_sequence_checker_.DetachFromSequence(); 98 origin_sequence_checker_.DetachFromSequence();
99 } 99 }
100 100
101 Timer::~Timer() { 101 Timer::~Timer() {
102 DCHECK(origin_sequence_checker_.CalledOnValidSequence()); 102 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
103 AbandonAndStop(); 103 Stop();
104 } 104 }
105 105
106 bool Timer::IsRunning() const { 106 bool Timer::IsRunning() const {
107 DCHECK(origin_sequence_checker_.CalledOnValidSequence()); 107 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
108 return is_running_; 108 return is_running_;
109 } 109 }
110 110
111 TimeDelta Timer::GetCurrentDelay() const { 111 TimeDelta Timer::GetCurrentDelay() const {
112 DCHECK(origin_sequence_checker_.CalledOnValidSequence()); 112 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
113 return delay_; 113 return delay_;
(...skipping 21 matching lines...) Expand all
135 user_task_ = user_task; 135 user_task_ = user_task;
136 136
137 Reset(); 137 Reset();
138 } 138 }
139 139
140 void Timer::Stop() { 140 void Timer::Stop() {
141 // TODO(gab): Enable this when it's no longer called racily from 141 // TODO(gab): Enable this when it's no longer called racily from
142 // RunScheduledTask(): https://crbug.com/587199. 142 // RunScheduledTask(): https://crbug.com/587199.
143 // DCHECK(origin_sequence_checker_.CalledOnValidSequence()); 143 // DCHECK(origin_sequence_checker_.CalledOnValidSequence());
144 144
145 AbandonScheduledTask();
145 is_running_ = false; 146 is_running_ = false;
146 147
147 // It's safe to destroy or restart Timer on another sequence after Stop(). 148 // It's safe to destroy or restart Timer on another sequence after Stop().
148 origin_sequence_checker_.DetachFromSequence(); 149 origin_sequence_checker_.DetachFromSequence();
gab 2017/06/27 21:12:23 This should allow deletion to happen on any sequen
ossu-chromium 2017/06/28 14:21:33 Detaching the checker works fine - the problem I'm
gab 2017/07/05 02:36:14 I see, should we just publicly expose AbandonAndSt
ossu-chromium 2017/07/05 10:58:12 Sounds good. It's likely that the change in the cu
149 150
150 if (!retain_user_task_) 151 if (!retain_user_task_)
151 user_task_.Reset(); 152 user_task_.Reset();
152 // No more member accesses here: |this| could be deleted after freeing 153 // No more member accesses here: |this| could be deleted after freeing
153 // |user_task_|. 154 // |user_task_|.
154 } 155 }
155 156
156 void Timer::Reset() { 157 void Timer::Reset() {
157 DCHECK(origin_sequence_checker_.CalledOnValidSequence()); 158 DCHECK(origin_sequence_checker_.CalledOnValidSequence());
158 DCHECK(!user_task_.is_null()); 159 DCHECK(!user_task_.is_null());
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 PostNewScheduledTask(delay_); 258 PostNewScheduledTask(delay_);
258 else 259 else
259 Stop(); 260 Stop();
260 261
261 task.Run(); 262 task.Run();
262 263
263 // No more member accesses here: |this| could be deleted at this point. 264 // No more member accesses here: |this| could be deleted at this point.
264 } 265 }
265 266
266 } // namespace base 267 } // namespace base
OLDNEW
« no previous file with comments | « base/timer/timer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698