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.
5 #ifndef MOJO_SYSTEM_WAITER_H_
6 #define MOJO_SYSTEM_WAITER_H_
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"
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.
23 class MOJO_SYSTEM_IMPL_EXPORT Waiter {
28 // A |Waiter| can be used multiple times; |Init()| should be called before
29 // each time it's used.
32 // Waits until a suitable |Awake()| is called. (|context| may be null, in
33 // which case, obviously no context is ever returned.)
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.
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()|).
45 // Typical |Awake()| results are:
46 // - |MOJO_RESULT_OK| if one of the flags passed to
47 // |MojoWait()|/|MojoWaitMany()| (hence |Dispatcher::AddWaiter()|) was
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
56 MojoResult Wait(MojoDeadline deadline, uint32_t* context);
58 // Wake the waiter up with the given result and context (or no-op if it's been
60 void Awake(MojoResult result, uint32_t context);
63 base::ConditionVariable cv_; // Associated to |lock_|.
64 base::Lock lock_; // Protects the following members.
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_;
75 DISALLOW_COPY_AND_ASSIGN(Waiter);
81 #endif // MOJO_SYSTEM_WAITER_H_