summaryrefslogtreecommitdiff
path: root/mojo/public/cpp/bindings/sequence_local_sync_event_watcher.h
blob: ad50bde436b79142386d09cc9e97cb383e51f2aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 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.

#ifndef MOJO_PUBLIC_CPP_BINDINGS_SEQUENCE_LOCAL_SYNC_EVENT_WATCHER_H_
#define MOJO_PUBLIC_CPP_BINDINGS_SEQUENCE_LOCAL_SYNC_EVENT_WATCHER_H_

#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 {
 public:
  explicit SequenceLocalSyncEventWatcher(
      const base::RepeatingClosure& callback);
  ~SequenceLocalSyncEventWatcher();

  // 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);

 private:
  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;

  DISALLOW_COPY_AND_ASSIGN(SequenceLocalSyncEventWatcher);
};

}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_SEQUENCE_LOCAL_SYNC_EVENT_WATCHER_H_