LCOV - code coverage report
Current view: directory - storage/src - SQLiteMutex.h (source / functions) Found Hit Coverage
Test: app.info Lines: 43 43 100.0 %
Date: 2012-06-02 Functions: 11 11 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
       2                 :  * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
       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                 :  * Mozilla Corporation.
      20                 :  * Portions created by the Initial Developer are Copyright (C) 2009
      21                 :  * the Initial Developer. All Rights Reserved.
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *   Shawn Wilsher <me@shawnwilsher.com> (Original Author)
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #ifndef mozilla_storage_SQLiteMutex_h_
      41                 : #define mozilla_storage_SQLiteMutex_h_
      42                 : 
      43                 : #include "mozilla/BlockingResourceBase.h"
      44                 : #include "sqlite3.h"
      45                 : 
      46                 : namespace mozilla {
      47                 : namespace storage {
      48                 : 
      49                 : /**
      50                 :  * Wrapper class for sqlite3_mutexes.  To be used whenever we want to use a
      51                 :  * sqlite3_mutex.
      52                 :  *
      53                 :  * @warning Never EVER wrap the same sqlite3_mutex with a different SQLiteMutex.
      54                 :  *          If you do this, you void the deadlock detector's warranty!
      55                 :  */
      56                 : class SQLiteMutex : private BlockingResourceBase
      57            3282 : {
      58                 : public:
      59                 :   /**
      60                 :    * Constructs a wrapper for a sqlite3_mutex that has deadlock detecting.
      61                 :    *
      62                 :    * @param aName
      63                 :    *        A name which can be used to reference this mutex.
      64                 :    */
      65            3289 :   SQLiteMutex(const char *aName)
      66                 :   : BlockingResourceBase(aName, eMutex)
      67            3289 :   , mMutex(NULL)
      68                 :   {
      69            3289 :   }
      70                 : 
      71                 :   /**
      72                 :    * Sets the mutex that we are wrapping.  We generally do not have access to
      73                 :    * our mutex at class construction, so we have to set it once we get access to
      74                 :    * it.
      75                 :    *
      76                 :    * @param aMutex
      77                 :    *        The sqlite3_mutex that we are going to wrap.
      78                 :    */
      79            3285 :   void initWithMutex(sqlite3_mutex *aMutex)
      80                 :   {
      81            3285 :     NS_ASSERTION(aMutex, "You must pass in a valid mutex!");
      82            3285 :     NS_ASSERTION(!mMutex, "A mutex has already been set for this!");
      83            3285 :     mMutex = aMutex;
      84            3285 :   }
      85                 : 
      86                 : #if !defined(DEBUG) || defined(MOZ_NATIVE_SQLITE)
      87                 :   /**
      88                 :    * Acquires the mutex.
      89                 :    */
      90                 :   void lock()
      91                 :   {
      92                 :     sqlite3_mutex_enter(mMutex);
      93                 :   }
      94                 : 
      95                 :   /**
      96                 :    * Releases the mutex.
      97                 :    */
      98                 :   void unlock()
      99                 :   {
     100                 :     sqlite3_mutex_leave(mMutex);
     101                 :   }
     102                 : 
     103                 :   /**
     104                 :    * Asserts that the current thread owns the mutex.
     105                 :    */
     106                 :   void assertCurrentThreadOwns()
     107                 :   {
     108                 :   }
     109                 : 
     110                 :   /**
     111                 :    * Asserts that the current thread does not own the mutex.
     112                 :    */
     113                 :   void assertNotCurrentThreadOwns()
     114                 :   {
     115                 :   }
     116                 : 
     117                 : #else
     118         1065795 :   void lock()
     119                 :   {
     120         1065795 :     NS_ASSERTION(mMutex, "No mutex associated with this wrapper!");
     121                 : 
     122                 :     // While SQLite Mutexes may be recursive, in our own code we do not want to
     123                 :     // treat them as such.
     124         1065795 :     CallStack callContext = CallStack();
     125                 : 
     126         1065781 :     CheckAcquire(callContext);
     127         1065864 :     sqlite3_mutex_enter(mMutex);
     128         1065836 :     Acquire(callContext); // Call is protected by us holding the mutex.
     129         1065787 :   }
     130                 : 
     131         1065873 :   void unlock()
     132                 :   {
     133         1065873 :     NS_ASSERTION(mMutex, "No mutex associated with this wrapper!");
     134                 : 
     135                 :     // While SQLite Mutexes may be recursive, in our own code we do not want to
     136                 :     // treat them as such.
     137         1065873 :     Release(); // Call is protected by us holding the mutex.
     138         1065873 :     sqlite3_mutex_leave(mMutex);
     139         1065873 :   }
     140                 : 
     141            1107 :   void assertCurrentThreadOwns()
     142                 :   {
     143            1107 :     NS_ASSERTION(mMutex, "No mutex associated with this wrapper!");
     144            1107 :     NS_ASSERTION(sqlite3_mutex_held(mMutex),
     145                 :                  "Mutex is not held, but we expect it to be!");
     146            1107 :   }
     147                 : 
     148              32 :   void assertNotCurrentThreadOwns()
     149                 :   {
     150              32 :     NS_ASSERTION(mMutex, "No mutex associated with this wrapper!");
     151              32 :     NS_ASSERTION(sqlite3_mutex_notheld(mMutex),
     152                 :                  "Mutex is held, but we expect it to not be!");
     153              32 :   }
     154                 : #endif // ifndef DEBUG
     155                 : 
     156                 : private:
     157                 :   sqlite3_mutex *mMutex;
     158                 : };
     159                 : 
     160                 : /**
     161                 :  * Automatically acquires the mutex when it enters scope, and releases it when
     162                 :  * it leaves scope.
     163                 :  */
     164                 : class NS_STACK_CLASS SQLiteMutexAutoLock
     165                 : {
     166                 : public:
     167          165851 :   SQLiteMutexAutoLock(SQLiteMutex &aMutex)
     168          165851 :   : mMutex(aMutex)
     169                 :   {
     170          165851 :     mMutex.lock();
     171          165851 :   }
     172                 : 
     173          165851 :   ~SQLiteMutexAutoLock()
     174                 :   {
     175          165851 :     mMutex.unlock();
     176          165851 :   }
     177                 : 
     178                 : private:
     179                 :   SQLiteMutex &mMutex;
     180                 : };
     181                 : 
     182                 : /**
     183                 :  * Automatically releases the mutex when it enters scope, and acquires it when
     184                 :  * it leaves scope.
     185                 :  */
     186                 : class NS_STACK_CLASS SQLiteMutexAutoUnlock
     187                 : {
     188                 : public:
     189              22 :   SQLiteMutexAutoUnlock(SQLiteMutex &aMutex)
     190              22 :   : mMutex(aMutex)
     191                 :   {
     192              22 :     mMutex.unlock();
     193              22 :   }
     194                 : 
     195              22 :   ~SQLiteMutexAutoUnlock()
     196                 :   {
     197              22 :     mMutex.lock();
     198              22 :   }
     199                 : 
     200                 : private:
     201                 :   SQLiteMutex &mMutex;
     202                 : };
     203                 : 
     204                 : } // namespace storage
     205                 : } // namespace mozilla
     206                 : 
     207                 : #endif // mozilla_storage_SQLiteMutex_h_

Generated by: LCOV version 1.7