Ignore:
Timestamp:
Mar 9, 2009, 2:01:42 PM (16 years ago)
Author:
[email protected]
Message:

2009-03-09 David Levin <[email protected]>

Reviewed by Darin Adler.

Bug 23175: String and UString should be able to share a UChar* buffer.
<https://bugs.webkit.org/show_bug.cgi?id=23175>

Add CrossThreadRefCounted.

  • wtf/CrossThreadRefCounted.h: Added. (WTF::CrossThreadRefCounted::create): (WTF::CrossThreadRefCounted::isShared): (WTF::CrossThreadRefCounted::dataAccessMustBeThreadSafe): (WTF::CrossThreadRefCounted::mayBePassedToAnotherThread): (WTF::CrossThreadRefCounted::CrossThreadRefCounted): (WTF::CrossThreadRefCounted::~CrossThreadRefCounted): (WTF::CrossThreadRefCounted::ref): (WTF::CrossThreadRefCounted::deref): (WTF::CrossThreadRefCounted::release): (WTF::CrossThreadRefCounted::copy): (WTF::CrossThreadRefCounted::threadSafeDeref):
  • wtf/RefCounted.h:
  • wtf/Threading.h: (WTF::ThreadSafeSharedBase::ThreadSafeSharedBase): (WTF::ThreadSafeSharedBase::derefBase): (WTF::ThreadSafeShared::ThreadSafeShared): (WTF::ThreadSafeShared::deref):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/Threading.h

    r40122 r41536  
    206206#endif
    207207
    208 template<class T> class ThreadSafeShared : Noncopyable {
    209 public:
    210     ThreadSafeShared(int initialRefCount = 1)
     208class ThreadSafeSharedBase : Noncopyable {
     209public:
     210    ThreadSafeSharedBase(int initialRefCount = 1)
    211211        : m_refCount(initialRefCount)
    212212    {
     
    223223    }
    224224
    225     void deref()
     225    bool hasOneRef()
     226    {
     227        return refCount() == 1;
     228    }
     229
     230    int refCount() const
     231    {
     232#if !USE(LOCKFREE_THREADSAFESHARED)
     233        MutexLocker locker(m_mutex);
     234#endif
     235        return static_cast<int const volatile &>(m_refCount);
     236    }
     237
     238protected:
     239    // Returns whether the pointer should be freed or not.
     240    bool derefBase()
    226241    {
    227242#if USE(LOCKFREE_THREADSAFESHARED)
    228243        if (atomicDecrement(&m_refCount) <= 0)
    229 #else
     244            return true;
     245#else
     246        int refCount;
    230247        {
    231248            MutexLocker locker(m_mutex);
    232249            --m_refCount;
     250            refCount = m_refCount;
    233251        }
    234         if (m_refCount <= 0)
    235 #endif
    236             delete static_cast<T*>(this);
    237     }
    238 
    239     bool hasOneRef()
    240     {
    241         return refCount() == 1;
    242     }
    243 
    244     int refCount() const
    245     {
    246 #if !USE(LOCKFREE_THREADSAFESHARED)
    247         MutexLocker locker(m_mutex);
    248 #endif
    249         return static_cast<int const volatile &>(m_refCount);
     252        if (refCount <= 0)
     253            return true;
     254#endif
     255        return false;
    250256    }
    251257
    252258private:
     259    template<class T>
     260    friend class CrossThreadRefCounted;
     261
    253262    int m_refCount;
    254263#if !USE(LOCKFREE_THREADSAFESHARED)
    255264    mutable Mutex m_mutex;
    256265#endif
     266};
     267
     268template<class T> class ThreadSafeShared : public ThreadSafeSharedBase {
     269public:
     270    ThreadSafeShared(int initialRefCount = 1)
     271        : ThreadSafeSharedBase(initialRefCount)
     272    {
     273    }
     274
     275    void deref()
     276    {
     277        if (derefBase())
     278            delete static_cast<T*>(this);
     279    }
    257280};
    258281
Note: See TracChangeset for help on using the changeset viewer.