OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef SYNC_INTERNAL_API_PUBLIC_BASE_ABORT_SIGNALLER_H_ | |
6 #define SYNC_INTERNAL_API_PUBLIC_BASE_ABORT_SIGNALLER_H_ | |
7 | |
8 #include "base/synchronization/lock.h" | |
9 #include "sync/base/sync_export.h" | |
10 | |
11 namespace syncer { | |
12 | |
13 class CancellationObserver; | |
14 | |
15 // This class is used to allow one thread to request that another abort and | |
16 // return early. | |
17 // | |
18 // The signalling thread owns this class and my call RequestStop() at any time. | |
19 // After that call, this class' IsStopRequested() will always return true. The | |
20 // intended use case is that the task intending to support early exit will | |
21 // periodically check the value of IsStopRequested() to see if it should return | |
22 // early. | |
23 // | |
24 // The receiving task may also choose to register an CancellationObserver whose | |
25 // OnStopRequested() method will be executed on the signaller's thread when | |
26 // RequestStop() is called. This may be used for sending an early Signal() to a | |
27 // WaitableEvent. The registration of the handler is necessarily racy. If | |
28 // RequestStop() is executes before TryRegisterHandler(), TryRegisterHandler() | |
29 // will not perform any registration and return false. That function's caller | |
30 // must handle this case. | |
31 // | |
32 // This class supports only one handler, though it could easily support multiple | |
33 // observers if we found a use case for such a feature. | |
34 class SYNC_EXPORT_PRIVATE CancellationSignal { | |
35 public: | |
36 CancellationSignal(); | |
37 ~CancellationSignal(); | |
38 | |
39 // Tries to register a handler to be invoked when RequestStop() is called. | |
40 // | |
41 // If RequestStop() has already been called, returns false without registering | |
42 // the handler. Returns true when the registration is successful. | |
43 // | |
44 // If the registration was successful, the handler must be unregistered with | |
45 // UnregisterHandler before this CancellationSignal is destroyed. | |
46 bool TryRegisterHandler(CancellationObserver* handler); | |
47 | |
48 // Unregisters the abort handler. | |
49 void UnregisterHandler(CancellationObserver* handler); | |
50 | |
51 // Returns true if RequestStop() has been called. | |
52 bool IsStopRequested(); | |
53 | |
54 // Sets the stop_requested_ flag and calls the OnStopRequested() method of the | |
55 // registered handler, if one exists. | |
akalin
2013/09/05 16:06:35
might want to mention that OnStopRequested() is ca
rlarocque
2013/09/05 22:37:06
Done.
| |
56 void RequestStop(); | |
57 | |
58 private: | |
59 // Protects all members of this class. | |
60 base::Lock stop_requested_lock_; | |
61 | |
62 // True if RequestStop() has been invoked. | |
63 bool stop_requested_; | |
64 | |
65 // The registered abort handler. May be NULL. | |
66 CancellationObserver* handler_; | |
67 }; | |
68 | |
69 } // namespace syncer | |
70 | |
71 #endif // SYNC_INTERNAL_API_PUBLIC_BASE_ABORT_SIGNALLER_H_ | |
OLD | NEW |