OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 package org.chromium.chromoting; | 5 package org.chromium.chromoting; |
6 | 6 |
7 import java.util.HashSet; | 7 import java.util.HashSet; |
8 | 8 |
9 /** | 9 /** |
10 * A thread-safe event queue which provides both {@link #add} and {@link #remove } functions with | 10 * A thread-safe event queue which provides both {@link #add} and {@link #remove } functions with |
11 * O(log(n)) time complexity, and a {@link raise} function in the derived class | 11 * O(log(n)) time complexity, and a {@link #raise} function in the derived class |
12 * {@link Event.Raisable} to execute all queued callbacks. | 12 * {@link Event.Raisable} to execute all queued callbacks. |
13 * | 13 * |
14 * @param <ParamT> The parameter used in {@link ParameterRunnable} callback. | 14 * @param <ParamT> The parameter used in {@link ParameterRunnable} callback. |
15 */ | 15 */ |
16 public class Event<ParamT> { | 16 public class Event<ParamT> { |
17 /** A runnable with parameter. */ | 17 /** A runnable with parameter. */ |
18 public static interface ParameterRunnable<ParamT> { | 18 public static interface ParameterRunnable<ParamT> { |
19 void run(ParamT p); | 19 void run(ParamT p); |
20 } | 20 } |
21 | 21 |
22 /** A callback with parameter. */ | 22 /** A callback with parameter. */ |
23 public static interface ParameterCallback<ReturnT, ParamT> { | 23 public static interface ParameterCallback<ReturnT, ParamT> { |
24 ReturnT run(ParamT p); | 24 ReturnT run(ParamT p); |
25 } | 25 } |
26 | 26 |
27 /** | 27 /** |
28 * An event provider version of {@link Event} implementation, provides {@lin k raise} function to | 28 * An event provider version of {@link Event} implementation, provides {@lin k #raise} function |
29 * execute appended {@link ParameterRunnable}, and {@link clear} function to clear all appended | 29 * to execute appended {@link ParameterRunnable}, and {@link clear} function to clear all |
30 * callbacks. | 30 * appended callbacks. |
31 */ | 31 */ |
32 public static final class Raisable<ParamT> extends Event<ParamT> { | 32 public static class Raisable<ParamT> extends Event<ParamT> { |
33 /** Clears all appended callbacks */ | 33 /** Clears all appended callbacks */ |
34 public void clear() { | 34 public void clear() { |
35 synchronized (mSet) { | 35 synchronized (mSet) { |
36 mSet.clear(); | 36 mSet.clear(); |
37 } | 37 } |
38 } | 38 } |
39 | 39 |
40 /** | 40 /** |
41 * Executes all queued {@link ParameterRunnable} with |parameter|, retur ns an integer of | 41 * Executes all queued {@link ParameterRunnable} with |parameter|, retur ns an integer of |
42 * total callbacks executed. Note, if an 'add' function call is executin g concurrently | 42 * total callbacks executed. Note, if an 'add' function call is executin g concurrently |
(...skipping 14 matching lines...) Expand all Loading... | |
57 | 57 |
58 /** Executes |obj| as ParameterRunnable<ParamT> with |parameter| as Para mT. */ | 58 /** Executes |obj| as ParameterRunnable<ParamT> with |parameter| as Para mT. */ |
59 @SuppressWarnings("unchecked") | 59 @SuppressWarnings("unchecked") |
60 private void execute(Object obj, ParamT parameter) { | 60 private void execute(Object obj, ParamT parameter) { |
61 ParameterRunnable<ParamT> runnable = (ParameterRunnable<ParamT>) obj ; | 61 ParameterRunnable<ParamT> runnable = (ParameterRunnable<ParamT>) obj ; |
62 runnable.run(parameter); | 62 runnable.run(parameter); |
63 } | 63 } |
64 } | 64 } |
65 | 65 |
66 /** | 66 /** |
67 * An {@link Raisable} which always executes the newly added {@link Paramete rRunnable} with the | |
Lambros
2016/10/05 19:30:04
s/An/A/
Hzj_jie
2016/10/06 00:08:09
Done.
| |
68 * parameter sent to the last {@link #raise} function call. If the event has not been raised, | |
69 * this class has a consistent behavior as {@link Raisable}. | |
Lambros
2016/10/05 19:30:03
..has consistent behavior with..
or
..behaves the
Hzj_jie
2016/10/06 00:08:09
Done.
| |
70 * <p> This class is useful for some one-time events, such as RenderStub.onC lientSizeChanged(). | |
Lambros
2016/10/05 19:30:04
I might be mistaken, but is it common style to use
Hzj_jie
2016/10/06 00:08:09
AFAICT, without <p> or <br>, the newline character
| |
71 * A later attached runnable will never be able to get the event. | |
72 * <p> The {@link ParameterRunnable} will be executed in the thread which {@ link #add} function | |
Lambros
2016/10/05 19:30:04
..thread which calls {@link #add}..
or
..thread in
Yuwei
2016/10/05 20:50:34
I think to be consistent it could be better to run
Hzj_jie
2016/10/06 00:08:09
Done.
Hzj_jie
2016/10/06 00:08:09
Yes, but I would prefer to remove all the synchron
Yuwei
2016/10/06 01:32:24
Acknowledged.
| |
73 * is called if the event has been raised before. | |
Lambros
2016/10/05 19:30:04
You're saying that the implementation will call th
joedow
2016/10/05 19:46:43
Agreed, I've used events which can either fire syn
Yuwei
2016/10/05 20:50:34
Yep. Running the callbacks in current task will ha
Hzj_jie
2016/10/06 00:08:09
This should not happen, raise is only called from
Yuwei
2016/10/06 01:32:24
Well, you code doesn't disallow circular event dep
Hzj_jie
2016/10/06 19:01:34
I think that's a totally incorrect pattern. It loo
| |
74 * <p> The {@link ParameterRunnable} may be executed twice on different thre ads with exactly | |
Lambros
2016/10/05 19:30:04
Personally, I wouldn't make this a separate paragr
Hzj_jie
2016/10/06 00:08:09
Done.
| |
75 * same parameter. Usually this is not an issue, but consumers need to aware . | |
Lambros
2016/10/05 19:30:04
..to be aware.
But you can remove this anyway.
Hzj_jie
2016/10/06 00:08:09
Done.
| |
76 */ | |
77 public static final class ReproducibleRaisable<ParamT> extends Raisable<Para mT> { | |
joedow
2016/10/05 19:46:43
The name ReproducibleRaisable doesn't tell me that
Hzj_jie
2016/10/06 00:08:09
Changed to PromisedRaisable.
| |
78 private boolean mRaised; | |
79 private ParamT mLastParameter; | |
80 | |
81 @Override | |
82 public Object add(ParameterRunnable<ParamT> runnable) { | |
83 Object result = super.add(runnable); | |
84 if (result != null) { | |
85 synchronized (mSet) { | |
86 if (mRaised) { | |
Yuwei
2016/10/05 20:50:34
Could you do the check without the boolean, say ch
Hzj_jie
2016/10/06 00:08:09
No, not a good idea, we cannot ensure a null-able
Yuwei
2016/10/06 01:32:24
Ah. I think you are right.
| |
87 runnable.run(mLastParameter); | |
88 } | |
89 } | |
90 } | |
91 return result; | |
92 } | |
93 | |
94 @Override | |
95 public int raise(ParamT parameter) { | |
96 synchronized (mSet) { | |
97 mRaised = true; | |
98 mLastParameter = parameter; | |
99 } | |
100 return super.raise(parameter); | |
101 } | |
102 } | |
103 | |
104 /** | |
67 * A self removable {@link ParameterRunner}, uses a boolean {@link Parameter Callback} to decide | 105 * A self removable {@link ParameterRunner}, uses a boolean {@link Parameter Callback} to decide |
68 * whether removes self from {@link Event} or not. | 106 * whether removes self from {@link Event} or not. |
69 */ | 107 */ |
70 private static final class SelfRemovableParameterRunnable<ParamT> | 108 private static final class SelfRemovableParameterRunnable<ParamT> |
71 implements ParameterRunnable<ParamT> { | 109 implements ParameterRunnable<ParamT> { |
72 private final ParameterCallback<Boolean, ParamT> mCallback; | 110 private final ParameterCallback<Boolean, ParamT> mCallback; |
73 private final Event<ParamT> mOwner; | 111 private final Event<ParamT> mOwner; |
74 | 112 |
75 // This lock is used to make sure mEvent is correctly set before remove in run function. | 113 // This lock is used to make sure mEvent is correctly set before remove in run function. |
76 // i.e. mOwner.add and assigment of mEvent need to be atomic. | 114 // i.e. mOwner.add and assigment of mEvent need to be atomic. |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 | 205 |
168 /** | 206 /** |
169 * Returns true if there is no runnable attached to current instance. | 207 * Returns true if there is no runnable attached to current instance. |
170 */ | 208 */ |
171 public boolean isEmpty() { | 209 public boolean isEmpty() { |
172 synchronized (mSet) { | 210 synchronized (mSet) { |
173 return mSet.isEmpty(); | 211 return mSet.isEmpty(); |
174 } | 212 } |
175 } | 213 } |
176 } | 214 } |
OLD | NEW |