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

gpu / command_buffer / service / ref_counted_lock.h [blame]

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

#ifndef GPU_COMMAND_BUFFER_SERVICE_REF_COUNTED_LOCK_H_
#define GPU_COMMAND_BUFFER_SERVICE_REF_COUNTED_LOCK_H_

#include <optional>

#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "gpu/gpu_gles2_export.h"

namespace gpu {

// Ref counted wrapper for base::Lock.
class GPU_GLES2_EXPORT RefCountedLock
    : public base::RefCountedThreadSafe<RefCountedLock> {
 public:
  RefCountedLock();

  // Disallow copy and assign.
  RefCountedLock(const RefCountedLock&) = delete;
  RefCountedLock& operator=(const RefCountedLock&) = delete;

  virtual base::Lock* GetDrDcLockPtr();
  virtual void AssertAcquired();

 protected:
  virtual ~RefCountedLock();

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

  base::Lock lock_;
};

// Helper class for handling RefCountedLock for drdc usage.
class GPU_GLES2_EXPORT RefCountedLockHelperDrDc {
 public:
  explicit RefCountedLockHelperDrDc(scoped_refptr<RefCountedLock> lock);
  ~RefCountedLockHelperDrDc();

  base::Lock* GetDrDcLockPtr() const {
    return lock_ ? lock_->GetDrDcLockPtr() : nullptr;
  }

  const scoped_refptr<RefCountedLock>& GetDrDcLock() { return lock_; }

  void AssertAcquiredDrDcLock() const {
    if (lock_)
      lock_->AssertAcquired();
  }

  std::optional<base::AutoLockMaybe> GetScopedDrDcLock() const {
    return std::optional<base::AutoLockMaybe>(GetDrDcLockPtr());
  }

 private:
  mutable scoped_refptr<RefCountedLock> lock_;
};

}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_SERVICE_REF_COUNTED_LOCK_H_