watch.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2017 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.
  4. #ifndef MOJO_CORE_WATCH_H_
  5. #define MOJO_CORE_WATCH_H_
  6. #include "base/memory/ref_counted.h"
  7. #include "base/synchronization/lock.h"
  8. #include "mojo/core/atomic_flag.h"
  9. #include "mojo/core/handle_signals_state.h"
  10. #include "mojo/public/c/system/trap.h"
  11. namespace mojo {
  12. namespace core {
  13. class Dispatcher;
  14. class WatcherDispatcher;
  15. // Encapsulates the state associated with a single watch context within a
  16. // watcher.
  17. //
  18. // Every Watch has its own cancellation state, and is captured by RequestContext
  19. // notification finalizers to avoid redundant context resolution during
  20. // finalizer execution.
  21. class Watch : public base::RefCountedThreadSafe<Watch> {
  22. public:
  23. // Constructs a Watch which represents a watch within |watcher| associated
  24. // with |context|, watching |dispatcher| for |signals|.
  25. Watch(const scoped_refptr<WatcherDispatcher>& watcher,
  26. const scoped_refptr<Dispatcher>& dispatcher,
  27. uintptr_t context,
  28. MojoHandleSignals signals,
  29. MojoTriggerCondition condition);
  30. Watch(const Watch&) = delete;
  31. Watch& operator=(const Watch&) = delete;
  32. // Notifies the Watch of a potential state change.
  33. //
  34. // If |allowed_to_call_callback| is true, this may add a notification
  35. // finalizer to the current RequestContext to invoke the watcher's callback
  36. // with this watch's context. See return values below.
  37. //
  38. // This is called directly by WatcherDispatcher whenever the Watch's observed
  39. // dispatcher notifies the WatcherDispatcher of a state change.
  40. //
  41. // Returns |true| if the Watch entered or remains in a ready state as a result
  42. // of the state change. If |allowed_to_call_callback| was true in this case,
  43. // the Watch will have also attached a notification finalizer to the current
  44. // RequestContext.
  45. //
  46. // Returns |false| if the
  47. bool NotifyState(const HandleSignalsState& state,
  48. bool allowed_to_call_callback);
  49. // Notifies the watch of cancellation ASAP. This will always be the last
  50. // notification sent for the watch.
  51. void Cancel();
  52. // Finalizer method for RequestContexts. This method is invoked once for every
  53. // notification finalizer added to a RequestContext by this object. This calls
  54. // down into the WatcherDispatcher to do the actual notification call.
  55. void InvokeCallback(MojoResult result,
  56. const HandleSignalsState& state,
  57. MojoTrapEventFlags flags);
  58. const scoped_refptr<Dispatcher>& dispatcher() const { return dispatcher_; }
  59. uintptr_t context() const { return context_; }
  60. MojoResult last_known_result() const {
  61. AssertWatcherLockAcquired();
  62. return last_known_result_;
  63. }
  64. MojoHandleSignalsState last_known_signals_state() const {
  65. AssertWatcherLockAcquired();
  66. return last_known_signals_state_;
  67. }
  68. bool ready() const {
  69. AssertWatcherLockAcquired();
  70. return last_known_result_ == MOJO_RESULT_OK ||
  71. last_known_result_ == MOJO_RESULT_FAILED_PRECONDITION;
  72. }
  73. private:
  74. friend class base::RefCountedThreadSafe<Watch>;
  75. ~Watch();
  76. #if DCHECK_IS_ON()
  77. void AssertWatcherLockAcquired() const;
  78. #else
  79. void AssertWatcherLockAcquired() const {}
  80. #endif
  81. const scoped_refptr<WatcherDispatcher> watcher_;
  82. const scoped_refptr<Dispatcher> dispatcher_;
  83. const uintptr_t context_;
  84. const MojoHandleSignals signals_;
  85. const MojoTriggerCondition condition_;
  86. // The result code with which this Watch would notify if currently armed,
  87. // based on the last known signaling state of |dispatcher_|. Guarded by the
  88. // owning WatcherDispatcher's lock.
  89. MojoResult last_known_result_ = MOJO_RESULT_UNKNOWN;
  90. // The last known signaling state of |dispatcher_|. Guarded by the owning
  91. // WatcherDispatcher's lock.
  92. MojoHandleSignalsState last_known_signals_state_ = {0, 0};
  93. // Guards |is_cancelled_| below and mutually excludes individual watch
  94. // notification executions for this same watch context.
  95. //
  96. // Note that this should only be acquired from a RequestContext finalizer to
  97. // ensure that no other internal locks are already held.
  98. base::Lock notification_lock_;
  99. // Guarded by |notification_lock_|.
  100. bool is_cancelled_ = false;
  101. };
  102. } // namespace core
  103. } // namespace mojo
  104. #endif // MOJO_CORE_WATCH_H_