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_SYSTEM_WAITER_H_
6#define MOJO_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/public/c/system/types.h"
14#include "mojo/system/system_impl_export.h"
15
16namespace mojo {
17namespace system {
18
19// IMPORTANT (all-caps gets your attention, right?): |Waiter| methods are called
20// 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// class is thread-safe.
23class MOJO_SYSTEM_IMPL_EXPORT Waiter {
24 public:
25  Waiter();
26  ~Waiter();
27
28  // A |Waiter| can be used multiple times; |Init()| should be called before
29  // each time it's used.
30  void Init();
31
32  // Waits until a suitable |Awake()| is called. (|context| may be null, in
33  // which case, obviously no context is ever returned.)
34  // Returns:
35  //   - 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  //     to that call to |Awake()|.
38  //   - |MOJO_RESULT_DEADLINE_EXCEEDED| if the deadline was exceeded; in this
39  //     case |*context| is not modified.
40  //
41  // 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  // handles passed to |MojoWaitMany()| (or 0 for |MojoWait()|).
44  //
45  // Typical |Awake()| results are:
46  //   - |MOJO_RESULT_OK| if one of the flags passed to
47  //     |MojoWait()|/|MojoWaitMany()| (hence |Dispatcher::AddWaiter()|) was
48  //     satisfied;
49  //   - |MOJO_RESULT_CANCELLED| if a handle (on which
50  //     |MojoWait()|/|MojoWaitMany()| was called) was closed (hence the
51  //     dispatcher closed); and
52  //   - |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  //     the corresponding handle (e.g., if the other end of a message or data
55  //     pipe is closed).
56  MojoResult Wait(MojoDeadline deadline, uint32_t* context);
57
58  // Wake the waiter up with the given result and context (or no-op if it's been
59  // woken up already).
60  void Awake(MojoResult result, uint32_t context);
61
62 private:
63  base::ConditionVariable cv_;  // Associated to |lock_|.
64  base::Lock lock_;             // Protects the following members.
65#ifndef NDEBUG
66  bool initialized_;
67#endif
68  bool awoken_;
69  MojoResult awake_result_;
70  // This is a |uint32_t| because we really only need to store an index (for
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
75  DISALLOW_COPY_AND_ASSIGN(Waiter);
76};
77
78}  // namespace system
79}  // namespace mojo
80
81#endif  // MOJO_SYSTEM_WAITER_H_
82