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

cc / base / unique_notifier.h [blame]

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

#ifndef CC_BASE_UNIQUE_NOTIFIER_H_
#define CC_BASE_UNIQUE_NOTIFIER_H_

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "cc/base/base_export.h"

namespace base {
class SequencedTaskRunner;
}  // namespace base

namespace cc {

// Callers must ensure that they only schedule the notifier on the same thread
// that the provided |task_runner| runs on.
class CC_BASE_EXPORT UniqueNotifier {
 public:
  // Configure this notifier to issue the |closure| notification when scheduled.
  UniqueNotifier(base::SequencedTaskRunner* task_runner,
                 base::RepeatingClosure closure);
  UniqueNotifier(const UniqueNotifier&) = delete;

  // Destroying the notifier will ensure that no further notifications will
  // happen from this class.
  ~UniqueNotifier();

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

  // Schedule a notification to be run. If another notification is already
  // pending, then only one notification will take place.
  void Schedule();

  // Cancel a pending notification, if one was scheduled.
  void Cancel();

 private:
  void Notify();

  // TODO(dcheng): How come this doesn't need to hold a ref to the task runner?
  const raw_ptr<base::SequencedTaskRunner> task_runner_;
  const base::RepeatingClosure closure_;

  // Lock should be held before modifying |notification_pending_|.
  base::Lock lock_;
  bool notification_pending_;

  base::WeakPtrFactory<UniqueNotifier> weak_ptr_factory_{this};
};

}  // namespace cc

#endif  // CC_BASE_UNIQUE_NOTIFIER_H_