gecko-dev/gfx/layers/ipc/ThreadSafeRefcountingWithMainThreadDestruction.h
Sylvestre Ledru 265e672179 Bug 1511181 - Reformat everything to the Google coding style r=ehsan a=clang-format
# ignore-this-changeset

--HG--
extra : amend_source : 4d301d3b0b8711c4692392aa76088ba7fd7d1022
2018-11-30 11:46:48 +01:00

97 lines
5.1 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 THREADSAFEREFCOUNTINGWITHMAINTHREADDESTRUCTION_H_
#define THREADSAFEREFCOUNTINGWITHMAINTHREADDESTRUCTION_H_
#include "base/message_loop.h"
#include "MainThreadUtils.h"
#include "nsThreadUtils.h"
namespace mozilla {
namespace layers {
inline MessageLoop* GetMainLoopAssertingMainThread() {
MOZ_ASSERT(NS_IsMainThread());
return MessageLoop::current();
}
inline MessageLoop* GetMainLoop() {
static MessageLoop* sMainLoop = GetMainLoopAssertingMainThread();
return sMainLoop;
}
struct HelperForMainThreadDestruction {
HelperForMainThreadDestruction() {
MOZ_ASSERT(NS_IsMainThread());
GetMainLoop();
}
~HelperForMainThreadDestruction() { MOZ_ASSERT(NS_IsMainThread()); }
};
template <typename T>
struct DeleteOnMainThreadTask : public Runnable {
T* mToDelete;
explicit DeleteOnMainThreadTask(T* aToDelete)
: Runnable("layers::DeleteOnMainThreadTask"), mToDelete(aToDelete) {}
NS_IMETHOD Run() override {
MOZ_ASSERT(NS_IsMainThread());
mToDelete->DeleteToBeCalledOnMainThread();
return NS_OK;
}
};
} // namespace layers
} // namespace mozilla
#define NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_MAIN_THREAD_DESTRUCTION_AND_RECORDING( \
_class, _recording) \
public: \
NS_METHOD_(MozExternalRefCountType) AddRef(void) { \
MOZ_ASSERT_TYPE_OK_FOR_REFCOUNTING(_class) \
MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt"); \
nsrefcnt count = ++mRefCnt; \
NS_LOG_ADDREF(this, count, #_class, sizeof(*this)); \
return (nsrefcnt)count; \
} \
void DeleteToBeCalledOnMainThread() { \
MOZ_ASSERT(NS_IsMainThread()); \
NS_LOG_RELEASE(this, 0, #_class); \
delete this; \
} \
NS_METHOD_(MozExternalRefCountType) Release(void) { \
MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release"); \
nsrefcnt count = --mRefCnt; \
if (count == 0) { \
if (NS_IsMainThread()) { \
DeleteToBeCalledOnMainThread(); \
} else { \
NS_DispatchToMainThread( \
new mozilla::layers::DeleteOnMainThreadTask<_class>(this)); \
} \
} else { \
NS_LOG_RELEASE(this, count, #_class); \
} \
return count; \
} \
\
protected: \
::mozilla::ThreadSafeAutoRefCntWithRecording<_recording> mRefCnt; \
\
private: \
::mozilla::layers::HelperForMainThreadDestruction \
mHelperForMainThreadDestruction; \
\
public:
#define NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_MAIN_THREAD_DESTRUCTION( \
_class) \
NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_MAIN_THREAD_DESTRUCTION_AND_RECORDING( \
_class, recordreplay::Behavior::DontPreserve)
#endif