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