xpcom/glue/Mutex.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/glue/Mutex.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,235 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: sw=4 ts=4 et :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef mozilla_Mutex_h
    1.11 +#define mozilla_Mutex_h
    1.12 +
    1.13 +#include "prlock.h"
    1.14 +
    1.15 +#include "mozilla/BlockingResourceBase.h"
    1.16 +#include "mozilla/GuardObjects.h"
    1.17 +
    1.18 +//
    1.19 +// Provides:
    1.20 +//
    1.21 +//  - Mutex, a non-recursive mutex
    1.22 +//  - MutexAutoLock, an RAII class for ensuring that Mutexes are properly 
    1.23 +//    locked and unlocked
    1.24 +//  - MutexAutoUnlock, complementary sibling to MutexAutoLock
    1.25 +//
    1.26 +//  - OffTheBooksMutex, a non-recursive mutex that doesn't do leak checking
    1.27 +//  - OffTheBooksMutexAuto{Lock,Unlock} - Like MutexAuto{Lock,Unlock}, but for
    1.28 +//    an OffTheBooksMutex.
    1.29 +//
    1.30 +// Using MutexAutoLock/MutexAutoUnlock etc. is MUCH preferred to making bare
    1.31 +// calls to Lock and Unlock.
    1.32 +//
    1.33 +namespace mozilla {
    1.34 +
    1.35 +/**
    1.36 + * OffTheBooksMutex is identical to Mutex, except that OffTheBooksMutex doesn't
    1.37 + * include leak checking.  Sometimes you want to intentionally "leak" a mutex
    1.38 + * until shutdown; in these cases, OffTheBooksMutex is for you.
    1.39 + */
    1.40 +class NS_COM_GLUE OffTheBooksMutex : BlockingResourceBase
    1.41 +{
    1.42 +public:
    1.43 +    /**
    1.44 +     * @param name A name which can reference this lock
    1.45 +     * @returns If failure, nullptr
    1.46 +     *          If success, a valid Mutex* which must be destroyed
    1.47 +     *          by Mutex::DestroyMutex()
    1.48 +     **/
    1.49 +    OffTheBooksMutex(const char* name) :
    1.50 +        BlockingResourceBase(name, eMutex)
    1.51 +    {
    1.52 +        mLock = PR_NewLock();
    1.53 +        if (!mLock)
    1.54 +            NS_RUNTIMEABORT("Can't allocate mozilla::Mutex");
    1.55 +    }
    1.56 +
    1.57 +    ~OffTheBooksMutex()
    1.58 +    {
    1.59 +        NS_ASSERTION(mLock,
    1.60 +                     "improperly constructed Lock or double free");
    1.61 +        // NSPR does consistency checks for us
    1.62 +        PR_DestroyLock(mLock);
    1.63 +        mLock = 0;
    1.64 +    }
    1.65 +
    1.66 +#ifndef DEBUG
    1.67 +    /**
    1.68 +     * Lock
    1.69 +     * @see prlock.h
    1.70 +     **/
    1.71 +    void Lock()
    1.72 +    {
    1.73 +        PR_Lock(mLock);
    1.74 +    }
    1.75 +
    1.76 +    /**
    1.77 +     * Unlock
    1.78 +     * @see prlock.h
    1.79 +     **/
    1.80 +    void Unlock()
    1.81 +    {
    1.82 +        PR_Unlock(mLock);
    1.83 +    }
    1.84 +
    1.85 +    /**
    1.86 +     * AssertCurrentThreadOwns
    1.87 +     * @see prlock.h
    1.88 +     **/
    1.89 +    void AssertCurrentThreadOwns () const
    1.90 +    {
    1.91 +    }
    1.92 +
    1.93 +    /**
    1.94 +     * AssertNotCurrentThreadOwns
    1.95 +     * @see prlock.h
    1.96 +     **/
    1.97 +    void AssertNotCurrentThreadOwns () const
    1.98 +    {
    1.99 +    }
   1.100 +
   1.101 +#else
   1.102 +    void Lock();
   1.103 +    void Unlock();
   1.104 +
   1.105 +    void AssertCurrentThreadOwns () const
   1.106 +    {
   1.107 +        PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mLock);
   1.108 +    }
   1.109 +
   1.110 +    void AssertNotCurrentThreadOwns () const
   1.111 +    {
   1.112 +        // FIXME bug 476536
   1.113 +    }
   1.114 +
   1.115 +#endif  // ifndef DEBUG
   1.116 +
   1.117 +private:
   1.118 +    OffTheBooksMutex();
   1.119 +    OffTheBooksMutex(const OffTheBooksMutex&);
   1.120 +    OffTheBooksMutex& operator=(const OffTheBooksMutex&);
   1.121 +
   1.122 +    PRLock* mLock;
   1.123 +
   1.124 +    friend class CondVar;
   1.125 +};
   1.126 +
   1.127 +/**
   1.128 + * Mutex
   1.129 + * When possible, use MutexAutoLock/MutexAutoUnlock to lock/unlock this
   1.130 + * mutex within a scope, instead of calling Lock/Unlock directly.
   1.131 + */
   1.132 +class NS_COM_GLUE Mutex : public OffTheBooksMutex
   1.133 +{
   1.134 +public:
   1.135 +   Mutex(const char* name)
   1.136 +       : OffTheBooksMutex(name)
   1.137 +   {
   1.138 +       MOZ_COUNT_CTOR(Mutex);
   1.139 +   }
   1.140 +
   1.141 +   ~Mutex()
   1.142 +   {
   1.143 +       MOZ_COUNT_DTOR(Mutex);
   1.144 +   }
   1.145 +
   1.146 +private:
   1.147 +    Mutex();
   1.148 +    Mutex(const Mutex&);
   1.149 +    Mutex& operator=(const Mutex&);
   1.150 +};
   1.151 +
   1.152 +/**
   1.153 + * MutexAutoLock
   1.154 + * Acquires the Mutex when it enters scope, and releases it when it leaves 
   1.155 + * scope.
   1.156 + *
   1.157 + * MUCH PREFERRED to bare calls to Mutex.Lock and Unlock.
   1.158 + */ 
   1.159 +template<typename T>
   1.160 +class NS_COM_GLUE MOZ_STACK_CLASS BaseAutoLock
   1.161 +{
   1.162 +public:
   1.163 +    /**
   1.164 +     * Constructor
   1.165 +     * The constructor aquires the given lock.  The destructor
   1.166 +     * releases the lock.
   1.167 +     * 
   1.168 +     * @param aLock A valid mozilla::Mutex* returned by 
   1.169 +     *              mozilla::Mutex::NewMutex. 
   1.170 +     **/
   1.171 +    BaseAutoLock(T& aLock MOZ_GUARD_OBJECT_NOTIFIER_PARAM) :
   1.172 +        mLock(&aLock)
   1.173 +    {
   1.174 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.175 +        NS_ASSERTION(mLock, "null mutex");
   1.176 +        mLock->Lock();
   1.177 +    }
   1.178 +    
   1.179 +    ~BaseAutoLock(void) {
   1.180 +        mLock->Unlock();
   1.181 +    }
   1.182 + 
   1.183 +private:
   1.184 +    BaseAutoLock();
   1.185 +    BaseAutoLock(BaseAutoLock&);
   1.186 +    BaseAutoLock& operator=(BaseAutoLock&);
   1.187 +    static void* operator new(size_t) CPP_THROW_NEW;
   1.188 +    static void operator delete(void*);
   1.189 +
   1.190 +    T* mLock;
   1.191 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.192 +};
   1.193 +
   1.194 +typedef BaseAutoLock<Mutex> MutexAutoLock;
   1.195 +typedef BaseAutoLock<OffTheBooksMutex> OffTheBooksMutexAutoLock;
   1.196 +
   1.197 +/**
   1.198 + * MutexAutoUnlock
   1.199 + * Releases the Mutex when it enters scope, and re-acquires it when it leaves 
   1.200 + * scope.
   1.201 + *
   1.202 + * MUCH PREFERRED to bare calls to Mutex.Unlock and Lock.
   1.203 + */ 
   1.204 +template<typename T>
   1.205 +class NS_COM_GLUE MOZ_STACK_CLASS BaseAutoUnlock 
   1.206 +{
   1.207 +public:
   1.208 +    BaseAutoUnlock(T& aLock MOZ_GUARD_OBJECT_NOTIFIER_PARAM) :
   1.209 +        mLock(&aLock)
   1.210 +    {
   1.211 +        MOZ_GUARD_OBJECT_NOTIFIER_INIT;
   1.212 +        NS_ASSERTION(mLock, "null lock");
   1.213 +        mLock->Unlock();
   1.214 +    }
   1.215 +
   1.216 +    ~BaseAutoUnlock() 
   1.217 +    {
   1.218 +        mLock->Lock();
   1.219 +    }
   1.220 +
   1.221 +private:
   1.222 +    BaseAutoUnlock();
   1.223 +    BaseAutoUnlock(BaseAutoUnlock&);
   1.224 +    BaseAutoUnlock& operator =(BaseAutoUnlock&);
   1.225 +    static void* operator new(size_t) CPP_THROW_NEW;
   1.226 +    static void operator delete(void*);
   1.227 +     
   1.228 +    T* mLock;
   1.229 +    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
   1.230 +};
   1.231 +
   1.232 +typedef BaseAutoUnlock<Mutex> MutexAutoUnlock;
   1.233 +typedef BaseAutoUnlock<OffTheBooksMutex> OffTheBooksMutexAutoUnlock;
   1.234 +
   1.235 +} // namespace mozilla
   1.236 +
   1.237 +
   1.238 +#endif // ifndef mozilla_Mutex_h

mercurial