OLD | NEW |
| (Empty) |
1 // Copyright 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 MOJO_EDK_SYSTEM_WAITER_H_ | |
6 #define MOJO_EDK_SYSTEM_WAITER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include "base/macros.h" | |
11 #include "base/synchronization/condition_variable.h" | |
12 #include "base/synchronization/lock.h" | |
13 #include "mojo/edk/system/awakable.h" | |
14 #include "mojo/edk/system/system_impl_export.h" | |
15 #include "mojo/public/c/system/types.h" | |
16 | |
17 namespace mojo { | |
18 namespace system { | |
19 | |
20 // IMPORTANT (all-caps gets your attention, right?): |Waiter| methods are called | |
21 // under other locks, in particular, |Dispatcher::lock_|s, so |Waiter| methods | |
22 // must never call out to other objects (in particular, |Dispatcher|s). This | |
23 // class is thread-safe. | |
24 class MOJO_SYSTEM_IMPL_EXPORT Waiter : public Awakable { | |
25 public: | |
26 Waiter(); | |
27 ~Waiter(); | |
28 | |
29 // A |Waiter| can be used multiple times; |Init()| should be called before | |
30 // each time it's used. | |
31 void Init(); | |
32 | |
33 // Waits until a suitable |Awake()| is called. (|context| may be null, in | |
34 // which case, obviously no context is ever returned.) | |
35 // Returns: | |
36 // - The result given to the first call to |Awake()| (possibly before this | |
37 // call to |Wait()|); in this case, |*context| is set to the value passed | |
38 // to that call to |Awake()|. | |
39 // - |MOJO_RESULT_DEADLINE_EXCEEDED| if the deadline was exceeded; in this | |
40 // case |*context| is not modified. | |
41 // | |
42 // Usually, the context passed to |Awake()| will be the value passed to | |
43 // |Dispatcher::AddAwakable()|, which is usually the index to the array of | |
44 // handles passed to |MojoWaitMany()| (or 0 for |MojoWait()|). | |
45 // | |
46 // Typical |Awake()| results are: | |
47 // - |MOJO_RESULT_OK| if one of the flags passed to | |
48 // |MojoWait()|/|MojoWaitMany()| (hence |Dispatcher::AddAwakable()|) was | |
49 // satisfied; | |
50 // - |MOJO_RESULT_CANCELLED| if a handle (on which | |
51 // |MojoWait()|/|MojoWaitMany()| was called) was closed (hence the | |
52 // dispatcher closed); and | |
53 // - |MOJO_RESULT_FAILED_PRECONDITION| if one of the set of flags passed to | |
54 // |MojoWait()|/|MojoWaitMany()| cannot or can no longer be satisfied by | |
55 // the corresponding handle (e.g., if the other end of a message or data | |
56 // pipe is closed). | |
57 MojoResult Wait(MojoDeadline deadline, uint32_t* context); | |
58 | |
59 // Wake the waiter up with the given result and context (or no-op if it's been | |
60 // woken up already). | |
61 bool Awake(MojoResult result, uintptr_t context) override; | |
62 | |
63 private: | |
64 base::ConditionVariable cv_; // Associated to |lock_|. | |
65 base::Lock lock_; // Protects the following members. | |
66 #ifndef NDEBUG | |
67 bool initialized_; | |
68 #endif | |
69 bool awoken_; | |
70 MojoResult awake_result_; | |
71 uintptr_t awake_context_; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(Waiter); | |
74 }; | |
75 | |
76 } // namespace system | |
77 } // namespace mojo | |
78 | |
79 #endif // MOJO_EDK_SYSTEM_WAITER_H_ | |
OLD | NEW |