Chromium Code Reviews| Index: extensions/common/async_event.h |
| diff --git a/extensions/common/async_event.h b/extensions/common/async_event.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ea902a98c6e8ebc1937c074d0181743369d54fe6 |
| --- /dev/null |
| +++ b/extensions/common/async_event.h |
| @@ -0,0 +1,90 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef EXTENSIONS_COMMON_ASYNC_EVENT_H_ |
| +#define EXTENSIONS_COMMON_ASYNC_EVENT_H_ |
| + |
| +#include <vector> |
| + |
| +#include "base/callback_forward.h" |
| +#include "base/logging.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/threading/thread_checker.h" |
| + |
| +namespace base { |
| +class TaskRunner; |
| +} |
| + |
| +namespace tracked_objects { |
| +class Location; |
| +} |
| + |
| +namespace extensions { |
| + |
| +// This class represents an event that's expected to happen once. It |
| +// allows clients to guarantee that code is run after the event has |
| +// happened. If the AsyncEvent is destroyed before it happens, the |
| +// delayed closures are destroyed without being run. |
| +// |
| +// This class is similar to a WaitableEvent combined with several |
| +// WaitableEventWatchers, but using it is simpler. |
| +// |
| +// This class is not thread-safe, and must be used from a single thread. |
| +// |
| +// OneShotEvent? |
| +class AsyncEvent { |
|
Devlin
2013/05/15 00:21:16
Just my opinion, but I think that AsyncEvent is a
Jeffrey Yasskin
2013/05/15 03:07:18
Yeah, this name isn't great. I'd like to use just
|
| + public: |
| + AsyncEvent(); |
| + ~AsyncEvent(); |
| + |
| + // True if MarkHappened has been called. |
| + bool has_happened() const { |
|
Jeffrey Yasskin
2013/05/15 03:07:18
Maybe done, released, or notified?
|
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + return happened_; |
| + } |
| + |
| + // Causes has_happened() to return true and all queued tasks to be |
| + // run in an arbitrary order. This method must only be called once. |
| + void MarkHappened(); |
| + |
| + // Scheduled |task| to be called on |runner| after has_happened() |
| + // becomes true. Inside |task|, if this AsyncEvent is still alive, |
| + // CHECK(has_happened()) will never fail (which implies that |
| + // AsyncEvent::Reset() doesn't exist). |
| + // |
| + // |runner|==NULL indicates that |task| should run on |
| + // MessageLoopProxy::current(). |
| + // |
| + // Tasks may be run in an arbitrary order, not just FIFO. Tasks |
| + // will never be called on the current thread before this function |
| + // returns. |
| + // |
| + // Const because RunAfter doesn't modify the logical state of this |
| + // object (which is just the happened_ bit). |
| + void RunAfter(const tracked_objects::Location& from_here, |
|
Jeffrey Yasskin
2013/05/15 03:07:18
event.RunAfter(task) isn't great either. Maybe eve
|
| + const base::Closure& task, |
| + const scoped_refptr<base::TaskRunner>& runner = 0) const; |
|
Devlin
2013/05/15 00:21:16
Style forbids default arguments here: http://googl
Jeffrey Yasskin
2013/05/15 03:07:18
Sure, switched to 2 overloads instead and removed
|
| + |
| + private: |
| + struct TaskInfo; |
| + |
| + base::ThreadChecker thread_checker_; |
| + |
| + bool happened_; |
| + |
| + // The task list is mutable because it's not part of the logical |
| + // state of the object. This lets us return const references to the |
| + // AsyncEvent to clients that just want to run tasks through it |
| + // without worrying that they'll mark the event as having happened. |
| + // |
| + // Optimization note: We could reduce the size of this class to a |
| + // single pointer by storing |happened_| in the low bit of a |
| + // pointer, and storing the size and capacity of the array (if any) |
| + // on the far end of the pointer. |
| + mutable std::vector<TaskInfo> tasks_; |
| +}; |
| + |
| +}; |
|
Devlin
2013/05/15 00:21:16
s/;/ \/\/ namespace extensions
Jeffrey Yasskin
2013/05/15 03:07:18
Done.
|
| + |
| +#endif // EXTENSIONS_COMMON_ASYNC_EVENT_H_ |
|
Devlin
2013/05/15 00:21:16
nit: remove extra space after //.
Jeffrey Yasskin
2013/05/15 03:07:18
Done.
|