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