1/*
2 * Copyright (C) 2007, 2008, 2010, 2013, 2014 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <atomic>
29#include <wtf/FastMalloc.h>
30#include <wtf/MainThread.h>
31#include <wtf/Noncopyable.h>
32
33namespace WTF {
34
35class ThreadSafeRefCountedBase {
36 WTF_MAKE_NONCOPYABLE(ThreadSafeRefCountedBase);
37 WTF_MAKE_FAST_ALLOCATED;
38public:
39 ThreadSafeRefCountedBase() = default;
40
41 void ref() const
42 {
43 ++m_refCount;
44 }
45
46 bool hasOneRef() const
47 {
48 return refCount() == 1;
49 }
50
51 unsigned refCount() const
52 {
53 return m_refCount;
54 }
55
56protected:
57 // Returns whether the pointer should be freed or not.
58 bool derefBase() const
59 {
60 return !--m_refCount;
61 }
62
63private:
64 mutable std::atomic<unsigned> m_refCount { 1 };
65};
66
67enum class DestructionThread { Any, Main };
68
69template<class T, DestructionThread destructionThread = DestructionThread::Any> class ThreadSafeRefCounted : public ThreadSafeRefCountedBase {
70public:
71 void deref() const
72 {
73 if (!derefBase())
74 return;
75 if (destructionThread == DestructionThread::Any || isMainThread()) {
76 delete static_cast<const T*>(this);
77 return;
78 }
79 callOnMainThread([this] {
80 delete static_cast<const T*>(this);
81 });
82 }
83
84protected:
85 ThreadSafeRefCounted() = default;
86};
87
88} // namespace WTF
89
90using WTF::ThreadSafeRefCounted;
91