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
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127

mojo / core / watch.h [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_CORE_WATCH_H_
#define MOJO_CORE_WATCH_H_

#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "mojo/core/atomic_flag.h"
#include "mojo/core/handle_signals_state.h"
#include "mojo/public/c/system/trap.h"

namespace mojo {
namespace core {

class Dispatcher;
class WatcherDispatcher;

// Encapsulates the state associated with a single watch context within a
// watcher.
//
// Every Watch has its own cancellation state, and is captured by RequestContext
// notification finalizers to avoid redundant context resolution during
// finalizer execution.
class Watch : public base::RefCountedThreadSafe<Watch> {
 public:
  // Constructs a Watch which represents a watch within |watcher| associated
  // with |context|, watching |dispatcher| for |signals|.
  Watch(const scoped_refptr<WatcherDispatcher>& watcher,
        const scoped_refptr<Dispatcher>& dispatcher,
        uintptr_t context,
        MojoHandleSignals signals,
        MojoTriggerCondition condition);

  Watch(const Watch&) = delete;
  Watch& operator=(const Watch&) = delete;

  // Notifies the Watch of a potential state change.
  //
  // If |allowed_to_call_callback| is true, this may add a notification
  // finalizer to the current RequestContext to invoke the watcher's callback
  // with this watch's context. See return values below.
  //
  // This is called directly by WatcherDispatcher whenever the Watch's observed
  // dispatcher notifies the WatcherDispatcher of a state change.
  //
  // Returns |true| if the Watch entered or remains in a ready state as a result
  // of the state change. If |allowed_to_call_callback| was true in this case,
  // the Watch will have also attached a notification finalizer to the current
  // RequestContext.
  //
  // Returns |false| if the
  bool NotifyState(const HandleSignalsState& state,
                   bool allowed_to_call_callback);

  // Notifies the watch of cancellation ASAP. This will always be the last
  // notification sent for the watch.
  void Cancel();

  // Finalizer method for RequestContexts. This method is invoked once for every
  // notification finalizer added to a RequestContext by this object. This calls
  // down into the WatcherDispatcher to do the actual notification call.
  void InvokeCallback(MojoResult result,
                      const HandleSignalsState& state,
                      MojoTrapEventFlags flags);

  const scoped_refptr<Dispatcher>& dispatcher() const { return dispatcher_; }
  uintptr_t context() const { return context_; }

  MojoResult last_known_result() const {
    AssertWatcherLockAcquired();
    return last_known_result_;
  }

  MojoHandleSignalsState last_known_signals_state() const {
    AssertWatcherLockAcquired();
    return last_known_signals_state_;
  }

  bool ready() const {
    AssertWatcherLockAcquired();
    return last_known_result_ == MOJO_RESULT_OK ||
           last_known_result_ == MOJO_RESULT_FAILED_PRECONDITION;
  }

 private:
  friend class base::RefCountedThreadSafe<Watch>;

  ~Watch();

#if DCHECK_IS_ON()
  void AssertWatcherLockAcquired() const;
#else
  void AssertWatcherLockAcquired() const {}
#endif

  const scoped_refptr<WatcherDispatcher> watcher_;
  const scoped_refptr<Dispatcher> dispatcher_;
  const uintptr_t context_;
  const MojoHandleSignals signals_;
  const MojoTriggerCondition condition_;

  // The result code with which this Watch would notify if currently armed,
  // based on the last known signaling state of |dispatcher_|. Guarded by the
  // owning WatcherDispatcher's lock.
  MojoResult last_known_result_ = MOJO_RESULT_UNKNOWN;

  // The last known signaling state of |dispatcher_|. Guarded by the owning
  // WatcherDispatcher's lock.
  MojoHandleSignalsState last_known_signals_state_ = {0, 0};

  // Guards |is_cancelled_| below and mutually excludes individual watch
  // notification executions for this same watch context.
  //
  // Note that this should only be acquired from a RequestContext finalizer to
  // ensure that no other internal locks are already held.
  base::Lock notification_lock_;

  // Guarded by |notification_lock_|.
  bool is_cancelled_ = false;
};

}  // namespace core
}  // namespace mojo

#endif  // MOJO_CORE_WATCH_H_