Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef mozilla_storage_SQLiteMutex_h_ |
michael@0 | 8 | #define mozilla_storage_SQLiteMutex_h_ |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/BlockingResourceBase.h" |
michael@0 | 11 | #include "sqlite3.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | namespace storage { |
michael@0 | 15 | |
michael@0 | 16 | /** |
michael@0 | 17 | * Wrapper class for sqlite3_mutexes. To be used whenever we want to use a |
michael@0 | 18 | * sqlite3_mutex. |
michael@0 | 19 | * |
michael@0 | 20 | * @warning Never EVER wrap the same sqlite3_mutex with a different SQLiteMutex. |
michael@0 | 21 | * If you do this, you void the deadlock detector's warranty! |
michael@0 | 22 | */ |
michael@0 | 23 | class SQLiteMutex : private BlockingResourceBase |
michael@0 | 24 | { |
michael@0 | 25 | public: |
michael@0 | 26 | /** |
michael@0 | 27 | * Constructs a wrapper for a sqlite3_mutex that has deadlock detecting. |
michael@0 | 28 | * |
michael@0 | 29 | * @param aName |
michael@0 | 30 | * A name which can be used to reference this mutex. |
michael@0 | 31 | */ |
michael@0 | 32 | SQLiteMutex(const char *aName) |
michael@0 | 33 | : BlockingResourceBase(aName, eMutex) |
michael@0 | 34 | , mMutex(nullptr) |
michael@0 | 35 | { |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | /** |
michael@0 | 39 | * Sets the mutex that we are wrapping. We generally do not have access to |
michael@0 | 40 | * our mutex at class construction, so we have to set it once we get access to |
michael@0 | 41 | * it. |
michael@0 | 42 | * |
michael@0 | 43 | * @param aMutex |
michael@0 | 44 | * The sqlite3_mutex that we are going to wrap. |
michael@0 | 45 | */ |
michael@0 | 46 | void initWithMutex(sqlite3_mutex *aMutex) |
michael@0 | 47 | { |
michael@0 | 48 | NS_ASSERTION(aMutex, "You must pass in a valid mutex!"); |
michael@0 | 49 | NS_ASSERTION(!mMutex, "A mutex has already been set for this!"); |
michael@0 | 50 | mMutex = aMutex; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | #if !defined(DEBUG) || defined(MOZ_NATIVE_SQLITE) |
michael@0 | 54 | /** |
michael@0 | 55 | * Acquires the mutex. |
michael@0 | 56 | */ |
michael@0 | 57 | void lock() |
michael@0 | 58 | { |
michael@0 | 59 | sqlite3_mutex_enter(mMutex); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | /** |
michael@0 | 63 | * Releases the mutex. |
michael@0 | 64 | */ |
michael@0 | 65 | void unlock() |
michael@0 | 66 | { |
michael@0 | 67 | sqlite3_mutex_leave(mMutex); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | /** |
michael@0 | 71 | * Asserts that the current thread owns the mutex. |
michael@0 | 72 | */ |
michael@0 | 73 | void assertCurrentThreadOwns() |
michael@0 | 74 | { |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | /** |
michael@0 | 78 | * Asserts that the current thread does not own the mutex. |
michael@0 | 79 | */ |
michael@0 | 80 | void assertNotCurrentThreadOwns() |
michael@0 | 81 | { |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | #else |
michael@0 | 85 | void lock() |
michael@0 | 86 | { |
michael@0 | 87 | NS_ASSERTION(mMutex, "No mutex associated with this wrapper!"); |
michael@0 | 88 | |
michael@0 | 89 | // While SQLite Mutexes may be recursive, in our own code we do not want to |
michael@0 | 90 | // treat them as such. |
michael@0 | 91 | CallStack callContext = CallStack(); |
michael@0 | 92 | |
michael@0 | 93 | CheckAcquire(callContext); |
michael@0 | 94 | sqlite3_mutex_enter(mMutex); |
michael@0 | 95 | Acquire(callContext); // Call is protected by us holding the mutex. |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | void unlock() |
michael@0 | 99 | { |
michael@0 | 100 | NS_ASSERTION(mMutex, "No mutex associated with this wrapper!"); |
michael@0 | 101 | |
michael@0 | 102 | // While SQLite Mutexes may be recursive, in our own code we do not want to |
michael@0 | 103 | // treat them as such. |
michael@0 | 104 | Release(); // Call is protected by us holding the mutex. |
michael@0 | 105 | sqlite3_mutex_leave(mMutex); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | void assertCurrentThreadOwns() |
michael@0 | 109 | { |
michael@0 | 110 | NS_ASSERTION(mMutex, "No mutex associated with this wrapper!"); |
michael@0 | 111 | NS_ASSERTION(sqlite3_mutex_held(mMutex), |
michael@0 | 112 | "Mutex is not held, but we expect it to be!"); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | void assertNotCurrentThreadOwns() |
michael@0 | 116 | { |
michael@0 | 117 | NS_ASSERTION(mMutex, "No mutex associated with this wrapper!"); |
michael@0 | 118 | NS_ASSERTION(sqlite3_mutex_notheld(mMutex), |
michael@0 | 119 | "Mutex is held, but we expect it to not be!"); |
michael@0 | 120 | } |
michael@0 | 121 | #endif // ifndef DEBUG |
michael@0 | 122 | |
michael@0 | 123 | private: |
michael@0 | 124 | sqlite3_mutex *mMutex; |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * Automatically acquires the mutex when it enters scope, and releases it when |
michael@0 | 129 | * it leaves scope. |
michael@0 | 130 | */ |
michael@0 | 131 | class MOZ_STACK_CLASS SQLiteMutexAutoLock |
michael@0 | 132 | { |
michael@0 | 133 | public: |
michael@0 | 134 | SQLiteMutexAutoLock(SQLiteMutex &aMutex) |
michael@0 | 135 | : mMutex(aMutex) |
michael@0 | 136 | { |
michael@0 | 137 | mMutex.lock(); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | ~SQLiteMutexAutoLock() |
michael@0 | 141 | { |
michael@0 | 142 | mMutex.unlock(); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | private: |
michael@0 | 146 | SQLiteMutex &mMutex; |
michael@0 | 147 | }; |
michael@0 | 148 | |
michael@0 | 149 | /** |
michael@0 | 150 | * Automatically releases the mutex when it enters scope, and acquires it when |
michael@0 | 151 | * it leaves scope. |
michael@0 | 152 | */ |
michael@0 | 153 | class MOZ_STACK_CLASS SQLiteMutexAutoUnlock |
michael@0 | 154 | { |
michael@0 | 155 | public: |
michael@0 | 156 | SQLiteMutexAutoUnlock(SQLiteMutex &aMutex) |
michael@0 | 157 | : mMutex(aMutex) |
michael@0 | 158 | { |
michael@0 | 159 | mMutex.unlock(); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | ~SQLiteMutexAutoUnlock() |
michael@0 | 163 | { |
michael@0 | 164 | mMutex.lock(); |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | private: |
michael@0 | 168 | SQLiteMutex &mMutex; |
michael@0 | 169 | }; |
michael@0 | 170 | |
michael@0 | 171 | } // namespace storage |
michael@0 | 172 | } // namespace mozilla |
michael@0 | 173 | |
michael@0 | 174 | #endif // mozilla_storage_SQLiteMutex_h_ |