LCOV - app.info - objdir/dist/include/mozilla/Mutex.h

LCOV - code coverage report
| Current view: | directory - objdir/dist/include/mozilla - Mutex.h (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 37 | 36 | 97.3 %
| Date: | 2012-06-02 | Functions: | 10 | 8 | 80.0 %

           1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
           2                 :  * vim: sw=4 ts=4 et :
           3                 :  * ***** BEGIN LICENSE BLOCK *****
           4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
           5                 :  *
           6                 :  * The contents of this file are subject to the Mozilla Public License Version
           7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
           8                 :  * the License. You may obtain a copy of the License at
           9                 :  * http://www.mozilla.org/MPL/
          10                 :  *
          11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
          12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
          13                 :  * for the specific language governing rights and limitations under the
          14                 :  * License.
          15                 :  *
          16                 :  * The Original Code is mozilla.org code.
          17                 :  *
          18                 :  * The Initial Developer of the Original Code is
          19                 :  * Netscape Communications Corporation.
          20                 :  * Portions created by the Initial Developer are Copyright (C) 1998
          21                 :  * the Initial Developer. All Rights Reserved.
          22                 :  *
          23                 :  * Contributor(s):
          24                 :  *
          25                 :  * Alternatively, the contents of this file may be used under the terms of
          26                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
          27                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
          28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
          29                 :  * of those above. If you wish to allow use of your version of this file only
          30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
          31                 :  * use your version of this file under the terms of the MPL, indicate your
          32                 :  * decision by deleting the provisions above and replace them with the notice
          33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
          34                 :  * the provisions above, a recipient may use your version of this file under
          35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
          36                 :  *
          37                 :  * ***** END LICENSE BLOCK ***** */
          38                 :
          39                 : #ifndef mozilla_Mutex_h
          40                 : #define mozilla_Mutex_h
          41                 :
          42                 : #include "prlock.h"
          43                 :
          44                 : #include "mozilla/BlockingResourceBase.h"
          45                 : #include "mozilla/GuardObjects.h"
          46                 :
          47                 : //
          48                 : // Provides:
          49                 : //
          50                 : //  - Mutex, a non-recursive mutex
          51                 : //  - MutexAutoLock, an RAII class for ensuring that Mutexes are properly
          52                 : //    locked and unlocked
          53                 : //  - MutexAutoUnlock, complementary sibling to MutexAutoLock
          54                 : //
          55                 : // Using MutexAutoLock/MutexAutoUnlock is MUCH preferred to making bare
          56                 : // calls to Mutex.Lock and Unlock.
          57                 : //
          58                 : namespace mozilla {
          59                 :
          60                 :
          61                 : /**
          62                 :  * Mutex
          63                 :  * When possible, use MutexAutoLock/MutexAutoUnlock to lock/unlock this
          64                 :  * mutex within a scope, instead of calling Lock/Unlock directly.
          65                 :  **/
          66                 :
          67                 : class NS_COM_GLUE Mutex : BlockingResourceBase
          68                 : {
          69                 : public:
          70                 :     /**
          71                 :      * Mutex
          72                 :      * @param name A name which can reference this lock
          73                 :      * @returns If failure, nsnull
          74                 :      *          If success, a valid Mutex* which must be destroyed
          75                 :      *          by Mutex::DestroyMutex()
          76                 :      **/
          77          121743 :     Mutex(const char* name) :
          78          121743 :         BlockingResourceBase(name, eMutex)
          79                 :     {
          80          121743 :         MOZ_COUNT_CTOR(Mutex);
          81          121743 :         mLock = PR_NewLock();
          82          121743 :         if (!mLock)
          83               0 :             NS_RUNTIMEABORT("Can't allocate mozilla::Mutex");
          84          121743 :     }
          85                 :
          86                 :     /**
          87                 :      * ~Mutex
          88                 :      **/
          89          121618 :     ~Mutex()
          90          121618 :     {
          91          121618 :         NS_ASSERTION(mLock,
          92                 :                      "improperly constructed Lock or double free");
          93                 :         // NSPR does consistency checks for us
          94          121618 :         PR_DestroyLock(mLock);
          95          121618 :         mLock = 0;
          96          121618 :         MOZ_COUNT_DTOR(Mutex);
          97          121618 :     }
          98                 :
          99                 : #ifndef DEBUG
         100                 :     /**
         101                 :      * Lock
         102                 :      * @see prlock.h
         103                 :      **/
         104                 :     void Lock()
         105                 :     {
         106                 :         PR_Lock(mLock);
         107                 :     }
         108                 :
         109                 :     /**
         110                 :      * Unlock
         111                 :      * @see prlock.h
         112                 :      **/
         113                 :     void Unlock()
         114                 :     {
         115                 :         PR_Unlock(mLock);
         116                 :     }
         117                 :
         118                 :     /**
         119                 :      * AssertCurrentThreadOwns
         120                 :      * @see prlock.h
         121                 :      **/
         122                 :     void AssertCurrentThreadOwns () const
         123                 :     {
         124                 :     }
         125                 :
         126                 :     /**
         127                 :      * AssertNotCurrentThreadOwns
         128                 :      * @see prlock.h
         129                 :      **/
         130                 :     void AssertNotCurrentThreadOwns () const
         131                 :     {
         132                 :     }
         133                 :
         134                 : #else
         135                 :     void Lock();
         136                 :     void Unlock();
         137                 :
         138           84194 :     void AssertCurrentThreadOwns () const
         139                 :     {
         140           84194 :         PR_ASSERT_CURRENT_THREAD_OWNS_LOCK(mLock);
         141           84194 :     }
         142                 :
         143          276827 :     void AssertNotCurrentThreadOwns () const
         144                 :     {
         145                 :         // FIXME bug 476536
         146          276827 :     }
         147                 :
         148                 : #endif  // ifndef DEBUG
         149                 :
         150                 : private:
         151                 :     Mutex();
         152                 :     Mutex(const Mutex&);
         153                 :     Mutex& operator=(const Mutex&);
         154                 :
         155                 :     PRLock* mLock;
         156                 :
         157                 :     friend class CondVar;
         158                 : };
         159                 :
         160                 :
         161                 : /**
         162                 :  * MutexAutoLock
         163                 :  * Acquires the Mutex when it enters scope, and releases it when it leaves
         164                 :  * scope.
         165                 :  *
         166                 :  * MUCH PREFERRED to bare calls to Mutex.Lock and Unlock.
         167                 :  */
         168                 : template<typename T>
         169                 : class NS_COM_GLUE NS_STACK_CLASS BaseAutoLock
         170                 : {
         171                 : public:
         172                 :     /**
         173                 :      * Constructor
         174                 :      * The constructor aquires the given lock.  The destructor
         175                 :      * releases the lock.
         176                 :      *
         177                 :      * @param aLock A valid mozilla::Mutex* returned by
         178                 :      *              mozilla::Mutex::NewMutex.
         179                 :      **/
         180         2833128 :     BaseAutoLock(T& aLock MOZ_GUARD_OBJECT_NOTIFIER_PARAM) :
         181         2833128 :         mLock(&aLock)
         182                 :     {
         183         2833124 :         MOZ_GUARD_OBJECT_NOTIFIER_INIT;
         184         2833112 :         NS_ASSERTION(mLock, "null mutex");
         185         2833112 :         mLock->Lock();
         186         2833121 :     }
         187                 :
         188         2833117 :     ~BaseAutoLock(void) {
         189         2833117 :         mLock->Unlock();
         190         2833107 :     }
         191                 :
         192                 : private:
         193                 :     BaseAutoLock();
         194                 :     BaseAutoLock(BaseAutoLock&);
         195                 :     BaseAutoLock& operator=(BaseAutoLock&);
         196                 :     static void* operator new(size_t) CPP_THROW_NEW;
         197                 :     static void operator delete(void*);
         198                 :
         199                 :     T* mLock;
         200                 :     MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
         201                 : };
         202                 :
         203                 : typedef BaseAutoLock<Mutex> MutexAutoLock;
         204                 :
         205                 : /**
         206                 :  * MutexAutoUnlock
         207                 :  * Releases the Mutex when it enters scope, and re-acquires it when it leaves
         208                 :  * scope.
         209                 :  *
         210                 :  * MUCH PREFERRED to bare calls to Mutex.Unlock and Lock.
         211                 :  */
         212                 : template<typename T>
         213                 : class NS_COM_GLUE NS_STACK_CLASS BaseAutoUnlock
         214                 : {
         215                 : public:
         216            1960 :     BaseAutoUnlock(T& aLock MOZ_GUARD_OBJECT_NOTIFIER_PARAM) :
         217            1960 :         mLock(&aLock)
         218                 :     {
         219            1960 :         MOZ_GUARD_OBJECT_NOTIFIER_INIT;
         220            1960 :         NS_ASSERTION(mLock, "null lock");
         221            1960 :         mLock->Unlock();
         222            1960 :     }
         223                 :
         224            1960 :     ~BaseAutoUnlock()
         225                 :     {
         226            1960 :         mLock->Lock();
         227            1960 :     }
         228                 :
         229                 : private:
         230                 :     BaseAutoUnlock();
         231                 :     BaseAutoUnlock(BaseAutoUnlock&);
         232                 :     BaseAutoUnlock& operator =(BaseAutoUnlock&);
         233                 :     static void* operator new(size_t) CPP_THROW_NEW;
         234                 :     static void operator delete(void*);
         235                 :
         236                 :     T* mLock;
         237                 :     MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
         238                 : };
         239                 :
         240                 : typedef BaseAutoUnlock<Mutex> MutexAutoUnlock;
         241                 :
         242                 : } // namespace mozilla
         243                 :
         244                 :
         245                 : #endif // ifndef mozilla_Mutex_h

Generated by: LCOV version 1.7