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_PUBLIC_CPP_UTILITY_RUN_LOOP_H_
6 #define MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_
10 #include "mojo/public/cpp/system/core.h"
21 // Sets up state needed for RunLoop. This must be invoked before creating a
25 // Cleans state created by Setup().
26 static void TearDown();
28 // Returns the RunLoop for the current thread. Returns NULL if not yet
30 static RunLoop* current();
32 // Registers a RunLoopHandler for the specified handle. Only one handler can
33 // be registered for a specified handle.
34 void AddHandler(RunLoopHandler* handler,
36 MojoWaitFlags wait_flags,
37 MojoDeadline deadline);
38 void RemoveHandler(const Handle& handle);
39 bool HasHandler(const Handle& handle) const;
41 // Runs the loop servicing handles as they are ready. This returns when Quit()
42 // is invoked, or there no more handles.
45 // Runs the loop servicing any handles that are ready. Does not wait for
46 // handles to become ready before returning. Returns early if Quit() is
56 // Contains the data needed to track a request to AddHandler().
60 wait_flags(MOJO_WAIT_FLAG_NONE),
64 RunLoopHandler* handler;
65 MojoWaitFlags wait_flags;
66 MojoTimeTicks deadline;
67 // See description of |RunLoop::next_handler_id_| for details.
71 typedef std::map<Handle, HandlerData> HandleToHandlerData;
73 // Waits for a handle to be ready. Returns after servicing at least one
74 // handle (or there are no more handles) unless |non_blocking| is true,
75 // in which case it will also return if servicing at least one handle
76 // would require blocking. Returns true if a RunLoopHandler was notified.
77 bool Wait(bool non_blocking);
79 // Notifies any handlers whose deadline has expired. Returns true if a
80 // RunLoopHandler was notified.
81 bool NotifyDeadlineExceeded();
83 // Removes the first invalid handle. This is called if MojoWaitMany() finds an
84 // invalid handle. Returns true if a RunLoopHandler was notified.
85 bool RemoveFirstInvalidHandle(const WaitState& wait_state);
87 // Returns the state needed to pass to WaitMany().
88 WaitState GetWaitState(bool non_blocking) const;
90 HandleToHandlerData handler_data_;
92 // If non-NULL we're running (inside Run()). Member references a value on the
96 // An ever increasing value assigned to each HandlerData::id. Used to detect
97 // uniqueness while notifying. That is, while notifying expired timers we copy
98 // |handler_data_| and only notify handlers whose id match. If the id does not
99 // match it means the handler was removed then added so that we shouldn't
101 int next_handler_id_;
103 MOJO_DISALLOW_COPY_AND_ASSIGN(RunLoop);
108 #endif // MOJO_PUBLIC_CPP_UTILITY_RUN_LOOP_H_