| 1 | /* |
| 2 | * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #ifndef CrossThreadRefCounted_h |
| 32 | #define CrossThreadRefCounted_hcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/CrossThreadRefCounted.h:32:9`! |
| 33 | |
| 34 | #include <wtf/Noncopyable.h>cxx . . $ `<file>/src/3rdparty/javascriptcore/JavaScriptCore/wtf/Noncopyable.h`/ |
| 35 | #include <wtf/PassRefPtr.h>cxx . . $ `<file>/src/3rdparty/javascriptcore/JavaScriptCore/wtf/PassRefPtr.h`/ |
| 36 | #include <wtf/RefCounted.h>cxx . . $ `<file>/src/3rdparty/javascriptcore/JavaScriptCore/wtf/RefCounted.h`/ |
| 37 | #include <wtf/Threading.h>cxx . . $ `<file>/src/3rdparty/javascriptcore/JavaScriptCore/wtf/Threading.h`/ |
| 38 | |
| 39 | namespace WTFcxx . . $ QTWTF/ { |
| 40 | |
| 41 | // Used to allowing sharing data across classes and threads (like ThreadedSafeShared). |
| 42 | // |
| 43 | // Why not just use ThreadSafeShared? |
| 44 | // ThreadSafeShared can have a significant perf impact when used in low level classes |
| 45 | // (like UString) that get ref/deref'ed a lot. This class has the benefit of doing fast ref |
| 46 | // counts like RefPtr whenever possible, but it has the downside that you need to copy it |
| 47 | // to use it on another thread. |
| 48 | // |
| 49 | // Is this class threadsafe? |
| 50 | // While each instance of the class is not threadsafe, the copied instance is threadsafe |
| 51 | // with respect to the original and any other copies. The underlying m_data is jointly |
| 52 | // owned by the original instance and all copies. |
| 53 | template<class T> |
| 54 | class CrossThreadRefCountedcxx . . $ QTWTF/CrossThreadRefCounted# : public Noncopyablecxx . . $ Noncopyable# { |
| 55 | public: |
| 56 | static PassRefPtrcxx . . $ QTWTF/PassRefPtr#<CrossThreadRefCountedcxx . . $ QTWTF/CrossThreadRefCounted#<T> > createcxx . . $ QTWTF/CrossThreadRefCounted#create(21865b45fe5a6cba).(T* data) |
| 57 | { |
| 58 | return adoptRefcxx . . $ QTWTF/adoptRef(e761da608bb8c29c).(new CrossThreadRefCountedcxx . . $ QTWTF/CrossThreadRefCounted#<T>(data, 0)); |
| 59 | } |
| 60 | |
| 61 | // Used to make an instance that can be used on another thread. |
| 62 | PassRefPtrcxx . . $ QTWTF/PassRefPtr#<CrossThreadRefCountedcxx . . $ QTWTF/CrossThreadRefCounted#<T> > crossThreadCopycxx . . $ QTWTF/CrossThreadRefCounted#crossThreadCopy(106fd6e31658c569).(); |
| 63 | |
| 64 | void refcxx . . $ QTWTF/CrossThreadRefCounted#ref(49f6e7a06ebc5aa8).(); |
| 65 | void derefcxx . . $ QTWTF/CrossThreadRefCounted#deref(49f6e7a06ebc5aa8).(); |
| 66 | T* releasecxx . . $ QTWTF/CrossThreadRefCounted#release(155dc7f103cdb9db).(); |
| 67 | |
| 68 | bool isSharedcxx . . $ QTWTF/CrossThreadRefCounted#isShared(50ce9a9e25b4a850).() const |
| 69 | { |
| 70 | return !m_refCountercxx . . $ QTWTF/CrossThreadRefCounted#m_refCounter..hasOneRefcxx . . $ QTWTF/RefCountedBase#hasOneRef(50ce9a9e25b4a850).() || (m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter. && !m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter.->hasOneRefcxx . . $ QTWTF/ThreadSafeSharedBase#hasOneRef(c9857a73d68e59ba).()); |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | CrossThreadRefCountedcxx . . $ QTWTF/CrossThreadRefCounted#`CrossThreadRefCounted<T>`(59cdbded5adb606d).(T* data, ThreadSafeSharedBasecxx . . $ QTWTF/ThreadSafeSharedBase#* threadedCounter) |
| 75 | : m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter.(threadedCounter) |
| 76 | , m_datacxx . . $ QTWTF/CrossThreadRefCounted#m_data.(data) |
| 77 | #ifndef NDEBUGcxx . . $ `<built-in>:472:9`! |
| 78 | , m_threadId(0) |
| 79 | #endif |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | ~cxx . . $ QTWTF/CrossThreadRefCounted#`~CrossThreadRefCounted<T>`(49f6e7a06ebc5aa8).CrossThreadRefCounted() |
| 84 | { |
| 85 | if (!m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter.) |
| 86 | delete m_datacxx . . $ QTWTF/CrossThreadRefCounted#m_data.; |
| 87 | } |
| 88 | |
| 89 | void threadSafeDerefcxx . . $ QTWTF/CrossThreadRefCounted#threadSafeDeref(49f6e7a06ebc5aa8).(); |
| 90 | |
| 91 | #ifndef NDEBUGcxx . . $ `<built-in>:472:9`! |
| 92 | bool isOwnedByCurrentThread() const { return !m_threadId || m_threadId == currentThread(); } |
| 93 | #endif |
| 94 | |
| 95 | RefCountedBasecxx . . $ QTWTF/RefCountedBase# m_refCountercxx . . $ QTWTF/CrossThreadRefCounted#m_refCounter.; |
| 96 | ThreadSafeSharedBasecxx . . $ QTWTF/ThreadSafeSharedBase#* m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter.; |
| 97 | T* m_datacxx . . $ QTWTF/CrossThreadRefCounted#m_data.; |
| 98 | #ifndef NDEBUGcxx . . $ `<built-in>:472:9`! |
| 99 | ThreadIdentifier m_threadId; |
| 100 | #endif |
| 101 | }; |
| 102 | |
| 103 | template<class T> |
| 104 | void CrossThreadRefCountedcxx . . $ QTWTF/CrossThreadRefCounted#<T>::refcxx . . $ QTWTF/CrossThreadRefCounted#ref(49f6e7a06ebc5aa8).() |
| 105 | { |
| 106 | ASSERTcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Assertions.h:184:9`!(isOwnedByCurrentThread()); |
| 107 | m_refCountercxx . . $ QTWTF/CrossThreadRefCounted#m_refCounter..refcxx . . $ QTWTF/RefCountedBase#ref(49f6e7a06ebc5aa8).(); |
| 108 | #ifndef NDEBUGcxx . . $ `<built-in>:472:9`! |
| 109 | // Store the threadId as soon as the ref count gets to 2. |
| 110 | // The class gets created with a ref count of 1 and then passed |
| 111 | // to another thread where to ref count get increased. This |
| 112 | // is a heuristic but it seems to always work and has helped |
| 113 | // find some bugs. |
| 114 | if (!m_threadId && m_refCounter.refCount() == 2) |
| 115 | m_threadId = currentThread(); |
| 116 | #endif |
| 117 | } |
| 118 | |
| 119 | template<class T> |
| 120 | void CrossThreadRefCountedcxx . . $ QTWTF/CrossThreadRefCounted#<T>::derefcxx . . $ QTWTF/CrossThreadRefCounted#deref(49f6e7a06ebc5aa8).() |
| 121 | { |
| 122 | ASSERTcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Assertions.h:184:9`!(isOwnedByCurrentThread()); |
| 123 | if (m_refCountercxx . . $ QTWTF/CrossThreadRefCounted#m_refCounter..derefBasecxx . . $ QTWTF/RefCountedBase#derefBase(c9857a73d68e59ba).()) { |
| 124 | threadSafeDerefcxx . . $ QTWTF/CrossThreadRefCounted#threadSafeDeref(49f6e7a06ebc5aa8).(); |
| 125 | delete this; |
| 126 | } else { |
| 127 | #ifndef NDEBUGcxx . . $ `<built-in>:472:9`! |
| 128 | // Clear the threadId when the ref goes to 1 because it |
| 129 | // is safe to be passed to another thread at this point. |
| 130 | if (m_threadId && m_refCounter.refCount() == 1) |
| 131 | m_threadId = 0; |
| 132 | #endif |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | template<class T> |
| 137 | T* CrossThreadRefCountedcxx . . $ QTWTF/CrossThreadRefCounted#<T>::releasecxx . . $ QTWTF/CrossThreadRefCounted#release(155dc7f103cdb9db).() |
| 138 | { |
| 139 | ASSERTcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Assertions.h:184:9`!(!isShared()); |
| 140 | |
| 141 | T* data = m_datacxx . . $ QTWTF/CrossThreadRefCounted#m_data.; |
| 142 | m_datacxx . . $ QTWTF/CrossThreadRefCounted#m_data. = 0; |
| 143 | return data; |
| 144 | } |
| 145 | |
| 146 | template<class T> |
| 147 | PassRefPtrcxx . . $ QTWTF/PassRefPtr#<CrossThreadRefCountedcxx . . $ QTWTF/CrossThreadRefCounted#<T> > CrossThreadRefCountedcxx . . $ QTWTF/CrossThreadRefCounted#<T>::crossThreadCopycxx . . $ QTWTF/CrossThreadRefCounted#crossThreadCopy(106fd6e31658c569).() |
| 148 | { |
| 149 | ASSERTcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Assertions.h:184:9`!(isOwnedByCurrentThread()); |
| 150 | if (m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter.) |
| 151 | m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter.->refcxx . . $ QTWTF/ThreadSafeSharedBase#ref(49f6e7a06ebc5aa8).(); |
| 152 | else |
| 153 | m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter. = new ThreadSafeSharedBasecxx . . $ QTWTF/ThreadSafeSharedBase#(2); |
| 154 | |
| 155 | return adoptRefcxx . . $ QTWTF/adoptRef(e761da608bb8c29c).(new CrossThreadRefCountedcxx . . $ QTWTF/CrossThreadRefCounted#<T>(m_datacxx . . $ QTWTF/CrossThreadRefCounted#m_data., m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter.)); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | template<class T> |
| 160 | void CrossThreadRefCountedcxx . . $ QTWTF/CrossThreadRefCounted#<T>::threadSafeDerefcxx . . $ QTWTF/CrossThreadRefCounted#threadSafeDeref(49f6e7a06ebc5aa8).() |
| 161 | { |
| 162 | if (m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter. && m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter.->derefBasecxx . . $ QTWTF/ThreadSafeSharedBase#derefBase(c9857a73d68e59ba).()) { |
| 163 | delete m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter.; |
| 164 | m_threadSafeRefCountercxx . . $ QTWTF/CrossThreadRefCounted#m_threadSafeRefCounter. = 0; |
| 165 | } |
| 166 | } |
| 167 | } // namespace WTF |
| 168 | |
| 169 | using WTFcxx . . $ `<built-in>:467:9`!::CrossThreadRefCountedcxx . . $ CrossThreadRefCounted#; |
| 170 | |
| 171 | #endif // CrossThreadRefCounted_h |