blob: ad50bde436b79142386d09cc9e97cb383e51f2aa [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/bindings_export.h"
namespace mojo {
// This encapsulates a SyncEventWatcher watching an event shared by all
// |SequenceLocalSyncEventWatcher| on the same sequence. This class is NOT
// sequence-safe in general, but |SignalEvent()| is safe to call from any
// sequence.
// Interfaces which support sync messages use a WaitableEvent to block and
// be signaled when messages are available, but having a WaitableEvent for every
// such interface endpoint would cause the number of WaitableEvents to grow
// arbitrarily large.
// Some platform constraints may limit the number of WaitableEvents the bindings
// layer can wait upon concurrently, so this type is used to keep the number
// of such events fixed at a small constant value per sequence regardless of the
// number of active interface endpoints supporting sync messages on that
// sequence.
class MOJO_CPP_BINDINGS_EXPORT SequenceLocalSyncEventWatcher {
explicit SequenceLocalSyncEventWatcher(
const base::RepeatingClosure& callback);
// Signals the shared event on behalf of this specific watcher. Safe to call
// from any sequence.
void SignalEvent();
// Resets the shared event on behalf of this specific watcher.
void ResetEvent();
// Allows this watcher to be notified during sync wait operations invoked by
// other watchers (for example, other SequenceLocalSyncEventWatchers calling
// |SyncWatch()|) on the same sequence.
void AllowWokenUpBySyncWatchOnSameSequence();
// Blocks the calling sequence until the shared event is signaled on behalf of
// this specific watcher (i.e. until someone calls |SignalEvent()| on |this|).
// Behaves similarly to SyncEventWatcher and SyncHandleWatcher, returning
// |true| when |*should_stop| is set to |true|, or |false| if some other
// (e.g. error) event interrupts the wait.
bool SyncWatch(const bool* should_stop);
class Registration;
class SequenceLocalState;
friend class SequenceLocalState;
const std::unique_ptr<Registration> registration_;
const base::RepeatingClosure callback_;
bool can_wake_up_during_any_watch_ = false;
} // namespace mojo