gfx/layers/ipc/AsyncTransactionTracker.h
author Nathan Froyd <froydnj@mozilla.com>
Wed, 07 Oct 2015 15:05:02 -0400
changeset 301510 1f51d1614b9a58e41dd3a1bf2dc6aad34dbb2170
parent 285008 15bc0858e2be5e5f8fbb920b72aca6f96244e5dd
child 301514 91d4539e00cecb658604e021675a923c60ef3235
permissions -rw-r--r--
Bug 1207245 - part 3 - switch all uses of mozilla::RefPtr<T> to nsRefPtr<T>; r=ehsan This commit was generated using the following script, executed at the top level of a typical source code checkout. # Don't modify select files in mfbt/ because it's not worth trying to # tease out the dependencies currently. # # Don't modify anything in media/gmp-clearkey/0.1/ because those files # use their own RefPtr, defined in their own RefCounted.h. find . -name '*.cpp' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ grep -v 'mfbt/RefPtr.h' | \ grep -v 'mfbt/nsRefPtr.h' | \ grep -v 'mfbt/RefCounted.h' | \ grep -v 'media/gmp-clearkey/0.1/' | \ xargs perl -p -i -e ' s/mozilla::RefPtr/nsRefPtr/g; # handle declarations in headers s/\bRefPtr</nsRefPtr</g; # handle local variables in functions s#mozilla/RefPtr.h#mozilla/nsRefPtr.h#; # handle #includes s#mfbt/RefPtr.h#mfbt/nsRefPtr.h#; # handle strange #includes ' # |using mozilla::RefPtr;| is OK; |using nsRefPtr;| is invalid syntax. find . -name '*.cpp' -o -name '*.mm' | xargs sed -i -e '/using nsRefPtr/d' # RefPtr.h used |byRef| for dealing with COM-style outparams. # nsRefPtr.h uses |getter_AddRefs|. # Fixup that mismatch. find . -name '*.cpp' | \ xargs perl -p -i -e 's/byRef/getter_AddRefs/g'

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: sw=2 ts=8 et :
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_layers_AsyncTransactionTracker_h
#define mozilla_layers_AsyncTransactionTracker_h

#include <map>

#include "mozilla/Atomics.h"
#include "mozilla/layers/FenceUtils.h"  // for FenceHandle
#include "mozilla/Monitor.h"      // for Monitor
#include "mozilla/nsRefPtr.h"       // for AtomicRefCounted

namespace mozilla {
namespace layers {

class TextureClient;
class AsyncTransactionTrackersHolder;

/**
 * Object that lets you wait for one or more async transactions to complete.
 */
class AsyncTransactionWaiter
{
public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AsyncTransactionWaiter)

  AsyncTransactionWaiter()
    : mCompletedMonitor("AsyncTransactionWaiter")
    , mWaitCount(0)
  {}

  void IncrementWaitCount()
  {
    MonitorAutoLock lock(mCompletedMonitor);
    ++mWaitCount;
  }
  void DecrementWaitCount()
  {
    MonitorAutoLock lock(mCompletedMonitor);
    MOZ_ASSERT(mWaitCount > 0);
    --mWaitCount;
    if (mWaitCount == 0) {
      mCompletedMonitor.Notify();
    }
  }

  /**
   * Wait until asynchronous transactions complete.
   */
  void WaitComplete();

private:
  ~AsyncTransactionWaiter() {}

  Monitor mCompletedMonitor;
  uint32_t mWaitCount;
};

/**
 * AsyncTransactionTracker tracks asynchronous transaction.
 * It is typically used for asynchronous layer transaction handling.
 */
class AsyncTransactionTracker
{
  friend class AsyncTransactionTrackersHolder;
public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AsyncTransactionTracker)

  explicit AsyncTransactionTracker(AsyncTransactionWaiter* aWaiter = nullptr);

  /**
   * Notify async transaction complete.
   */
  void NotifyComplete();

  /**
   * Notify async transaction cancel.
   */
  void NotifyCancel();

  uint64_t GetId()
  {
    return mSerial;
  }

  /**
   * Called when asynchronous transaction complete.
   */
  virtual void Complete()= 0;

  /**
   * Called when asynchronous transaction is cancelled.
   * The cancel typically happens when IPC is disconnected
   */
  virtual void Cancel()= 0;

  virtual void SetTextureClient(TextureClient* aTextureClient) {}

  virtual void SetReleaseFenceHandle(FenceHandle& aReleaseFenceHandle) {}

protected:
  virtual ~AsyncTransactionTracker();

  static void Initialize()
  {
    if (!sLock) {
      sLock = new Mutex("AsyncTransactionTracker::sLock");
    }
  }

  static void Finalize()
  {
    if (sLock) {
      delete sLock;
      sLock = nullptr;
    }
  }

  static uint64_t GetNextSerial()
  {
    MOZ_ASSERT(sLock);
    MutexAutoLock lock(*sLock);
    ++sSerialCounter;
    return sSerialCounter;
  }

  uint64_t mSerial;
  nsRefPtr<AsyncTransactionWaiter> mWaiter;
  DebugOnly<bool> mCompleted;

  /**
   * gecko does not provide atomic operation for uint64_t.
   * Ensure atomicity by using Mutex.
   */
  static uint64_t sSerialCounter;
  static Mutex* sLock;
};

class AsyncTransactionTrackersHolder
{
public:
  AsyncTransactionTrackersHolder();
  virtual ~AsyncTransactionTrackersHolder();

  static void Initialize()
  {
    if (!sHolderLock) {
      sHolderLock = new Mutex("AsyncTransactionTrackersHolder::sHolderLock");
    }
    AsyncTransactionTracker::Initialize();
  }

  static void Finalize()
  {
    if (sHolderLock) {
      delete sHolderLock;
      sHolderLock = nullptr;
    }
    AsyncTransactionTracker::Finalize();
  }

  void HoldUntilComplete(AsyncTransactionTracker* aTransactionTracker);

  void TransactionCompleteted(uint64_t aTransactionId);

  static void TransactionCompleteted(uint64_t aHolderId, uint64_t aTransactionId);

  static void SetReleaseFenceHandle(FenceHandle& aReleaseFenceHandle,
                                    uint64_t aHolderId,
                                    uint64_t aTransactionId);

  uint64_t GetId()
  {
    return mSerial;
  }

protected:

  static uint64_t GetNextSerial()
  {
    MOZ_ASSERT(sHolderLock);
    MutexAutoLock lock(*sHolderLock);
    ++sSerialCounter;
    return sSerialCounter;
  }

  void TransactionCompletetedInternal(uint64_t aTransactionId);

  void SetReleaseFenceHandle(FenceHandle& aReleaseFenceHandle, uint64_t aTransactionId);

  void ClearAllAsyncTransactionTrackers();

  void DestroyAsyncTransactionTrackersHolder();

  uint64_t mSerial;

  bool mIsTrackersHolderDestroyed;
  std::map<uint64_t, nsRefPtr<AsyncTransactionTracker> > mAsyncTransactionTrackers;

  /**
   * gecko does not provide atomic operation for uint64_t.
   * Ensure atomicity by using Mutex.
   */
  static uint64_t sSerialCounter;
  static Mutex* sHolderLock;

  /**
   * Map of all living AsyncTransactionTrackersHolder instances
   */
  static std::map<uint64_t, AsyncTransactionTrackersHolder*> sTrackersHolders;
};

} // namespace layers
} // namespace mozilla

#endif  // mozilla_layers_AsyncTransactionTracker_h